简体   繁体   中英

Android Status Bar Notifications - Opening the correct activity when selecting a notification

I have been having a problem with a notification not opening/going to the correct activity when it has been clicked.

My notification code (located in a class which extends Service):

Context context = getApplicationContext();

    CharSequence contentTitle = "Notification";

    CharSequence contentText = "New Notification";

    final Notification notifyDetails =
        new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());

    Intent notifyIntent = new Intent(context, MainActivity.class);

    PendingIntent intent =
          PendingIntent.getActivity(context, 0,
          notifyIntent,  PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);

    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);

If I click the notification while the application which created the service is open, the notification disappears (due to the FLAG_AUTO_CANCEL) but the activity does not switch.

If I click the notification from the home screen, the notification disappears and my app is brought to the front, however it remains on the activity which was open before going to the home screen, instead of going to the main screen.

What am I doing wrong? How do I specify the activity that will be pulled up?

May have actually answered my own question:

Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), Main.class);

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