代码之家  ›  专栏  ›  技术社区  ›  Jay RobG

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

  •  2
  • Jay RobG  · 技术社区  · 7 年前

    我提到 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();
    

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

    3 回复  |  直到 7 年前
        1
  •  2
  •   ZedTuX    7 年前

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

        2
  •  1
  •   JackDev    6 年前

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

        3
  •  0
  •   David Schumann Axnyff    7 年前

    Xcode部分怎么样?

    您应该在AppDelegate文件上导入TCTPushNotification

    #import <React/RCTPushNotificationManager.h>
    

    并实现以下代码以启用 notification register 在应用程序中

        // 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

    👊