我正在从服务创建通知;显示了通知,但是当我单击它时,没有任何反应:它应该打开一个活动。
My code:
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);
但是,如果我在活动内部使用几乎相同的代码,我可以单击通知,然后显示我的活动。我究竟做错了什么?
EDIT:事实证明这段代码没有任何问题,有一个不同的问题:
当我的服务完成时,它使用上面的代码创建了通知。但是,该服务还广播它已完成,并且接收者创建了另一个通知,该通知使用不同的代码来创建通知(没有 PendingIntents,因此单击通知时没有定义的操作),并且该通知必须已放置其自身而不是原来的,正确的。
除了在Android 3.0以上使用Notification.Builder,或在支持库v4中使用NotificationCompat.Builder(如@Raghunandan在评论中建议的那样)之外,我也遇到了同样的问题,并有一个可能的通用解决方案来解决您的问题。
这是 4.4 特有的,如下所示:问题 63236:TaskStackBuilder.getPendingIntent() 的通知未打开活动和这里问题 61850:KitKat 通知操作待处理意图在应用程序重新安装后失败
一种已确认的解决方案是对与您要创建的 PendingIntent 相同的 PendingIntent 执行 cancel() 操作。
对我有用的是修改目标活动的清单定义并添加安卓:导出=“真”在目标活动的“活动”标签内。我认为在你的情况下这将是 MainActivity 。
Example:
<activity
android:name="com.your.MainActivity"
android:exported="true" >
.
.
</activity>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)