SDK

Device Registration

In this guide we will dive deeper into how you should handle the device registration. Registering devices should only happen after our library has finished loading its initial configuration. This is signaled back to your app via the on onReady event. This is where you first can enable your app to receive notifications:

Open your .js file in www add the following code to the Cordova’s onDeviceReady callback method:

onDeviceReady: function() {

    Notificare.on('ready', function(applicationInfo) {
        Notificare.enableNotifications();
    });

    Notificare.start();

    ...more code
});

By calling enableNotifications it will trigger a GCM/FCM registration, if your app is correctly setup you will receive a device token that you should immediately register in Notificare.

This should be handled by implementing the following event listener:

onDeviceReady: function() {

    ...more code

    Notificare.on('registration', function(deviceId) {

        Notificare.registerDevice(deviceId, function() {
            //Device registered successfully
        }, function(error) {
            //Handle error
        });

    });

    ...more code
});

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 device registration differently:

onDeviceReady: function() {

    ...more code

    Notificare.on('registration', function(deviceId) {

        if (MyAuthClass.isLoggedIn) {

            Notificare.registerDevice(deviceId, MyAuthClass.userID, MyAuthClass.userName, function() {
                //Device registered successfully
            }, function(error) {
                //Handle error
            });

        } else {

            Notificare.registerDevice(deviceId, function() {
                //Device registered successfully
            }, function(error) {
                //Handle error
            });

        }

    });

    ...more code
});

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(deviceId, MyAuthClass.userID, MyAuthClass.userName, function() {
    //Device registered successfully
}, function(error) {
    //Handle error
});

If you've followed these steps correctly and all the requests 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.