开发者

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

I have been having a problem with a notifica开发者_StackOverflowtion 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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜