Remote Notifications
In this article you'll learn how notifications are handled in your app and what are 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.
Requesting Permission
Since Android 13, the notification permission is not granted by default and should be requested. We recommended targeting Android 13 to have more control over the request.
When running Android 13 and targeting Android 12 or lower, users will be prompted for the permission when the notification channel is created. Typically, when the application starts.
Although permission requests must follow a recommended standard, controlling the overall experience is something unique to each application. However, you can use the following code as inspiration for your implementation.
async function requestNotificationsPermission() {
try {
if (await NotificarePush.shouldShowPermissionRationale()) {
await NotificarePush.presentPermissionRationale({
title: '...',
message: '...',
buttonText: '...',
});
}
const status = await NotificarePush.requestPermission();
if (status == PushPermissionStatus.GRANTED) {
// Enable remote notifications
return;
}
// Unlike the method NotificarePush.checkPermissionStatus(), both iOS and Android can return permanently denied.
if (status == PushPermissionStatus.PERMANENTLY_DENIED) {
// TODO: Show some informational UI, educating the user to change the permission via the Settings app.
await NotificarePush.openAppSettings();
}
} catch (e) {
// Handle error
}
}
You can also check the status of the notifications permission at any time, as illustrated below.
async function checkNotificationsPermissionStatus() {
try {
const status = await NotificarePush.checkPermissionStatus();
switch (status) {
case PushPermissionStatus.GRANTED:
// granted
break;
case PushPermissionStatus.DENIED:
// denied
break;
case PushPermissionStatus.PERMANENTLY_DENIED: {
// Permanently denied status can only be returned in iOS and Android 12 or lower.
// Devices with Android 13 and up will return granted or denied.
break;
}
}
} catch (e) {
// Handle error
}
}
The examples above use the convenience methods available in the NotificarePush
plugin to handle the bridge into native permission. You can use any other plugin or implement the bridge yourself. The focus of the examples is guiding you on the necessary permission and recommended upgrade path.
Enabling Notifications
In order to enable the device to receive notifications, all that you need to do is invoke a single method.
await NotificarePush.enableRemoteNotifications();
Typically, the step above is done during some form of user onboarding. When the user already went through that flow, we automatically enable notifications when Notificare launches.
You can also check whether the user enrolled on remote notifications.
await NotificarePush.hasRemoteNotificationsEnabled();
Additionally, you can check if the user has disabled notifications in the System Settings.
await NotificarePush.allowedUI();
Disabling remote notifications
Disabling remote notifications can be achieved in the same fashion as enabling them.
await NotificarePush.disableRemoteNotifications();
When this method is called, we will automatically register your device to never receive remote notifications, although 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 for push notifications if you wish to.
Receiving Notifications
Before you can start receiving push notifications, you will need to set up the native counterparts of your app.
- Android
- iOS
Due to the changes in Android 12 when it comes to handling notifications, your Activity
needs to become the trampoline. Assuming you will let your MainActivity
receive the notification intents, you need to declare the following intent-filter
it in your AndroidManifest
.xml.
<activity android:name=".MainActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="re.notifica.intent.action.RemoteMessageOpened" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You should configure the Activity receiving these intents with android:launchMode="singleTask"
to prevent recreating it several times when processing the series of intents fired from opening a notification from the notifications drawer. You can also use android:launchMode="singleTop"
, but be aware the OS will recreate it when processing deep links triggered from the NotificationActivity
. For more information on launch modes, refer to the Android documentation.
The plugin will automatically handle the intent and process the incoming notification.
Go to your app's target, click in the tab Capabilities and add both Push Notifications and Background Modes/Remote Notifications as shown below.
Authorization options
Our SDK allows you to define which notification's authorization options you would like to request from your user. This is an optional step, if not defined we will register UNAuthorizationOptions.alert
, UNAuthorizationOptions.badge
and UNAuthorizationOptions.sound
by default. To define authorization options you can use the code below and customise as needed:
const availableOptions = [
'alert',
'badge',
'sound',
'carPlay',
'providesAppNotificationSettings',
'provisional',
'criticalAlert',
'announcement',
];
await NotificarePush.setAuthorizationOptions(availableOptions);
Also note that if you implement the UNAuthorizationOptions.providesAppNotificationSettings option, notifications from your app will display a button in both the Instant Tuning menu and Notification Settings. The purpose of that button is to provide users a shortcut to your app settings where they can fine-tune which kind of notifications they would like to receive. Implementing such settings views is highly recommended as it could be the reason between allowing your app to keep display notifications or being mute completely. If you do implement this option it is mandatory that you implement the following event:
NotificarePush.onShouldOpenNotificationSettings((notification) => {
// Deep link to your settings view.
});
This will give you the opportunity to present your users with the in-app settings view where you should allow them to customize what kind of notifications they should receive. If the user clicked the button from a specific notification, you will receive that object too. When it comes from the Notification Settings, that object will be nil.
To know more about authorization options, please take a look at the native documentation.