Device Registration
In this guide we will dive deeper into how you should handle the device registration.
Before you can send notifications to your React Native app you will need to register the device with Notificare. To register for notifications and request a device token you will need to add the following listener in your index.ios.js file:
this.eventEmitter.addListener('ready', (data) => {
Notificare.registerForNotifications();
});
The ready event signals your application when the Notificare library has loaded its initial configuration and is ready to be used. Only after that, you should request a device identifier from APNS, by calling registerForNotifications.
Whenever you invoke the registerForNotifications method, our library will request a device registration with APNS which in turn will prompt the user with the permission to send alerts, sounds and badge dialog. This will then trigger the didReceiveDeviceToken event, as shown below:
this.eventEmitter.addListener('didReceiveDeviceToken',(data) => {
Notificare.registerDevice(data.device, null, null, (error, data) => {
});
});
Because these tokens can change at any time you need to make sure the device registration happens every time your component mounts. The library will automatically discern if the registration should be skipped, in case nothing changes, or actually complete the registration in case a new device token is assigned.
It is also possible to get the notification authorization status by using the following method:
Notificare.fetchNotificationSettings((error, response) => {
if (response.granted) {
//User accepted user notifications
}
});
But there will be situations where simply registering an anonymous device is not what you want. For example if you authenticate your users you will not want to register an anonymous device whenever the app launches. When that is the case, you will need to handle this event differently:
this.eventEmitter.addListener('didReceiveDeviceToken', function(e: Event) {
if (MyAuthClass.isLoggedIn) {
Notificare.registerDevice(e.device, MyAuthClass.userID, MyAuthClass.userName, (error, msg) => {
});
} else {
Notificare.registerDevice(e.device, null, null, (error, msg) => {
});
}
});
When you authenticate users, you will also have the need to do this device registration right after they login in your app. This can be done basically from any where in your app using the method below:
Notificare.registerDevice(e.device, MyAuthClass.userID, MyAuthClass.userName, (error, msg) => {
});
If you've followed these steps correctly and all the request succeeded, you should be able to see and send messages to devices/users via our dashboard and API.