Description
Smartphones today are considered more and more a key part in any digital solutions. Increased computing capabilities and faster internet connections allow for a new horizon of connected apps. Some IoT applications today use smartphones as gateways, but it is also normal to use the smartphone as a notification and awareness tool in some cases where promptness is key.
That is why we have developed a new node for the onesait Platform Flow Engine: onesaitplatform-firebase-notification. This node features integration with Firebase. Firebase is a platform from Google that helps developers dealing with their mobile apps backend. Some of the platform features are open to everybody and that is the case for the Cloud Messaging feature.
This post showcases an example of native notifications to Android apps using onesait Platform Flow Engine. The guide will highlight the process needed to perform the example using onesait Platform modules, whereas the Firebase specific task will be just mentioned but not explained in detail, please contact us for extra info on how to integrate your mobile app within the Firebase platform.
For this example, we have built a custom Android app with a basic layout and functionality:
This app has native integration with Firebase, allowing it to be connected to Firebase and receive native notifications (to know more about Firebase integration follow this link).
The app behavior looks like this:
- First, upon app installation, the app connects with Firebase and is granted with a Device Token. This token is unique for the Android device though it might be updated eventually. It is advisable to monitor these changes using the callback within the onNewToken method of your class which extends FirebaseMessagingService.
- Whenever a new token is granted, or when the user clicks on the REGISTER DEVICE TOKEN button, the granted token is then forwarded to onesait Platform. These token keys are stored in the NativeNotifKeys ontology, which looks like this (for this example app will be messaging, and the user will be the IMEI code from the host smartphone):
- To perform step 2, NativeNotifKeys ontology should be created (already existing in the demo environment), and an API endpoint shall be created in the environment. This API will publish this ontology through an URL, with INSERT capabilities. For this example this is how the API definition looks like:
- Now the app, which already has a valid token from Firebase, has replicated this info to onesait Platform, allowing developers to leverage this data and send notifications to these devices. But we still have to enable a way to insert messages via onesait Platform to the end devices. That is why we define this ontology, to record the incoming messages:
- After the ontology creation, we need to define the rule to send the messages inserted in the notificationMessage ontology to the end devices. We will use the onesait Platform Flow Engine module to perform this operation:
- The first block just inserts notification messages into the platform. Then the second block uses a onesaitplatform-notification-endpoint node to subscribe to the previous insertions into the notificationMessage ontology. This node will insert the message to the onesaitplatform-firebase-notification node which deals with the Firebase integration for the demo app.
- With the flow set, and before start sending notifications, let us explore two different types of messages we have integrated in the example:
- Notification to a single, specific device.
- Notification to all devices subscribed to a certain topic.
- To change this behavior, there is a topic field inside the notificationMessage ontology. If it is blank ("topic":"") then the firebase-notification node will send this notification to the device corresponding to the user field inside the message, using the current Firebase token updated by the app via the API in NativeNotifKeys. This is the message, the Flow Engine output and how the notification appears on the mobile phone:
The notification appears promptly with the title and body we defined in the instance of the ontology. Firebase claims that over 95% of the notifications they send are delivered within an interval of 250ms.
The last step will be to send notifications to a group of devices subscribed to a certain topic. To do so first, go to the app and click on the SUBSCRIBE TO NEWS button:
And then change the topic to news inside the Flow Engine insertion:
For topic-based notifications testing, it is better to install the example app into more than one phone so you can see the difference. From one phone, the result of this will be the arrival of the notification:
The time-diagram to understand the full flow of actions is this:
Now test it yourself and let us know!
If you have any question or requirement please contact us at support@onesaitplatform.com