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.android.js file:
DeviceEventEmitter.addListener('ready', function(e: Event) {
Notificare.enableNotifications();
});
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 FCM, by calling enableNotifications.
Whenever you invoke the enableNotifications method, our library will request a device registration with FCM which in turn will trigger the didReceiveDeviceToken event, as shown below:
DeviceEventEmitter.addListener('didReceiveDeviceToken', function(e: Event) {
Notificare.registerDevice(e.device, null, null, (error, msg) => {
});
});
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.
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:
DeviceEventEmitter.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) => {
});
}
});
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
}
});
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.
Disable Notifications
It is also possible to disabled notifications in FCM and unregister a device from Notificare. Although we do not see a reason to this, there might be cases where you want to implement this kind of functionality.
Pretty much the same way you enable notifications you will also disabled them. To do that, use the following method:
Notificare.disableNotifications();
Doing this will remove the device token from FCM and deactivate the device in Notificare. This will also mean that all the messages in the device's inbox (if applicable), tags or any other device setting will be deleted too. If this is the user's only device, Notificare will also inactivate the user. Obviously after this you will not be able to find or send a message to this device.