【问题标题】:Android FCM badge didn't working in backgroundAndroid FCM 徽章在后台不起作用
【发布时间】:2017-08-22 08:07:37
【问题描述】:

我想在后台更新徽章并使用通知样式,但它不起作用。我不想使用数据样式,因为只出现了一个通知。 请帮助我要检查后台推送并更新徽章

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

    }
}

【问题讨论】:

    标签: android firebase push-notification firebase-cloud-messaging badge


    【解决方案1】:
    @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 的方法。

    【讨论】:

    • 使用 firebase-messaging 17.3.4 我得到错误:方法没有覆盖或实现超类型中的方法
    • onMessageReceived(RemoteMessage remoteMessage) 将在后台运行 17 年,但您必须修改通知并在通知中发送“数据”而不是 google fcm 文档中提到的“通知”。
    • 是的,我覆盖了 onMessageReceived 现在可以工作,但我还发现你必须将它添加到你的清单中:
    • 显然您必须在清单中提供服务。太好了,你明白了:)
    • 你拯救了我的一天
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2020-03-13
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多