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

React Native iOS应用后台接收通知时角标不更新问题求助

解决React Native iOS后台推送角标始终显示1的问题

问题根源

你的AppDelegate存在两个核心问题:

  • didFinishLaunchingWithOptions中设置UNUserNotificationCenter代理的代码被注释,且原本位置在return语句之后,永远不会执行,导致后台通知的处理逻辑无法正常触发;
  • 后台收到推送时,没有根据推送payload正确更新角标计数。

修改后的AppDelegate.m代码

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <Firebase.h>
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import <React/RCTLog.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  self.moduleName = @"Bisly";
  self.initialProps = @{};

  [FIRApp configure];
  RCTLog(@"Some information3");
  NSLog(@"Some information4");

  // 关键:设置UNUserNotificationCenter代理,必须放在return之前
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  center.delegate = self;

  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
  return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
  return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
 [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  RCTLog(@"Some information");
  NSLog(@"Some information2");
  
  // 从推送payload中获取badge值并更新应用角标
  NSNumber *badgeCount = userInfo[@"aps"][@"badge"];
  if (badgeCount) {
    application.applicationIconBadgeNumber = badgeCount.integerValue;
  }
  
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
 [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  RCTLog(@"Some information1");
  // 用户点击通知后重置角标为0(可根据业务需求调整)
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  completionHandler();
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}

- (BOOL)concurrentRootEnabled
{
  return true;
}

@end

额外注意事项

  • 确保推送服务器发送的payload包含badge字段,示例格式:
    {
      "aps": {
        "alert": "新通知提醒",
        "badge": 3,
        "sound": "default"
      }
    }
    
  • 若需在RN代码中手动管理角标,可调用RNCPushNotificationIOS.setApplicationIconBadgeNumber(count),但后台推送场景下仍需依赖AppDelegate的处理逻辑。

内容的提问来源于stack exchange,提问作者Tanmoy Sarker

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:13:26