SDK

Device Registration

In this guide we will dive deeper into how you should handle the device registration.

Before you can send notifications to your Flutter app you will need to register the device with Notificare. By default our plugin will automatically assign a non-push device token whenever you invoke the launch() method for the first time. This will then trigger the deviceRegistered event, as shown below:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "deviceRegistered") {
		//At this point you will have a non-push device which will allow you to use all the feature of Notificare, except remote notifications
	}
});

To actually register a device with FCM (remote notifications) you will need to invoke the following method:

1
notificare.registerForNotifications();

Whenever you invoke this method, your app will receive a FCM device token and automatically register an anonymous device. This will also trigger the deviceRegistered event, as shown below:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "deviceRegistered") {
		//At this point you will have a push device
	}
});

Because these tokens can change at any time you need to make sure the device registration happens every time your apps launch. The library will automatically discern if the registration should be skipped, in case nothing changes, or if it should actually update device in case a new device token is assigned. To handle this you will want to implement something like this in the ready event:

1
2
3
4
5
6
7
notificare.onEventReceived.listen((NotificareEvent event) async {
	if (event.name == "ready") {
		if (await notificare.isRemoteNotificationsEnabled()) {
			notificare.registerForNotifications();
		}
	}
});

You can also quickly know if a user has granted permission for alerts, sounds and badge by invoking the following method:

1
bool granted = await notificare.isAllowedUIEnabled();

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 (and user has a valid session) or when the user signs in. When that is the case, you will can invoke the following:

1
2
3
4
5
6
7
try {
	//Note the pseudo code MyAuthClass which should be your own property that holds the user information
	NotificareDevice response = await notificare.registerDevice(MyAuthClass.userID, MyAuthClass.userName);
	//Handle Success
} catch(e){
	//Handle Error
}

When you authenticate users, you will also have the need to anonymize the device whenever a user signs out. This can be done by using the method below:

1
2
3
4
5
6
try {
	NotificareDevice response = await notificare.registerDevice(null, null);
	//Handle Success
} catch(e){
	//Handle Error
}

Override Device Language

By default we will automatically collect the language and region of a device based on the locale of the device. For most cases this will be enough but for those cases where you would like to override the device language and region combination to a strict selection of languages, you can do so by invoking the following method:

1
2
3
4
5
6
try {
	await notificare.updatePreferredLanguage("be-NL");
	//Handle Success
} catch(e){
	//Handle Error
}

Eventually you can always retrieve the preferred language by invoking the following method:

1
2
3
4
5
6
try {
	String response = await notificare.fetchPreferredLanguage();
	//Handle Success
} catch(e){
	//Handle Error
}

User Data Fields

There are use cases where simply associating an userID and userName will not suffice. For those cases you can make use of User Data Fields where you can create key-value pairs for your application and then use them to extend your device registration. Before you can implement them you will need to first create them in your application via our dashboard. This is described here.

Once you have created one or more fields, you can implement this functionality in your app. To retrieve the list of fields allowed in your app you should use the following method (make sure you call it after the ready event):

1
2
3
4
5
6
try {
	List<NotificareUserData> response = await notificare.fetchUserData();
	//Handle Success
} catch(e){
	//Handle Error
}

Whenever you want to update those fields with new values you should use the following method:

1
2
3
4
5
6
7
try {
	//Where fields must be an array of objects retrieved from fetchUserData()
	List response = await notificare.updateUserData(fields);
	//Handle Success
} catch(e){
	//Handle Error
}

Do Not Disturb

Each device registered with Notificare can be configured to specify a period of time during the day where it should not receive notifications. You can use this functionality in your app settings view to allow the user to provide a time range where messages will not be displayed in the lock screen or notification center. Please note that if you are using our inbox functionality these message will still be there.

To retrieve a device's DnD preferences use the following method:

1
2
3
4
5
6
try {
	NotificareDeviceDnD response = await notificare.fetchDoNotDisturb();
	//Handle Success
} catch(e){
	//Handle Error
}

You can update the DnD values for a device, using the following method:

1
2
3
4
5
6
try {
	NotificareDeviceDnD response = await notificare.updateDoNotDisturb({start: "01:00", end: "07:00"});
	//Handle Success
} catch(e){
	//Handle Error
}

And eventually if you want to clear any Do Not Disturb values from this device, you can use the method below:

1
2
3
4
5
6
try {
	await notificare.clearDoNotDisturb();
	//Handle Success
} catch(e){
	//Handle Error
}

Disable Notifications

It is also possible to disabled remote notifications and unregister from FCM. To do that use the following method:

1
notificare.unregisterForNotifications();

Doing this will remove the device from FCM and remote notifications will no longer be sent or received. When this method is invoked, we will automatically register a device identifier that although will never receive remote notifications, you will still maintain the same user profile, inbox messages and enjoy all the other services your plan supports. You can at anytime request a re-register with FCM if you wish to.