点击android通知图标不会触发广播接收器
我有一个与推送通知挂钩的应用程序。当用户单击通知时,我希望触发广播接收器而不是活动 我看了这些线,看起来这是完全可能的,也是很常见的 但是我不能让它工作。我看到了通知,但当我点击它时,它从未触发我的广播接收器 以下是我尝试过的:点击android通知图标不会触发广播接收器,android,android-intent,push-notification,notifications,android-pendingintent,Android,Android Intent,Push Notification,Notifications,Android Pendingintent,我有一个与推送通知挂钩的应用程序。当用户单击通知时,我希望触发广播接收器而不是活动 我看了这些线,看起来这是完全可能的,也是很常见的 但是我不能让它工作。我看到了通知,但当我点击它时,它从未触发我的广播接收器 以下是我尝试过的: 我创建了一个自定义接收器: public class NotificationOnClickReceiver extends BroadcastReceiver { @Override public void onReceive(final Cont
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");
}
}
// 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(新意图(“通知已单击”)代码>
现在要接收广播,请在应用程序标记下的manifestunder中提及这一点
<receiver android:name=".NotificationOnClickReceiver" >
<intent-filter>
<action android:name="NotificationClicked" >
</action>
</intent-filter>
</receiver>
这似乎就是问题所在:
pushNotificationIntent = new Intent(this, NotificationOnClickReceiver.class);
实际上,您正在为广播pendingent
创建一个明确的Intent
。ExplicitIntent
s不适用于动态注册的接收方,因为它们的目标是接收方类,而不是实例
您需要使用隐式的Intent
,只需使用action字符串来实例化Intent
,而不是上下文和类即可。例如:
pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
请注意,如果您的应用程序进程在单击通知
之前被终止,则动态注册的接收器实例也将死亡,通知
的广播实际上不会起任何作用
根据所需的行为,最好在清单中静态注册接收方类,并保持显式的Intent
。这样做将允许您的应用程序接收通知
广播,即使其进程已被终止
要做到这一点,您只需要在清单中为NotificationOnClickReceiver
添加
元素
<receiver android:name="NotificationOnClickReceiver" />
尽管您仍然可以在该广播上设置操作Intent
,但此处不需要
,因为显式Intent
直接针对类。如果接收器仅用于该功能,则不一定需要该操作,只需将其全部删除即可
然后可以删除步骤2中的代码,因为不再需要动态注册的实例
pushNotificationIntent = new Intent(Constants.Engine.BROADCAST_RECEIVER_FILTER_NOTIFICATION_ONCLICK);
<receiver android:name="NotificationOnClickReceiver" />