SDK

Remote Notifications

In this page you'll learn how notifications are handled in your app and what all the options at your disposal to create a great messaging experience for your users.

Notificare supports several types of interactive and actionable notifications that will be handled for you without any extra development. If you are going to prevent this default behaviour, please note that you will have to either handle all the functionality yourself (metrics logging, presenting UI or collect replies) or if you don't, you understand that some features will not work as advertised.

Receiving Notifications

Then your main.dart, you'll need to implement the following event listener in order to receive a notification whenever it arrives and the app is being used:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "remoteNotificationReceivedInForeground") {
		//Here you probably don't want to interrupt the user and simply show that a notification has arrived with an in-app badge
	}
});

The best approach when the app is being used and a notification arrives, is to not interrupt the user, you can save this information and display some UI element that warns the user without interrupting whatever the user was doing.

But in most cases, notifications will arrive when the app is not being used. Users will click on it from the notification center or lock screen and you will want to show those immediately to the user. In those cases the following event will be triggered:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "remoteNotificationReceivedInBackground") {

	}
});

Whenever this delegate is triggered you will need to handle it accordingly by presenting the notification. This is achieved by using the following method:

1
2
3
4
5
6
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "remoteNotificationReceivedInBackground") {
		NotificareRemoteNotificationReceivedInBackgroundEvent remoteNotificationReceivedInBackgroundEvent = event.data as NotificareRemoteNotificationReceivedInBackgroundEvent;
		notificare.presentNotification(remoteNotificationReceivedInBackgroundEvent.notification);
	}
});

System Notifications

In Notificare you can send silent notifications that will wake up your app and can be used to download new data. These are called System Notifications and will not be added to the device's lock screen or notification center nor they will be added to the inbox (if implemented).

In Android, these are handled by the Receiver in the native part of your app. To handle these notifications, please refer to the Android SDK docs.

Notifications from Unknown Sources

In some apps it possible you're also using other providers to send remote notifications, when that is the case Notificare will recognize an unknown notification and ignore it. If you want to allow non-Notificare messages, these should be handled by a custom PushService in the native part of your app and you will have to provide your own mechanism to have them end up in your ReactNative code.

Deep Links

In modern apps, this is a great way of creating interactions between notifications and your own app content, allowing you to send messages that can eventually drive the user to open content hidden deeper in your app.

To prepare your app to handle deep links is not complicated and will allow you to handle not only Deep Link notification types, but also any link made from an web page. In order to indicate that your MainActivity should handle a custom URL scheme you will have to declare the following in your AndroidManifest.xml file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<activity
	android:name=".MainActivity"
	android:label="@string/app_name"
	android:launchMode="singleTop"
	android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />
			<category android:name="android.intent.category.LAUNCHER" />
		</intent-filter>
		<intent-filter>
			<action android:name="re.notifica.intent.action.NotificationOpened" />
			<category android:name="android.intent.category.DEFAULT" />
		</intent-filter>
		<intent-filter android:label="my_deep_link">
			<action android:name="android.intent.action.VIEW" />
			<category android:name="android.intent.category.DEFAULT" />
			<category android:name="android.intent.category.BROWSABLE" />
			<data android:scheme="@string/app_url_scheme"/>
		</intent-filter>
</activity>

The example above assumes you've added the value of your URL Scheme in res/values/strings.xml as follows:

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
	...more
	<string name="app_url_scheme">your.deep.link</string>
	...more
</resources>

Then in your main.dart you must implement the following event listener which will be triggered whenever deep links are executed:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "urlOpened") {
		//Handle URL
	}
});

There is yet another situation where you will also want to handle deep links and that is when users click in a link in a HTML or Web Page notification. If that link should open a view in your app you will want to intercept those clicks and handle it the same way you handle deep links.

First make sure you declare those URL Scheme in res/values/url_schemes.xml:

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="url_schemes">
		<item>your.deep.link</item>
	</string-array>
</resources>

Our library will then intercept all the links using those URL Schemes and trigger the following event:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "urlClickedInNotification") {
		//Handle URL when clicked from a notification
	}
});

Inbox

With our library it's extremely easy to implement an in-app inbox. Implementing an inbox increases considerably the engagement rates of your notifications simply because messages will always be available inside your app. To activate the inbox functionality, please follow the instructions described here.

After activating this functionality, implement the following event listener in your main.dart which will be triggered whenever the inbox initializes or reloads its data:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "inboxLoaded") {
		//Update your list of inbox items
	}
});

To retrieve the list of messages the user have previously received, invoke the following method:

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

To open a message from the list of messages in the inbox, please use the following method:

1
2
3
4
5
6
7
8
try {
	//Use an object from the fetchInbox() method
	var inboxItem = inboxItems[0];
	notificare.presentInboxItem(inboxItem);
	//Handle Success
} catch(e){
	//Handle Error
}

At anytime you can also delete a notification from the inbox:

1
2
3
4
5
6
7
8
try {
	//Use an object from the fetchInbox() result
	let inboxItem = inboxItems[0];
	NotificareInboxItem result = await notificare.removeFromInbox(inboxItem);
	//Handle Success
} catch(e){
	//Handle Error
}

Additionally you can also mark a message as read by invoking the following method:

1
2
3
4
5
6
7
8
try {
	//Use an object from the fetchInbox() result
	let inboxItem = inboxItems[0];
	NotificareInboxItem result = await notificare.markAsRead(inboxItem);
	//Handle Success
} catch(e){
	//Handle Error
}

Finally to remove all the items in the inbox you would do the following:

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

If you've configured your inbox functionality to use the AutoBadge feature, then you can implement the following event listener in your main.dart. This will be triggered whenever there's changes to the inbox items unread count and can help you manage your UI:

1
2
3
4
5
notificare.onEventReceived.listen((NotificareEvent event) {
	if (event.name == "badgeUpdated") {
		//Use this to display a badge in your app
	}
});