我有一个与推送通知挂钩的应用程序.当用户单击通知时,我希望触发广播接收器而不是活动.
我看了看这些线程,看起来这是完全可行和普遍的.
Android Notification Action is not fired (PendingIntent)
Send broadcast on notification click
但是我无法正常工作.我看到了通知,但是当我单击它时,它永远不会触发我的广播接收器.
这是我尝试过的:
>我创建了一个自定义接收器:
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.显式意图不适用于动态注册的Receiver,因为它们针对的是Receiver类,而不是实例.
您将需要使用隐式Intent,您可以通过简单地使用 *** 作String(而不是Context和Class)实例化Intent来实现.例如:
pushNotificationIntent = new Intent(Constants.Engine.broADCAST_RECEIVER_FILTER_NOTIFICATION_ONCliCK);
请注意,如果您的应用程序进程在单击通知之前被杀死,那么动态注册的Receiver实例也将死亡,并且Notification的广播实际上不会做任何事情.
根据所需的行为,最好在清单中静态注册Receiver类,并保留显式的Intent.这样一来,即使您的应用已被终止,它也可以接收通知广播.
为此,您只需要添加< receiver>清单中NotificationOnClickReceiver的元素.
<receiver androID:name="NotificationOnClickReceiver" />
尽管您仍然可以在该广播Intent上设置 *** 作,但是不需要< intent-filter>为此,因为无论如何显式Intent都直接针对该类.如果Receiver仅用于该功能,则您不必执行该 *** 作,而可以将其完全删除.
然后,您可以删除步骤2中的代码,因为您不再需要动态注册的实例.
总结以上是内存溢出为你收集整理的单击android通知图标不会触发广播接收器全部内容,希望文章能够帮你解决单击android通知图标不会触发广播接收器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)