2

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
  • 1
    Just in case you have addEventLister instead of addEventListener in register. So attention to copy paste from this!! Commented Oct 30, 2019 at 14:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

👊

1 Comment

I had the header import but not the rest. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.