1

Actually i need to show badge in app icon but i don't know how to get badge and why onMessageReceived not call when app is in background or app is not running.

public class Custom_FirebaseMessagingService extends FirebaseMessagingService          {
            private static final String TAG = "FirebaseMsgService";
            String activityName;

            @Override
            public void onMessageReceived(RemoteMessage remoteMessage) {

                if (remoteMessage == null)
                    return;
                if (remoteMessage.getNotification() != null) {
                    Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
                }
                // Check if message contains a data payload.
                if (remoteMessage.getData().size() > 0) {
                    Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString());
                    try{
                        //boolean from_bg = Boolean.parseBoolean(remoteMessage.getData().get("from_bg").toString());
                        Map<String, String> data = remoteMessage.getData();
                        boolean show_fg =  Boolean.parseBoolean(data.get("show_fg"));
                        ..... 
Avinash
  • 365
  • 4
  • 14
  • 1
    if you want to receive message while in background, you have to use payload data, as described in https://firebase.google.com/docs/cloud-messaging/android/receive – Faruk May 24 '17 at 07:46
  • Can you tell which device you are using ? – Krishna Meena May 24 '17 at 07:52
  • Possible duplicate of [How to handle notification when app in background in Firebase](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – ak0692 May 24 '17 at 07:56
  • to be called onMessageReceived all time (app foreground or background), You need to receive "data" payload only (not both "data" & "notification" payload). if You send both "data" & "notification" payload, when app is in the foreground, "onMessageReceived" will be called for that time. – Solaiman Hossain May 24 '17 at 08:35
  • me send both "data" & "notification" payload and onMessageReceived in foreground but not in background, you have any idea about show badge in app icon ? – Avinash May 24 '17 at 08:41
  • I don't know how to get badge. – Avinash May 24 '17 at 08:42

2 Answers2

3

There are two types of FCM

notification Messages: Sending a payload with this message type triggers onMessageReceived() only when your app is in foreground.

data Messages: Sending a payload with only this specific message type triggers onMessageReceived() regardless if your app is in foreground/background.

Reference: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

AL.
  • 36,815
  • 10
  • 142
  • 281
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
  • You misused a terminology. There is no such thing as *Display* messages, it should be `notification` messages. The description is close but not at all accurate. Please modify them to be more precise as to what is shown in the docs or better yet, refer to the [docs](https://firebase.google.com/docs/cloud-messaging/android/receive) completely – AL. May 24 '17 at 07:48
  • @AL. Thanks for your valuable advice. – Ratilal Chopda May 24 '17 at 07:50
  • No problem. Cheers! – AL. May 24 '17 at 07:51
  • @AL have any idea about show badge at app icon like facebook? – Avinash May 24 '17 at 08:43
  • @Avi [No standard way of doing it](https://stackoverflow.com/a/17565479/4625829). – AL. May 24 '17 at 08:49
0

Add service to AndroidManifest.xml. Also make sure token is not expired.

<service android:name=".FirebaseMessagingService">
 <intent-filter>
    <action android:name="com.google.firebase.MESSAGING_EVENT"/>
 </intent-filter>
</service>

Refresh Token if it is expired.

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // If you want to send messages to this application instance or
    // manage this apps subscriptions on the server side, send the
    // Instance ID token to your app server.
    sendRegistrationToServer(refreshedToken);
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53