6

I need help. Firebase Notifications is Not Working in Background. This is My Code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "FROM:" + remoteMessage.getFrom());
   sharedPreference = getSharedPreferences(Global.SECURETRADE, 0);
    UID = sharedPreference.getString(Global.ID, "");


        Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)`enter code here`;

        NotificationCompat.Builder notificationBuilder = new
                NotificationCompat.Builder(this);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            notificationBuilder.setSmallIcon(R.mipmap.small_secure_trade_app_icon);

        } else {

            notificationBuilder.setSmallIcon(R.drawable.small_secure_trade_app_icon);
        }

        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.securetrade_icon));
        notificationBuilder.setContentTitle(remoteMessage.getData().get("title"));
        notificationBuilder.setContentText(remoteMessage.getData().get("body"));
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSound(defaultSoundUri);
        notificationBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

    }




}
AL.
  • 36,815
  • 10
  • 142
  • 281
Krishna
  • 61
  • 1
  • 3
  • How are you sending the message? – AL. Jun 20 '17 at 04:37
  • I am also wondering the same, before it was working properly – Raut Darpan Jun 20 '17 at 04:53
  • I am also wondering why it is not working, Firebase is given lots of errors, It was wokring perviously – Raut Darpan Jun 20 '17 at 04:54
  • Can u post your manifest – Anil Jun 20 '17 at 05:14
  • "onMessageReceived" will not get triggered when app is in background.You have to handle it like this https://stackoverflow.com/a/37395785/3111083. Or try this https://stackoverflow.com/a/43869272/3111083 (not tried it). – Sunil Sunny Jun 20 '17 at 05:22
  • Try My solution?
    I've already tried it.
    And it works
    https://stackoverflow.com/questions/48897883/how-to-handle-notifications-with-fcm-when-app-is-in-either-foreground-or-backgro/48899186#48899186
    – Leon Chang Feb 21 '18 at 07:06

5 Answers5

2

when app is in background or killed you have to use data payload for notification. Firebase onMessageReceived not called when app in background

AAA
  • 485
  • 6
  • 23
1

Just remove 'notification' section from the json you sent through push notification. Simply sent the 'data' section, onMessageReceived will work as normal

0

Yes, Firebase will not call the onMessageReceived() when the app is in background unless you make the notification request body changes from yoour server code.

Checkout this answer https://stackoverflow.com/a/40083727/4620609

Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
0

Are you sending data-messages (not notification-messages) ?

notification-messages don't call onMessageReceived()

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

Read more here: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

PS: FCM Web Console always sends notification-messages


If you are sending a data-messages, and onMessageReceived() is not called...
then it's a different problem.

It might even be a problem of that specific device.
See Push notifications using FCM not received when app is killed android

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
-1

when app running in background onMessageReceive will not work

offline message coming to launcher activity just copy this peace of code in your launcher activity and check it. it will work.

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        Logger.info(TAG, "FIRE BASE OFF LINE NOTIFICATIONS COMING TO THIS BLOCK--->");

        JSONObject json = new JSONObject();
        Set<String> keys = bundle.keySet();
        for (String key : keys) {
            Logger.info(TAG, "json object--->" + key + "---values--" + JSONObject.wrap(bundle.get(key)));
        }
    }

and your payload should be like this

{ "notification": {
    "title": "Your Title",
    "text": "Your Text",
     "click_action": "OPEN_ACTIVITY_1" // should match to your intent filter
  },
    "data": {
    "keyname": "any value " //you can get this data as extras in your activity and this data is optional
    },
  "to" : "to_id(firebase refreshedToken)"
}

reference

NareshRavva
  • 823
  • 3
  • 21
  • 50