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

如何在应用未激活/已杀死状态下执行远程通知角标设置操作?

在React Native中实现未激活/杀死状态下通知触发角标设置

当然有办法搞定这个需求!针对React Native里应用在未激活甚至被杀死状态下,收到远程通知时设置角标这个场景,得结合iOS和Android的不同机制来实现,我给你拆解具体步骤:

iOS端实现方案

iOS在应用后台或被杀死状态下,JS代码是不会运行的,所以得借助**原生的通知服务扩展(Notification Service Extension)**来处理通知并设置角标:

  • 首先在Xcode中添加通知服务扩展:打开你的RN项目的iOS工程,点击File > New > Target,选择Notification Service Extension,命名比如BadgeNotificationExtension,完成创建后确保扩展的版本号和主应用一致。
  • 配置远程通知payload:发送的远程通知必须包含mutable-content: 1字段,这样系统才会触发我们创建的扩展来处理通知内容。
  • 在扩展的NotificationService.m(Swift项目则是NotificationService.swift)中编写角标设置逻辑:
    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
        self.contentHandler = contentHandler;
        self.bestAttemptContent = [request.content mutableCopy];
        
        // 从通知payload中获取角标数字,这里假设字段名为badge
        NSInteger badgeCount = [self.bestAttemptContent.userInfo[@"badge"] integerValue];
        // 设置应用角标
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeCount];
        
        // 传递处理后的通知内容
        self.contentHandler(self.bestAttemptContent);
    }
    
  • 额外配置:在主应用的Info.plist中添加UIBackgroundModes,并包含remote-notification,确保应用能接收后台通知。

Android端实现方案

Android可以通过FCM的后台消息服务来实现,即使应用被杀死,只要设备联网且没有被系统限制,就能触发通知处理逻辑:

  • 确保已经集成了react-native-firebase/messaging模块,并且配置好了FCM相关权限。
  • 自定义Firebase消息服务类:创建一个MyFirebaseMessagingService继承自FirebaseMessagingService,在onMessageReceived方法中调用react-native-shortcut-badger的原生API设置角标:
    public class MyFirebaseMessagingService extends FirebaseMessagingService {
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            
            // 从通知的data字段中获取角标数字
            Integer badgeCount = Integer.parseInt(remoteMessage.getData().get("badge"));
            if (badgeCount != null) {
                // 调用ShortcutBadger设置角标
                ShortcutBadger.applyCount(getApplicationContext(), badgeCount);
            }
        }
        
        @Override
        public void onNewToken(String token) {
            super.onNewToken(token);
            // 这里可以处理设备token的更新逻辑
        }
    }
    
  • 在AndroidManifest.xml中注册这个服务:
    <service
        android:name=".MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    
  • 注意事项:要确保react-native-shortcut-badger已经按照文档配置好依赖和权限,同时如果遇到后台消息不触发的情况,可能需要引导用户关闭应用的省电优化,避免系统限制后台服务。

通用注意点

  • 发送远程通知时,payload中必须携带角标数字字段(比如统一用badge字段),这样两端的原生代码才能获取到对应的值进行设置。
  • iOS端如果只是简单设置角标,也可以直接在远程通知payload中添加badge字段,系统会自动更新角标;但如果需要自定义逻辑(比如累加角标数),就必须用通知服务扩展。

内容的提问来源于stack exchange,提问作者Manzoor Samad

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:23:21