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

Can't get iOS push notification device token in react-native

我参考 获取设备令牌,以便将推送通知发送到我的应用程序。我使用 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() 并确认已授予权限。但是如果没有设备令牌,就不可能向设备发送推送通知。我做错了什么?

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

另一件需要注意的事情是,在模拟器上 onRegister 功能不会启动,您必须使用真实设备。

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