This document explains how you can receive push notifications that are send from Sensorberg.
The push notification is a plugin to the Sensorberg SDK. You need to declare it’s dependency:
com.sensorberg.smartspaces:push:1.9.0
It’s version should be the same as the Sensorberg SDK version. It is available from 1.9.0
To make the push plugin work with the SDK you need to call withPluginInitializer()
on the SmartSpacesSdk.Builder and pass the PushPluginInitializer.
SmartSpacesSdk.Builder(application, baseUrl, oauthId, certificate)
.withPluginInitializer(PushPluginInitializer())
.build()
SensorbergFirebaseMessagingService is the base class for receiving push messages from Sensorberg via Firebase Cloud Messaging. To receive messages you must extend it:
class YourFirebaseMessagingService : SensorbergFirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
// register the token to the Sensorberg SDK by calling UserManager.registerFirebaseDeviceToken(token)
}
override fun onMessageReceived(pushMessage: PushMessage) {
// called when a push message has been received
}
}
And register it in your AndroidManifest.xml
.
<service
android:name=".YourSensorbergFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
To support receiving messages in direct boot mode, add android:directBootAware="true"
to the service declaration.
Before you can receive push messages, you need to register the Firebase device token to the Sensorberg SDK. You get the token either by overriding onNewToken or like this.
You can then register this token by calling PushNotificationController.registerFirebaseDeviceToken(token).
It is your responsibility to ensure the token is passed successfully to the SDK. If the request fails (no connection) you should retry the request as soon as possible. Without having the right token, push messages will not be delivered to the device. We recommend doing this by implementing a Worker and register it to the WorkManager.
onNewToken might be called before you logged in into the Sensorberg SDK, so you have to take care you are not calling PushNotficiationController.registerFirebaseDeviceToken(token) when user is not logged in yet.
The SensorbergFirebaseMessagingService
takes care of receiving and deserializing the push messages.
If it can successfully deserialize it will pass an instance of PushMessage
to the abstract function onMessageReceived(pushMessage: PushMessage).
If the SDK is not able to deserialize the received push message you won’t get any notification of this (as there is nothing you - as Sensorberg SDK client - can do then).
There are different sub types of PushMessage
:
There is no full end-to-end (Backend -> App) testing possible at the moment. We are working on this.
However if you just want to verify that you can receive push messages via the Sensorberg SDK you could use curl.
Once you send a push message SensorbergFirebaseMessagingService.onMessageReceived(remoteMessage: RemoteMessage)
should have get called.
You could overwite that function and print a log.
You do not need to parse the push message yourself within onMessageReceived(remoteMessage: RemoteMessage)
! This is only to verify that the SDK receives a push message.
If the push massge can be parsed the SDK will call SensorbergFirebaseMessagingService.onMessageReceived(pushMessage: PushMessage)
.
If the push message could not be parsed you will not receive any other notification from the SDK. As this is a scenario where the user or the consumer of the SDK can not do anything.
To send a push notification you can execute this curl command.
curl --location --request POST 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=<Firebase Server Key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"to":"<Firebase Device Token>",
"data": {}
}'
You have to set <Firebase Server Key>
and <Firebase Device Token>
.
<Firebase Server Key>
you can get at: Firebase console → Project Settings → Cloud Messaging → Project Credentials.<Firebase Device Token>
you get by overwriting SensorbergFirebaseMessagingService.onNewToken()
, see: FirebaseMessagingService.onNewToken()In the above curl request, the data property is empty because the SDK won’t be able to parse the data, as it requires proper encryption from the backend side!