在通知点击时发送广播但是我无法正常工作.我看到了通知,但是当我单击它时,它永远不会触发我的广播接收器.这是我尝试过的: 我创建了一个自定义接收方:public class NotificationOnClickReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { Log.d(Constants.Engine.LOGGER_TAG_GCM, NotificationOnClickReceiver.class.toString() + " triggered"); }} 我已在我的全局应用程序(不是活动)上动态注册了此接收器mNotificationOnClickReceiver = new NotificationOnClickReceiver();IntentFilter intentFilterOnClick = new IntentFilter(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);getApplicationContext().registerReceiver(mNotificationOnClickReceiver, intentFilterOnClick);Log.e(Constants.Engine.LOGGER_TAG_DBG, "mNotificationOnClickReceiver = " + mNotificationOnClickReceiver.toString());过滤器只是一个字符串常量(我想知道这是否是问题) public static final String BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK = "push_notification.onclick"; 接收者意图设置:// When the user taps on the notification bar, this is the receiver that I want to be calledIntent pushNotificationIntent;Log.e(Constants.Engine.LOGGER_TAG_DBG, "Notification called!");pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);// Not sure if this causing the problempushNotificationIntent.setAction(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);// ensures that each notification and intent are uniqueint uniqueCode = new Random().nextInt(Integer.MAX_VALUE);PendingIntent pendingIntent = PendingIntent.getBroadcast( context, uniqueCode, pushNotificationIntent, // Receiver Intent PendingIntent.FLAG_CANCEL_CURRENT);Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) .setSmallIcon(mIconId) .setContentTitle(context.getString(R.string.myString)) .setContentText("My GCM Alert!") .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); 从日志看来,除了我的广播接收器从未被调用之外,其他一切似乎都起作用:02-22 15:47:47.092 16863-16863/com.myapp.test E/MYAPP_DBG: mNotificationOnClickReceiver = mytest.broadcastreceivers.NotificationOnClickReceiver@d78f55902-22 15:47:53.312 16863-17038/com.myapp.test E/MYAPP_DBG: Notification called!如果任何人都可以使用此功能,您能给我指出一个示例代码吗?我看不到我的错误在哪里.谢谢!解决方案这似乎是问题所在:pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);您实际上是在为广播PendingIntent创建一个明确的Intent.显式Intent不适用于动态注册的接收器,因为它们针对的是Receiver类,而不是实例.您将需要使用隐式的Intent,您可以通过使用操作String而不是Context和Class实例化Intent来实现.例如:pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);请注意,如果您的应用程序进程在单击Notification之前被杀死,则动态注册的Receiver实例也将消失,并且Notification的广播实际上不会做任何事情.根据所需的行为,可能更可取的是在清单中静态注册Receiver类,并使用显式的Intent.这样一来,即使您的应用已被终止,您的应用仍可以接收Notification广播.为此,您只需在清单中为NotificationOnClickReceiver添加一个<receiver>元素.<receiver android:name="NotificationOnClickReceiver" />尽管您仍可以在广播Intent上设置操作,但是这里不需要<intent-filter>,因为明确的Intent始终直接针对该类.如果Receiver仅用于该功能,则不必执行该操作,而可以将其完全删除.然后,您就可以删除第2步中的代码,因为您不再需要动态注册的实例.I have an app hooked with push notifications. When the user click on the notification, I want a broadcast receiver to be triggered instead of an activity.I looked at these threads and looks like this is perfectly possible and common.Android Notification Action is not fired (PendingIntent)Send broadcast on notification clickHowever I can't get it to work. I see the notification, but when I click on it and it never triggers my Broadcast receiver.Here what I have tried:I created a custom receiver:public class NotificationOnClickReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { Log.d(Constants.Engine.LOGGER_TAG_GCM, NotificationOnClickReceiver.class.toString() + " triggered"); }}I registered this receiver dynamically on my global Application (not activity)mNotificationOnClickReceiver = new NotificationOnClickReceiver();IntentFilter intentFilterOnClick = new IntentFilter(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);getApplicationContext().registerReceiver(mNotificationOnClickReceiver, intentFilterOnClick);Log.e(Constants.Engine.LOGGER_TAG_DBG, "mNotificationOnClickReceiver = " + mNotificationOnClickReceiver.toString());The filter is just a string constant (I wonder if this is the problem) public static final String BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK = "push_notification.onclick";Receiver intent setup:// When the user taps on the notification bar, this is the receiver that I want to be calledIntent pushNotificationIntent;Log.e(Constants.Engine.LOGGER_TAG_DBG, "Notification called!");pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);// Not sure if this causing the problempushNotificationIntent.setAction(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);// ensures that each notification and intent are uniqueint uniqueCode = new Random().nextInt(Integer.MAX_VALUE);PendingIntent pendingIntent = PendingIntent.getBroadcast( context, uniqueCode, pushNotificationIntent, // Receiver Intent PendingIntent.FLAG_CANCEL_CURRENT);Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) .setSmallIcon(mIconId) .setContentTitle(context.getString(R.string.myString)) .setContentText("My GCM Alert!") .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent);From the logs, things seem to work except that my Broadcast receiver never gets called:02-22 15:47:47.092 16863-16863/com.myapp.test E/MYAPP_DBG: mNotificationOnClickReceiver = mytest.broadcastreceivers.NotificationOnClickReceiver@d78f55902-22 15:47:53.312 16863-17038/com.myapp.test E/MYAPP_DBG: Notification called!If anyone has this working, can you point me out a sample code? I don't see where my bug is.thank you! 解决方案 This would appear to be the problem:pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);You're actually creating an explicit Intent for the broadcast PendingIntent. Explicit Intents do not work with dynamically registered Receivers, as they target a Receiver class, rather than an instance.You'll need to use an implicit Intent instead, which you can do by simply instantiating the Intent with the action String, rather than a Context and Class. For example:pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);Please note that if your app's process is killed before the Notification is clicked, that dynamically registered Receiver instance will die, too, and the Notification's broadcast won't really do anything.Depending on the desired behavior, it might be preferable to instead statically register your Receiver class in the manifest, and stay with the explicit Intent. Doing this will allow your app to receive the Notification broadcast even if its process has been killed.To do that, you'd just need to add a <receiver> element for your NotificationOnClickReceiver in the manifest.<receiver android:name="NotificationOnClickReceiver" />Though you can still set an action on that broadcast Intent, you don't need an <intent-filter> for it here, as the explicit Intent is directly targeting the class anyway. If the Receiver is only for that one function, you don't necessarily need the action, and can just remove that altogether.You can then remove the code you have in step 2, as you'd no longer need the dynamically registered instance. 这篇关于单击android通知图标不会触发广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!