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

时间:2018-03-16 05:38:31

标签: android firebase firebase-cloud-messaging

我正在开发一个Android应用程序,当应用程序状态同时位于Foreground和Background中时,我希望在其中收到Push通知。

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

由于FCM默认提供click_action属性,并且还提供方法getClickAction()以便在onMessageReceived()中获取该属性,我是否应该在数据消息中真正使用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

中处理方式的代码段
 @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;
    }

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

1 个答案:

答案 0 :(得分:0)

如果您可以接收通过控制台发送的FCM通知,那么构建通知可能会出现问题。一个嫌疑人是你没有使用&#34; icon&#34;领域。 如果你可以调试android设备,在这里设置断点,看看你是否收到错误或错过了一些资源。     https://github.com/evollu/react-native-fcm/blob/master/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java#L46