On Android badge the badge number on the app icon when the app is in the background or killed when receiving notifications like facebook app

I am developing an android and ios app using react native. I want to show the badge number on the app icon when I receive remote notifications. I am using a third party library and the iOS icon works great. In android I can only show the icon number when the app is in the foreground. When the app is killed or in the background, I cannot show the icon numbers. I know Android doesn't support icon display natively, but I've seen facebook and messenger apps display icon on Android. Please can someone tell me how to do this on Android even the app is killed or in the background. Thanks in advance.

+3


source to share


1 answer


onMessageReceived does not receive a call, it only causes the data payload to be sent.

If no data and payload are sent as well, onMessageReceived is not called.



Use below code to get icon from server when app is in background or killed because FirebaseMessagingService is running.

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

    @Override
    public void zzm(Intent intent) {
        Log.i("uniqbadge", "zzm");
        Set<String> keys = intent.getExtras().keySet();
        for (String key : keys) {
            try {
                 Log.i("uniq", " " + key + " " + intent.getExtras().get(key));
                if (key.equals("badge")) {
                    String cnt = intent.getExtras().get(key).toString();
                    int badgeCount = Integer.valueOf(cnt);
                    Log.i("uniq", " badge count " + badgeCount);
                    ShortcutBadger.applyCount(this, badgeCount);
                    Log.i("uniq", " " + "end");
                }
            } catch (Exception e) {
                Log.i("uniqbadge", "zzm Custom_FirebaseMessagingService" + e.getMessage());
            }
        }

        super.zzm(intent);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        if (remoteMessage.getNotification() != null) {
            Log.i(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());

        }
        if (remoteMessage.getData().size() > 0) {
            Log.i(TAG, "Data Payload: " + remoteMessage.getData().toString());
            ...

      

+2


source







All Articles