单击通知不会启动活动 [英] Click on Notification doesn't start activity

查看:17
本文介绍了单击通知不会启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建来自服务的通知;显示了通知,但当我点击它时,没有任何反应:它应该打开一个活动.

I am creating a notification from a service; the notification is shown, but when I click on it, nothing happens: It was supposed to open an activity.

我的代码:

NotificationManager notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "test", when);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |   Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0, 
notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(this, "title", "message", intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

但是,如果我在活动中使用几乎相同的代码,我可以单击通知,然后显示我的活动.我做错了什么?

However if I use pretty much the same code from inside an activity, I can click on the notification, and my activity is shown. What am I doing wrong?

事实证明,这段代码没有任何问题,还有一个不同的问题:当我的服务完成时,它使用上面的代码创建了通知.但是,该服务还广播它已完成,并且接收者创建了另一个通知,该通知使用不同的代码来创建通知(没有 PendingIntents,因此单击通知时没有定义的操作),并且该通知必须自己放置而不是原来的,正确的.

It turns out that there was nothing wrong with this code, there was a different issue: When my service finished, it created the notification with the code above. However, the service also broadcasted that it was finished, and the receiver created another notification, which used a different code to create the notification (with no PendingIntents, so no defined action when the notification is clicked), and that notification must have placed itself instead of the original, correct one.

推荐答案

除了在 Android 3.0 以上使用 Notification.Builder 或支持库 v4 中的 NotificationCompat.Builder 正如@Raghunandan 在评论中建议的那样,我遇到了同样的问题为您的问题提供可能的通用解决方案.

On top of using Notification.Builder for above Android 3.0, or NotificationCompat.Builder in support library v4 as @Raghunandan suggests in the comment, I had the same problem with a possible common solution to your problem.

这是特定于 4.4 的,如下所示:问题 63236:通知TaskStackBuilder.getPendingIntent() 未打开 Activity 和这里 问题61850:重新安装应用程序后,KitKat 通知操作 Pending Intent 失败

This is specific to 4.4 as seen here:Issue 63236:Notification with TaskStackBuilder.getPendingIntent() is not open the Activity and here Issue 61850: KitKat notification action Pending Intent fails after application re-install

一种已确认的解决方案是对与您将要创建的相同的 PendingIntent 执行 cancel() 操作.

One confirmed solution is to perform cancel() operation on an identical PendingIntent with the one you are about to create.

对我有用的是修改目标活动的清单定义并添加android:exported="true" 在目标活动的活动"标签内.在我假设的情况下,这将是 MainActivity.

What worked for me was to modify the target Activity's manifest definition and add android:exported="true" within "activity" tags for the target Activity. That would be MainActivity in your case I assume.

示例:

<activity
        android:name="com.your.MainActivity"
        android:exported="true" >
.
.
</activity>

这篇关于单击通知不会启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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