Push Notification
API Reference: Device.Notifications
Firebase Plugin is used to receive push notifications. Please refer to the Firebase Plugin Guide
A notification is a message that device displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.

Info

Push Notification services only work with published projects. It will not work with Smartface Cloud Emulator.

Notification Handlers

There are bunch of notification providers. There is no guarantee that the received notification data schema is same for all and also, handling of notifications are differs between OS. Especially, Android, Firebase Message Services can receive any type of json schemas. But somehow, notification handler needs to know what title, body, icon etc of notification. To keep the things easier, our developers don't have to write own notification handler. Instead, we came up with solution that makes clear the properties of notification.

Implement Push Notification into your Project

Before implementing Push Notification, make sure to make following changes:
    Change your packagename on Android and bundleId on iOS to declare a unique ID for your project. All Push Notification services will need this variable.
    Register your application with given ID above on Firebase or any other Push Notification providers.
    Install the Firebase plugin into your project. Refer to sf-plugins-firebase for installation steps.

Android Requirements

If you are using FCM or GCM, follow the steps on the Firebase Plugin.
Also, make sure that following lines are active in AndroidManifest.xml
1
<!-- Defines the FCM listener service to receive the push messages. -->
2
<service android:exported="false" android:name="io.smartface.android.notifications.FCMListenerService">
3
<intent-filter>
4
<action android:name="com.google.firebase.MESSAGING_EVENT" />
5
</intent-filter>
6
</service>
Copied!

iOS Requirements

Push Notification services should be enabled on signing certificates. This APNs certificate must be used during publishing.

Create app.entitlements File

    Create a new file named app.entitlements under config/iOS in your workspace
    Here is a sample app.entitlements file:
There is no file extension. The name should exactly be app.entitlements. Otherwise device will not receive a token.
XML/HTML/SVG
1
<?xml version="1.0" encoding="UTF-8"?>
2
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
<plist version="1.0">
4
<dict>
5
<key>aps-environment</key>
6
<string>production</string>
7
</dict>
8
</plist>
Copied!

How to Receive Remote Push Notification in your Application

After the plugin is installed and configurations are made, you are ready to receive token from firebase to receive notification.
As an alternative, you can refer to Smartface Push Notification Sample Project to gain more insight.
In your app.ts or any file which is executed at the start of the project, you can receive token with Firebase.messaging.getToken event:
1
import Firebase from '@smartface/plugin-firebase';
2
3
// Your other code in the file.
4
5
/**
6
* e parameter is the token, as string.
7
*/
8
Firebase.messaging.subscribeToTopic("all"); //this triggers register for notifications
9
Firebase.messaging.getToken((e) => {
10
alert("FCM Token : " + JSON.stringify(e, null, '\t')); // For debug purposes, should be removed afterwards
11
// Do stuff, send to API etc.
12
})
Copied!
Smartface recommends to keep Firebase configurations and callbacks in a separate file.
It is even better to keep them in separate folder, since Firebase Plugin also supports Crashlytics and Analytics.

Push Notification Badge

    On Android, badges are handled by the Operating System itself. There is nothing to be done by the application side.
    On iOS, badges are handled by the Notification Provider.
      For Firebase, you should send the badge information from backend. Refer to the relevant firebase docs for this info.
      You can also set the badge of the Application from the Smartface Code. Refer to the Notifications.applicationIconBadgeNumber property for more details.

Push Notification Sound & Vibrate

    On Android, sound is handled by the Operating System itself. There is nothing to be done by the application side.
    On iOS, sounds are handled by the Notification Provider.
      For Firebase, you should send the badge information from backend. Refer to the relevant firebase docs for this info.
      You can also set which sound or is going to be prompted from the Smartface Code. Refer to the Notifications.onNotificationReceive event for more details.
1
Notifications.onNotificationReceive = (e) => {
2
const alertMessage = {
3
'onNotificationReceive': e
4
}
5
alert(JSON.stringify(e, null, 4));
6
return [Notifications.iOS.NotificationPresentationOptions.SOUND, Notifications.iOS.NotificationPresentationOptions.ALERT]; // or []
7
};
Copied!

Configure Push Notification Handler

As we described, handler needs to know properties of notification. Thus, there is a json file which named as pushnotification and placed under the ~/workspace/config/Androidfolder. It contains all properties of notification. The notification handler searches for keys as title, body and so on from this file and then found keys are exposed with hierarchical path. For example, $notification.title, here is notification is root object and title is key and was what we are searching.
Exposed path matches with the received notification data. That means, the title key also will be searched in the received notification data with the exposed path ($notification.title). So, the pushnotification.json prepared according to the received notification json schema.
The below example pushnotification.json includes above examples and, the mapping between actual and desired key . Here, desired key is title-key which is name of your key (checkout example 2) and actual key is title that is title property of notifications.

Push Notification Examples

pushnotification.json Example 1:
pushnotification.json
1
{
2
"notification":{
3
"title":"title-key",
4
"body":"body-key",
5
"icon":"icon-key",
6
"channelId":"channelId-key",
7
"channelIdTitle":"channelIdTitle-key",
8
"channelIdImportance":"channelIdImportance-key",
9
"deleteChannelId":"deleteChannelId-key",
10
"showBadge":"showBadge-key"
11
}
12
"configuration_version":"2"
13
}
Copied!
configuration_version: to support backward parsing.
Example 2: Push notification data: used while requesting fcm end-point
JavaScript
1
{
2
"to":"{device-token}",
3
"data":{
4
"notification":{
5
"title-key":"Test-Title",
6
"body-key":"Test-Body",
7
"icon-key":"Test-Icon",
8
"channelId-key":"50",
9
"channelIdTitle-key":"Smartface",
10
"channelIdImportance-key":"4",
11
"deleteChannelId-key":"49",
12
"showBadge-key":"false"
13
}
14
}
15
}
Copied!
Above examples are easy, what if you need to handle schema which you don't know ? onNotificationReceive method receive all data send by your notification provider. Thus, you can examine all its schema.

Notification Basic Properties

Notification appears on device with title, body and icon.
"title" : "title of notification" "body" : "body of notification" "icon":" icon name of notification. It searches the name from **images** folder. No need to specify file extension."

Notification Channel on Android

Starting in Android 8.0, all notifications must be assigned to a channel. If channelId and title aren't exist, default values assigned.
"channelId": "any number as unique notification id for an app ", "channelIdTitle": "title of notification(app name etc.)", "channelIdImportance": "a number between 1-5" "deleteChannelId": "deletes exist channel"
Please refer to here for more information.

Badge

Apps are appear with dot or number badge when receives notification.
"showBadge" : "if sets false, badge won't appear on app icon."

FCM Push Send Example

FCM cURL

Developers can use curl to send push notifications as well. The HTTP header must contain the following headers:
-Authorization: key: This is the server key, where values are available in your Firebase project console under Project Settings > Cloud Messaging. -Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.

Sending Push Notification On Android With cURL

-to : Registered token -data: should contain the body, title and icon as specified above (notification content)
cURL
1
curl -X POST --header "Authorization: key= SERVER_API_KEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d '{
2
“to”: “YOUR_TOKEN”,
3
“body”:{
4
"title-key": "Test-Title",
5
"body-key": "Test-Body",
6
"icon-key": "Test-Icon"
7
}
8
}'
Copied!

Sending Push Notification On iOS With cURL

First of all, you should obtain the FCM token acquired from the registerForPushNotifications method. For this purpose, you need to use the curl information below:
cURL
1
curl -X POST -H "Authorization: key=YOUR_FCM_SERVER_KEY" -H "Content-Type: application/json" -d '{
2
"application": "com.sample.application",
3
"sandbox":false,
4
"apns_tokens":[
5
"472aaacdedb539abc4a6bcd1b1795b6131194234b816e4f624ffa12",
6
"642d2acceaadee8400b9952c2f45325ab9831c1998ed70859d86"
7
]
8
}' "https://iid.googleapis.com/iid/v1:batchImport"
Copied!
With the returned FCM token, you can send push notifications with the curl information below: -notification: notification content -registration_ids: registered token
cURL
1
curl -i -H 'Content-type: application/json' -H 'Authorization: key=<YOUR_SERVER_KEY>' -XPOST https://fcm.googleapis.com/fcm/send -d '{
2
"registration_ids":["<YOUR_TOKEN>"],
3
"notification": {
4
"title":"Notification Title",
5
"body":"Notification Content"
6
},
7
"data": {
8
"CUSTOMKEY" : "CUSTOMVALUE"
9
}
10
}'
Copied!

Using Push Notifications

First, you need to register for the APNS services. If registration is successful, it returns a unique token for device identification.
TypeScript
1
import Firebase from '@smartface/plugin-firebase';
2
3
// Your other code in the file.
4
5
/**
6
* e parameter is the token, as string.
7
*/
8
Firebase.messaging.subscribeToTopic("all"); //this triggers register for notifications
9
Firebase.messaging.getToken((e) => {
10
alert("FCM Token : " + JSON.stringify(e, null, '\t')); // For debug purposes, should be removed afterwards
11
// Do stuff, send to API etc.
12
})
Copied!

Multiple Scenarios With Push Notification

Foreground:
TypeScript
1
Notifications.onNotificationReceive = (e) => {
2
alert("Notification received : " + JSON.stringify(e, null, '\t'));
3
return [Notifications.iOS.NotificationPresentationOptions.SOUND, Notifications.iOS.NotificationPresentationOptions.ALERT]; // or []
4
};
Copied!
When Clicked On Push Notification
TypeScript
1
Notifications.onNotificationClick = (e): void => {
2
alert("onNotificationClick onNotificationClick")
3
};
Copied!

Local Notifications

LocalNotification is an interface between user and application that enables an application to inform its users that it has something for them. You can schedule local notification to be fired at a specific time. After scheduling a notification, regardless of the application being running or being in the background, scheduled notification will be fired on the specified time.

Note

For Android, application icon must be placed in the apk. For this reason, you can see your own icon as a notification icon only in the published builds.

Note

For iOS, if you present a local notification while your application is running in foreground you cannot get a visual alert. However, Application.onReceivedNotification callback will fire if you implement it.
JavaScript
1
import Notifications from "@smartface/native/global/notifications";
2
let notification = new Notifications.LocalNotification();
3
notification.alertAction = "Notification alertAction";
4
notification.alertBody = "Notification alertBody";
5
notification.android.vibrate = true;
6
notification.ios.hasAction = true;
7
notification.present();
Copied!
Last modified 2mo ago