在点击通知时发送广播但是我无法让它工作.我看到了通知,但是当我点击它时,它永远不会触发我的广播接收器.这是我尝试过的:我创建了一个自定义接收器:public class NotificationOnClickReceiver extends BroadcastReceiver {@覆盖public void onReceive(最终上下文上下文,意图意图){Log.d(Constants.Engine.LOGGER_TAG_GCM,NotificationOnClickReceiver.class.toString() + "触发");}}我在我的全局应用程序(不是活动)上动态注册了这个接收器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";接收方意图设置://当用户点击通知栏时,这是我想被调用的接收者意图 pushNotificationIntent;Log.e(Constants.Engine.LOGGER_TAG_DBG, "通知被调用!");pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);//不确定这是否导致问题pushNotificationIntent.setAction(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);//确保每个通知和意图都是唯一的int uniqueCode = new Random().nextInt(Integer.MAX_VALUE);PendingIntent pendingIntent = PendingIntent.getBroadcast(语境,唯一代码,pushNotificationIntent,//接收者意图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("我的 GCM 警报!").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:通知被调用!如果有人有这个工作,你能指出我的示例代码吗?我看不到我的错误在哪里.谢谢! 解决方案 这似乎是问题所在:pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);您实际上是在为广播 PendingIntent 创建一个显式的 Intent.显式 Intent 不适用于动态注册的 Receiver,因为它们针对的是 Receiver 类,而不是实例.您需要使用隐式 Intent 代替,您可以通过使用操作 String 简单地实例化 Intent 来实现,而不是而不是 Context 和 Class.例如:pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);请注意,如果您的应用程序的进程在点击 Notification 之前被终止,那么动态注册的 Receiver 实例也会死亡,并且 Notification 的广播不会'真的什么都不做.根据所需的行为,最好在清单中静态注册您的 Receiver 类,并保持显式的 Intent.这样做将允许您的应用接收 Notification 广播,即使其进程已被终止.为此,您只需在清单中为您的 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通知图标不会触发广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!