简体   繁体   中英

How to open the activity that launched a notification when clicked on it?

I want to open the activity that gave a notification and the user should be directed to the activity if s/he clicks on it. And the notification should disappear.

use this method for open activity in 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);
}

1 . Create any notification something like

NotificationCompat.Builder mBuilder =

    new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!");

2. Now if you want to associate any activity say ResultActivity , create PendingIntent

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

PendingIntent resultPendingIntent =

    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

3: Set the pending intent to notification builder

mBuilder.setContentIntent(resultPendingIntent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM