You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

应用被杀时如何撤销单条/全部通知?React Native Firebase求助

Android端实现应用被杀时撤销通知&重置角标方案

核心逻辑

应用被杀状态下,仅能通过FCM数据消息触发后台逻辑(通知消息由系统直接弹窗,无法拦截修改),结合Android原生通知管理API和厂商私有角标接口完成需求。

具体实现步骤

1. 配置FCM数据消息接收

自定义FirebaseMessagingService子类,重写onMessageReceived方法——数据消息即使在应用被杀时也会触发该方法(需适配Android 12+后台限制):

public class CustomMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // 识别网页端已读通知的指令
        if ("mark_notification_read".equals(remoteMessage.getData().get("action"))) {
            String notificationId = remoteMessage.getData().get("notification_id");
            // 执行撤销通知+重置角标
            cancelTargetNotification(Integer.parseInt(notificationId));
            resetAppBadge();
        }
    }
}

在AndroidManifest.xml中注册该服务:

<service
    android:name=".CustomMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

2. 撤销指定通知

通过NotificationManager根据通知ID精准撤销:

private void cancelTargetNotification(int notificationId) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(notificationId);
    // 若需清空所有通知,调用notificationManager.cancelAll();
}

关键前提:推送通知时必须给每条通知分配唯一ID,且网页端已读时需将该ID同步传给移动端。

3. 重置应用角标

Android无统一系统角标API,需针对主流厂商分别适配:

  • 小米:
private void resetXiaomiBadge() {
    try {
        Intent updateIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
        updateIntent.putExtra("android.intent.extra.update_application_component", getPackageName() + "/" + getPackageName() + ".MainActivity");
        updateIntent.putExtra("android.intent.extra.update_application_message_count", 0);
        sendBroadcast(updateIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • 华为:
private void resetHuaweiBadge() {
    try {
        Bundle badgeBundle = new Bundle();
        badgeBundle.putString("package", getPackageName());
        badgeBundle.putString("class", getPackageName() + ".MainActivity");
        badgeBundle.putInt("badgenumber", 0);
        getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, badgeBundle);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • OPPO:
private void resetOppoBadge() {
    try {
        Intent badgeIntent = new Intent("com.oppo.unsettledevent");
        badgeIntent.putExtra("pakeageName", getPackageName());
        badgeIntent.putExtra("number", 0);
        sendBroadcast(badgeIntent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

可通过判断设备品牌调用对应方法,避免无效执行。

4. 适配Android权限与后台限制

  • Android 12+需在AndroidManifest.xml中声明通知权限:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  • 若需处理长期后台任务,可结合WorkManager确保逻辑稳定性,但仅处理FCM消息时onMessageReceived已足够。

注意事项

  • 必须使用FCM数据消息,通知消息无法触发应用被杀状态下的自定义逻辑。
  • 通知ID需在网页端与移动端保持一致,否则无法精准撤销目标通知。
  • 厂商角标API可能随系统版本更新,需定期测试适配。

内容的提问来源于stack exchange,提问作者Phạm Chỉnh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 01:50:00