Android FCM badge didn't working in background

1

I want to update badge in background and I use notification style, but it didn't work. I don't want to use data style because only one notification appeared. Please help I want check push in back ground and update badge

public void zzm(Intent intent) {

    Set<String> keys = intent.getExtras().keySet();
    for (String key : keys) {
        try {

            if (key.equals("badge")) {
                String cnt = intent.getExtras().get(key).toString();
                int badgeCount = Integer.valueOf(cnt);
                ShortcutBadger.applyCount(this, badgeCount);

            }
        } catch (Exception e) {
            Log.i("uniqbadge", "zzm Custom_FirebaseMessagingService" + e.getMessage());
        }
    }

    super.zzm(intent);
}

//this is received code

  public void onMessageReceived(RemoteMessage remoteMessage) {

        showNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));

        set_alarm_badge();
        }

//my notification setting

  private void showNotification(String title, String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

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

    }

// this is my badge update

public void set_alarm_badge(){
    Context context=getApplicationContext();
    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");


    MainActivity.badge_count++;
    intent.putExtra("badge_count", MainActivity.badge_count);
    intent.putExtra("badge_count_package_name",getApplicationContext().getPackageName());
    intent.putExtra("badge_count_class_name",MainActivity.class.getName());

        if(Build.VERSION.SDK_INT> Build.VERSION_CODES.GINGERBREAD_MR1) {
        intent.setFlags(0x00000020);
    }
    if (canResolveBroadcast(getApplicationContext(), intent)) {
        Log.d("TAG", "hi ");
        getApplicationContext().sendBroadcast(intent);

    }
}
android
firebase
push-notification
firebase-cloud-messaging
badge
asked on Stack Overflow Aug 22, 2017 by Jinsuk Oh • edited Aug 22, 2017 by KENdi

1 Answer

2
@Override
public void handleIntent(Intent intent) {
    Bundle bundle = intent.getExtras();
    // get your badge key from bundle
    ShortcutBadger.applyCount(this, "Your Badge Count");
    sendNotification(intent);
}

Just override this method in FirebaseMessagingService and it will work even the app is in foreground, background or killed. No need of extra service or alarm it is method from FCM.

answered on Stack Overflow Nov 22, 2017 by Awais

User contributions licensed under CC BY-SA 3.0