Android FCM徽章并没有在后台运行

时间:2017-08-22 08:07:38

标签: android firebase push-notification firebase-cloud-messaging 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);
}

//这是收到的代码

  public void onMessageReceived(RemoteMessage remoteMessage) {

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

        set_alarm_badge();
        }

//我的通知设置

  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());

    }

//这是我的徽章更新

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);

    }
}

1 个答案:

答案 0 :(得分: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);
}

只需在FirebaseMessagingService中覆盖此方法,即使应用程序处于前台,后台或已杀死,它也能正常工作。不需要额外的服务或警报,这是来自FCM的方法。