点击android通知图标不会触发广播接收器

时间:2017-02-23 00:18:19

标签: android android-intent push-notification notifications android-pendingintent

我有一个与推送通知挂钩的应用。当用户点击通知时,我希望触发广播接收器而不是活动。

我看了这些线程,看起来这是完全可能和常见的。

Android Notification Action is not fired (PendingIntent)

Send broadcast on notification click

但是我无法让它发挥作用。我看到了通知,但是当我点击它时它从未触发我的广播接收器。

我在这里尝试过:

  1. 我创建了一个自定义接收器:

    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");
        }
    }
    
  2. 我在全局应用程序(非活动)上动态注册了此接收器

    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());
    
  3. 过滤器只是一个字符串常量(我想知道这是不是问题)

        public static final String BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK = "push_notification.onclick";
    
    1. 接收方意图设置:

      // When the user taps on the notification bar, this is the receiver that I want to be called
      Intent pushNotificationIntent;
      Log.e(Constants.Engine.LOGGER_TAG_DBG, "Notification called!");
      pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
      // Not sure if this causing the problem
      pushNotificationIntent.setAction(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
      // ensures that each notification and intent are unique
      int 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);
      
    2. 从日志中看,除了我的广播接收器永远不会被调用之外,事情似乎有效:

      02-22 15:47:47.092 16863-16863/com.myapp.test E/MYAPP_DBG: mNotificationOnClickReceiver = mytest.broadcastreceivers.NotificationOnClickReceiver@d78f559
      02-22 15:47:53.312 16863-17038/com.myapp.test E/MYAPP_DBG: Notification called!
      
    3. 如果有人这样做,你能指出一个示例代码吗?我不知道我的错误在哪里。

      谢谢你!

2 个答案:

答案 0 :(得分:8)

这似乎是问题所在:

pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);

您实际上正在为广播Intent创建明确的PendingIntent。显式Intent不适用于动态注册的Receiver,因为它们的目标是Receiver类,而不是实例。

您需要使用隐式Intent,只需使用操作Intent实例化String,而不是Context即可。和Class。例如:

pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);

请注意,如果您的应用程序的进程在单击Notification之前被终止,那么该动态注册的Receiver实例也会死亡,并且Notification的广播赢了&真的做了什么。

根据所需的行为,最好是在清单中静态注册Receiver类,并使用显式Intent。这样做将允许您的应用即使其进程被杀死也会收到Notification广播。

为此,您只需在清单中为<receiver>添加NotificationOnClickReceiver元素。

<receiver android:name="NotificationOnClickReceiver" />

虽然您仍然可以对该广播Intent设置操作,但您在此处不需要<intent-filter>,因为明确的Intent无论如何直接针对该广告。如果Receiver仅用于该功能,则您不一定需要该操作,并且可以完全删除它。

然后,您可以删除步骤2中的代码,因为您不再需要动态注册的实例。

答案 1 :(得分:1)

要发送广播,请使用此sendBroadcast(new Intent("NotificationClicked"));

现在收到广播,请在你的manifestunder应用程序标签中提及

<receiver android:name=".NotificationOnClickReceiver" >
            <intent-filter>
                <action android:name="NotificationClicked" >
                </action>
            </intent-filter>
        </receiver>