Android 如何打开在单击时启动通知的活动?

Android 如何打开在单击时启动通知的活动?,android,Android,我想打开发出通知的活动,如果用户单击该活动,则应将其定向到该活动。通知应该消失。1。创建任何通知,例如 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello Wor

我想打开发出通知的活动,如果用户单击该活动,则应将其定向到该活动。通知应该消失。

1。创建任何通知,例如

NotificationCompat.Builder mBuilder =

    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
2.现在,如果您想关联任何活动,比如说
ResultActivity
,请创建
pendingent

Intent resultIntent = new Intent(this, ResultActivity.class);

PendingIntent resultPendingIntent =

    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);
3:将挂起的意图设置为notification builder

mBuilder.setContentIntent(resultPendingIntent);

1。创建任何通知,例如

NotificationCompat.Builder mBuilder =

    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");
2.现在,如果您想关联任何活动,比如说
ResultActivity
,请创建
pendingent

Intent resultIntent = new Intent(this, ResultActivity.class);

PendingIntent resultPendingIntent =

    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);
3:将挂起的意图设置为notification builder

mBuilder.setContentIntent(resultPendingIntent);

将此方法用于android中的开放式活动

   // Notification Method
private void Notification(String notificationTitle,
        String notificationMessage) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    android.app.Notification notification = new android.app.Notification(
            R.drawable.ic_launcher, "Message from Binesh Kumar! (Android Developer)",
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, AndroidNotifications.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(AndroidNotifications.this,
            notificationTitle, notificationMessage, pendingIntent);
    notificationManager.notify(10001, notification);
}

将此方法用于android中的开放式活动

   // Notification Method
private void Notification(String notificationTitle,
        String notificationMessage) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    android.app.Notification notification = new android.app.Notification(
            R.drawable.ic_launcher, "Message from Binesh Kumar! (Android Developer)",
            System.currentTimeMillis());

    Intent notificationIntent = new Intent(this, AndroidNotifications.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    notification.setLatestEventInfo(AndroidNotifications.this,
            notificationTitle, notificationMessage, pendingIntent);
    notificationManager.notify(10001, notification);
}

这行得通吗,因为我已经试过了。它会给我一个通知,但我没有返回任何地方。这将工作,因为我已经尝试过这个。它会给我一个通知,但我没有返回任何地方。