Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 484
Zwe Avatar asked Oct 18 '22 11:10

Zwe


1 Answers

onMessageReceived doesn't get call, it call only data payload send.

If data payload and notification payload both are send than also onMessageReceived not call.

Use below code for get badge from server when app is in background or killed because your 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());
            ...
like image 155
Avinash Avatar answered Oct 21 '22 04:10

Avinash