无法在 react-native 中获取 iOS 推送通知设备令牌

Posted

技术标签:

【中文标题】无法在 react-native 中获取 iOS 推送通知设备令牌【英文标题】:Can't get iOS push notification device token in react-native 【发布时间】:2018-03-22 06:23:56 【问题描述】:

我参考了this question 来获取设备令牌,以便向我的应用发送推送通知。我使用create-react-native-app 创建了我的应用程序。代码如下:

import React,  Component  from 'react';
import 
  Platform,
  StyleSheet,
  AppRegistry,
  Text,
  View,
  PushNotificationios
 from 'react-native';

type Props = ;

export default class Apptitude extends Component<Props> 
  constructor() 
    console.log('registering evt listerner in launchpad')
    PushNotificationIOS.addEventLister('register', (token) => 
      this.setState(
        deviceToken: token
      )
    );
  

  render() 
    return (
      <View>
      </View>
    );
  


PushNotificationIOS.addEventListener('registrationError', (registrationError) => 
  console.lo('was error')
  console.log(reason.message)
  console.log(reason.code)
  console.log(reason.details)
)
// yes I'm aware I've added an event listener in the constructor also. Neither of these callbacks fire
PushNotificationIOS.addEventListener('register', (token) => 
  console.log('this is the token', token);
);
console.log('requesting permissions')
PushNotificationIOS.requestPermissions();

问题是registerregistrationError 事件永远不会触发。系统提示我批准权限,下次启动应用程序时,我可以使用 checkPermissions() 并确认已授予权限。但是没有设备令牌,就不可能向设备发送推送通知。我做错了什么?

【问题讨论】:

以防万一您在register 中有addEventLister 而不是addEventListener。所以注意从这里复制粘贴!! 【参考方案1】:

另外需要注意的是,在模拟器上onRegister函数没有被触发,你必须使用真实的设备。

【讨论】:

【参考方案2】:

以防万一有人在真实设备上遇到与我类似的问题,关键是注册事件,但在注册事件后专门调用PushNotificationIOS.requestPermissions();

【讨论】:

【参考方案3】:

Xcode 部分怎么样?

你应该在你的 AppDelegate 文件中导入 TCTPushNotification

#import <React/RCTPushNotificationManager.h>

并实现以下代码以在您的应用中启用notificationregister

    // Required to register for notifications
 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
 
  [RCTPushNotificationManager didRegisterUserNotificationSettings:notificationSettings];
 
 // Required for the register event.
 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
 
  [RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
 
 // Required for the notification event. You must call the completion handler after handling the remote notification.
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
                                                        fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 
   [RCTPushNotificationManager didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
 
 // Required for the registrationError event.
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
 
  [RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
 
 // Required for the localNotification event.
 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
 
  [RCTPushNotificationManager didReceiveLocalNotification:notification];
 

欲了解更多信息,请使用官方文档

https://facebook.github.io/react-native/docs/pushnotificationios.html

?

【讨论】:

我导入了标题,但没有导入其余部分。谢谢

以上是关于无法在 react-native 中获取 iOS 推送通知设备令牌的主要内容,如果未能解决你的问题,请参考以下文章

zIndex 在 IOS 的 react-native 中无法正常工作

以真实设备为目标时,无法在 IOS 中构建 react-native

react-native:无法使用发布模式访问Android权限

无法在 React-native 中使用 127.0.0.1 从获取请求中获取响应

在 iOS 设备的 appium 中无法从 react-native 中找到内部 <Text> 标记/组件的 testID

获取 ReferenceError:在 react-native ios 上运行 detox 测试时未定义设备