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

iOS端react-native-notifications无法获取Device Token问题排查

解决react-native-notifications无法获取iOS Device Token的问题

以下是针对问题的排查和解决步骤:

1. 修复useEffect执行逻辑

你的useEffect未添加依赖数组,会在组件每次渲染时重复执行,可能导致通知注册逻辑混乱或事件监听被覆盖。修改代码确保仅在组件挂载时执行一次,并添加订阅清理逻辑避免内存泄漏:

useEffect(() => {
  Notifications.registerRemoteNotifications();

  const subscription = Notifications.events().registerRemoteNotificationsRegistered(event => {
    console.log('DEVICE TOKEN:', event.deviceToken);
    alert('DEVICE TOKEN: ' + event.deviceToken);
  });

  // 组件卸载时取消事件订阅
  return () => {
    subscription.remove();
  };
}, []); // 空依赖数组限制执行时机

2. 验证iOS项目配置

  • 打开Xcode,进入项目Signing & Capabilities标签,确认已添加Push Notifications能力。
  • 同样在Signing & Capabilities中,开启Background Modes并勾选Remote notifications选项(部分场景下获取Device Token依赖此配置)。

3. 检查AppDelegate回调实现

react-native-notifications需要原生端转发系统回调才能触发JS层事件,确保AppDelegate中添加以下代码:

Objective-C版本(AppDelegate.m)

#import <RNNotifications/RNNotifications.h>

// 注册成功获取Device Token的回调
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RNNotifications didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

// 注册失败的回调(用于排查问题)
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
  NSLog(@"推送注册失败: %@", error.localizedDescription);
  [RNNotifications didFailToRegisterForRemoteNotificationsWithError:error];
}

Swift版本(AppDelegate.swift)

import RNNotifications

// 注册成功获取Device Token的回调
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  RNNotifications.didRegisterForRemoteNotifications(withDeviceToken: deviceToken)
}

// 注册失败的回调(用于排查问题)
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  print("推送注册失败: \(error.localizedDescription)")
  RNNotifications.didFailToRegisterForRemoteNotifications(with: error)
}

4. 确认通知权限状态

即使弹窗正常弹出,也需确认用户是否允许了通知权限。可在注册前主动检查并请求权限:

useEffect(() => {
  const checkAndRegister = async () => {
    let status = await Notifications.getPermissionsStatus();
    if (status !== 'granted') {
      status = await Notifications.requestPermissions();
    }
    if (status === 'granted') {
      Notifications.registerRemoteNotifications();
    }
  };

  checkAndRegister();

  const subscription = Notifications.events().registerRemoteNotificationsRegistered(event => {
    console.log('DEVICE TOKEN:', event.deviceToken);
    alert('DEVICE TOKEN: ' + event.deviceToken);
  });

  return () => subscription.remove();
}, []);

5. 查看Xcode系统日志

RN的console.log在真机上可能无法直接显示,建议连接设备到Xcode,查看Console面板的系统日志,搜索RNNotifications或deviceToken关键词,获取注册失败的具体原因。

6. 检查版本兼容性

确认react-native-notifications版本与你的React Native版本适配,若存在版本不兼容问题,尝试升级或降级到匹配的版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:57:37