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

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

问题描述

我一直有一个通知,不开放/打算被点击时,正确的行为有问题。

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

我的通知code(位于一类延伸服务):

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天全站免登陆