Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
点击android通知图标不会触发广播接收器_Android_Android Intent_Push Notification_Notifications_Android Pendingintent - Fatal编程技术网

点击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
    。Explicit
    Intent
    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" />