Android 状态栏通知 - 选择通知时打开正确的活动 [英] Android Status Bar Notifications - Opening the correct activity when selecting a notification

查看:18
本文介绍了Android 状态栏通知 - 选择通知时打开正确的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,通知在被点击后无法打开/转到正确的活动.

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);

如果我在创建服务的应用程序打开时单击通知,通知会消失(由于 FLAG_AUTO_CANCEL)但活动不会切换.

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);

这篇关于Android 状态栏通知 - 选择通知时打开正确的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆