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?
addEventListerinstead ofaddEventListenerinregister. So attention to copy paste from this!!