点击android通知图标不触发广播接收器
Click on android notification icon does not trigger broadcast receiver
我有一个与推送通知挂钩的应用程序。当用户点击通知时,我希望触发广播接收器而不是 activity。
我查看了这些线程,看起来这是完全可能且常见的。
Android Notification Action is not fired (PendingIntent)
但是我无法让它工作。我看到了通知,但是当我点击它时它从未触发我的广播接收器。
这是我尝试过的:
我创建了一个自定义接收器:
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");
}
}
我在我的全局应用程序中动态注册了这个接收器(不是 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());
过滤器只是一个字符串常量(不知道是不是这个问题)
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 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);
从日志来看,一切似乎都正常,只是我的广播接收器从未被调用过:
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!
如果有人有这个工作,你能给我指出一个示例代码吗?我没有看到我的错误在哪里。
谢谢!
要发送广播,请使用此 sendBroadcast(new Intent("NotificationClicked"));
现在要接收广播,请在应用程序标签下的清单中提及此内容
<receiver android:name=".NotificationOnClickReceiver" >
<intent-filter>
<action android:name="NotificationClicked" >
</action>
</intent-filter>
</receiver>
这似乎是问题所在:
pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
您实际上是在为广播 PendingIntent
创建显式 Intent
。显式 Intent
不适用于动态注册的接收器,因为它们针对的是接收器 class,而不是实例。
您需要使用隐式 Intent
,您可以通过使用 String
操作简单地实例化 Intent
而不是 Context
和 Class
。例如:
pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
请注意,如果您的应用进程在单击 Notification
之前被终止,则动态注册的 Receiver 实例也会终止,并且 Notification
的广播不会真正执行任何操作.
根据所需的行为,最好在清单中静态注册您的接收器 class,并保持显式 Intent
。这样做将允许您的应用接收 Notification
广播,即使它的进程已被终止。
为此,您只需在清单中为您的 NotificationOnClickReceiver
添加一个 <receiver>
元素。
<receiver android:name="NotificationOnClickReceiver" />
尽管您仍然可以对该广播 Intent
设置操作,但此处不需要 <intent-filter>
,因为显式 Intent
直接针对 class 无论如何。如果 Receiver 仅用于该功能,则您不一定需要该操作,并且可以将其完全删除。
然后您可以删除步骤 2 中的代码,因为您不再需要动态注册的实例。
我有一个与推送通知挂钩的应用程序。当用户点击通知时,我希望触发广播接收器而不是 activity。
我查看了这些线程,看起来这是完全可能且常见的。
Android Notification Action is not fired (PendingIntent)
但是我无法让它工作。我看到了通知,但是当我点击它时它从未触发我的广播接收器。
这是我尝试过的:
我创建了一个自定义接收器:
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"); } }
我在我的全局应用程序中动态注册了这个接收器(不是 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());
过滤器只是一个字符串常量(不知道是不是这个问题)
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 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);
从日志来看,一切似乎都正常,只是我的广播接收器从未被调用过:
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!
如果有人有这个工作,你能给我指出一个示例代码吗?我没有看到我的错误在哪里。
谢谢!
要发送广播,请使用此 sendBroadcast(new Intent("NotificationClicked"));
现在要接收广播,请在应用程序标签下的清单中提及此内容
<receiver android:name=".NotificationOnClickReceiver" >
<intent-filter>
<action android:name="NotificationClicked" >
</action>
</intent-filter>
</receiver>
这似乎是问题所在:
pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
您实际上是在为广播 PendingIntent
创建显式 Intent
。显式 Intent
不适用于动态注册的接收器,因为它们针对的是接收器 class,而不是实例。
您需要使用隐式 Intent
,您可以通过使用 String
操作简单地实例化 Intent
而不是 Context
和 Class
。例如:
pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
请注意,如果您的应用进程在单击 Notification
之前被终止,则动态注册的 Receiver 实例也会终止,并且 Notification
的广播不会真正执行任何操作.
根据所需的行为,最好在清单中静态注册您的接收器 class,并保持显式 Intent
。这样做将允许您的应用接收 Notification
广播,即使它的进程已被终止。
为此,您只需在清单中为您的 NotificationOnClickReceiver
添加一个 <receiver>
元素。
<receiver android:name="NotificationOnClickReceiver" />
尽管您仍然可以对该广播 Intent
设置操作,但此处不需要 <intent-filter>
,因为显式 Intent
直接针对 class 无论如何。如果 Receiver 仅用于该功能,则您不一定需要该操作,并且可以将其完全删除。
然后您可以删除步骤 2 中的代码,因为您不再需要动态注册的实例。