Rakesh L
Rakesh L

Reputation: 1168

Android - FCM Push Notification not received after click_action is included

I am developing an Android application in which I would like to receive a Push notification when application state is both in Foreground and Background.

It was working fine before the click_action is added but after adding it does not make the push notification received when the application is background or killed. After some research, I could understand that I will not be able to receive the Push in Background if the FCM is "notification" message type but only "data" message type.

As FCM provides click_action attribute by default and also provides the method getClickAction() to get it in onMessageReceived(), Should I really use click_action in data message ?

The bundle in 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
}]

The code snippet of the way it is handled in 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;
    }

Please anyone guide me to find the solution.

Upvotes: 0

Views: 713

Answers (1)

Asteroid
Asteroid

Reputation: 758

if you can receive FCM notification sent through console, then potentially something is wrong with build the notification. one suspect is that you are not using "icon" field. If you can debug android device, set breakpoint here and see if you get error or missed some resource. https://github.com/evollu/react-native-fcm/blob/master/android/src/main/java/com/evollu/react/fcm/SendNotificationTask.java#L46

Upvotes: 0

Related Questions