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

瘦欲@ 提交于 2019-12-11 07:36:50

问题


I referred to this question to get a device token in order to send push notifications to my app. I created my app using create-react-native-app. Here is the code:

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();

The problem is that the register and the registrationError events never fire. I am prompted to approve permissions and next time the app starts I can use checkPermissions() and confirm that permissions are given. But without the device token, it's impossible to send push notifications to the device. What am I doing wrong?


回答1:


How about the Xcode part?

You should import TCTPushNotification on you AppDelegate file

#import <React/RCTPushNotificationManager.h>

and implement the follow code to enable the notification and register in your app

    // 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];
 }

For more information use the official docs

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

👊




回答2:


The other thing to be aware of is that on the simulator the onRegister function is not fired, you have to use a real device.




回答3:


Just in case anyone had a similar issue to mine, which was on a real device, the key is to register the events, but then specifically call PushNotificationIOS.requestPermissions(); after registering the events.



来源:https://stackoverflow.com/questions/49421887/cant-get-ios-push-notification-device-token-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!