Android - 包含 click_action 后未收到 FCM 推送通知

Posted

技术标签:

【中文标题】Android - 包含 click_action 后未收到 FCM 推送通知【英文标题】:Android - FCM Push Notification not received after click_action is included 【发布时间】:2018-03-16 05:38:30 【问题描述】:

我正在开发一个 android 应用程序,我希望在应用程序状态同时处于前台和后台时接收推送通知。

在添加 click_action 之前它工作正常,但在添加它之后,当应用程序处于后台或被终止时,它不会收到推送通知。经过一番研究,我可以理解如果 FCM 是“通知”消息类型而只有“数据”消息类型,我将无法在后台接收推送。

由于FCM默认提供click_action属性,并且在onMessageReceived()中也提供了getClickAction()的方法来获取它,我真的应该在数据消息中使用click_action吗?

onMessageReceived 中的捆绑包

Bundle[
    google.sent_time = 1521177008895,
    google.ttl = 3600,
    gcm.notification.e = 1,
    lastName = Test,
    profileUrl = ,
    roleId = ,
    userId = 140128,
    gcm.notification.badge = 1,
    gcm.notification.sound =
    default,
    gcm.notification.title = Someone
    try to login with your credentials,
    roleName = ,
    userName = test,
    flag = 0,
    from = 612005318045,
    type = 0,
    gcm.notification.sound2 = simpleSound,
    firstName = Test,
    gcm.notification.notification_id = 1140,
    google.message_id = 0: 1521177008900292 % c05b1316c05b1316,
    notification = Someone
    try to login with your credentials,
    gcm.notification.body = Someone
    try to login with your credentials,
    gcm.notification.icon = myApp,
    notificationId = 2047669,
    gcm.notification.notification_type = 1,
    gcm.notification.click_action = com.my.push.activities.OPEN_NOTIFICATION_LIST,
    gcm.notification.notification_message = TEST MESSAGE,
    notificationDate = Fri Mar 16 05: 10: 08 GMT 2018,
    collapse_key = com.my.push,
    gcm.notification.notification_title = APP
]

onMessageReceived中处理方式的代码sn-p

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) 
        ......
        ......
        showNotification(remoteMessage);
    



public void showNotification(RemoteMessage remoteMessage) 

        try 
            Map<String, String> response = remoteMessage.getData();

            Intent intent = prepareIntent(remoteMessage);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0,
                    intent, PendingIntent.FLAG_ONE_SHOT);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "D4E_ANDROID")
                    .setContentTitle("New Notification")
                    .setContentText(response.get("notification"))
                    .setSmallIcon(R.drawable.d4e_logo)
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .addAction(R.drawable.view_icon, "View", pIntent);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(Integer.parseInt(response.get("notificationId")), builder.build());
         catch (Exception exception) 
            Log.e("OnREC", exception.toString());
        

    


 public Intent prepareIntent(RemoteMessage remoteMessage) 
        Map<String, String> response = remoteMessage.getData();
        Intent intent;
        boolean isAppInBackground;
        if (SessionContext.isLoggedIn()) 
          isAppInBackground = SessionHelper.initializeSessionHelper().isAppInBackground(this);
                Log.e("LOGGGGG", isAppInBackground + "");
                if (isAppInBackground) 
                    intent = new Intent(this, SplashScreen.class);
                 else 
                    intent = new Intent(remoteMessage.getNotification().getClickAction());
                
            
         else 
            intent = new Intent(this, LoginActivity.class);
        


        return intent;
    

请任何人指导我找到解决方案。

【问题讨论】:

你的后端有问题..你的 android 部分看起来很不错 @Santanu 你能根据我分享的捆绑包指导我吗?在后端需要改变什么? 【参考方案1】:

如果您可以接收到通过控制台发送的 FCM 通知,那么构建通知可能有问题。一个怀疑是您没有使用“图标”字段。 如果您可以调试 android 设备,请在此处设置断点并查看是否出现错误或错过了某些资源。 https://github.com/evollu/react-native-fcm/blob/master/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java#L46

【讨论】:

应用在后台时日志也没有打印,但在前台状态下打印 看这个:pluralsight.com/guides/android/… 从 Firebase 通知控制台发送的消息被视为仅通知消息有效负载(除非您添加自定义键值对,否则它将成为通知和数据)。仅通知消息负载行为是当应用程序处于后台时,Android 系统将处理通知。有关该行为的更多详细信息,请参阅 Android 版 firebase.google.com/docs/cloud-messaging/android/…。

以上是关于Android - 包含 click_action 后未收到 FCM 推送通知的主要内容,如果未能解决你的问题,请参考以下文章

Firebase FCM通知click_action有效内容

Firebase click_action无效

收到无效的 JSON 有效负载。未知名称 click_action

FCM 通知 click_action 应重定向到打开浏览器

当click_action被发送到应用程序的先前版本尚未添加意图过滤器的情况下如何处理?

Android GCM 通知后台堆栈