If your project it configured to send push notifications you must use this set of API to proper register your users' devices tor receive push notifications. Also you'll need to have basic knowledge of Google Firebase and Firebase Cloud Messaging.
You must create a Firebase project for your application, this way you'll have the push server key to insert in your MBurger Push project
. This will generate a push API KEY.
To create a new Firebase project, please reference this documentation. Remember that you'll need at least these Firebase dependecies:
com.google.firebase:firebase-core
com.google.firebase:firebase-messaging
For FCM client setup refer this documentation. Long story short you'll ned to extend a new FirebaseMessagingService
which will trigger onNewToken
to obtain the Firebase token which you'll need to send to the MBurger API and onMessageReceived
, an event triggered when a push message is received.
Be aware that MBurger uses the "data" message types in order to permit the developers to completely customize notifications and behaviour on receiveing notifications (you'll find all about data messages here)
After you have set up your project for Firebase you can start using Nooko SDK to register users receive FCM messages.
If you have the MBurger Android SDK already installed on your project, you also have MPush, if you wish to add it without MBurger you can get the library via Maven adding to your top build.gradle
file this repository:
maven {
url "https://dl.bintray.com/mumbleideas/MBurger-Android/"
}
Then add to your dependencies:
implementation 'mumble.mburger:mpush-android:1.0'
To register a new device you'll need to have a Firebase token, which you can obtain one from your FirebaseMessagingService
method onNewToken
:
@Override
public void onNewToken(String token) {
}
Then is a best practice to register your device calling the registration API:
@Override
public void onNewToken(String token) {
MBurgerPushTasks.sendToken(getApplicationContext(),
listener, //OPTIONAL LISTENER FOR TOKEN SENDING AND ERROR MANAGING
getDeviceID(), token);
}
Where getDeviceID()
is your method to obtain the Android ID which will be your unique identifier. Pay attention to the changes Oreo makes to this data, refer to this documentation.
Now your device is ready to receive push messages with your FirebaseMessagingService
method onMessageReceived
, but if you need to differentiate push groups you may need to use topics.
A topic is like a group you can subscribe in order to send push notifications specifically to that topic, you can subscribe to multiple topics creating a JSONArray with the topic names, then call the API:
JSONArray topics = new JSONArray();
//You can decide whatever name you wish for the topics.
topics.put("topic1");
MBurgerPushTasks.registerTopics(context, getDeviceID(), topics);
From the MBurger Push dashboard
then you can send push notification only to some topics of make an app send push notifications to a specific topic. While it's not necessary to subscribe to topics at every startup, it can be useful to resubscribe anytime your InstanceID
changes in order to mantain data coherence.
When you receive a push notification then your FirebaseMessagingService
will be triggered and you will find all the data you inserted on the "data" field of the RemoteMessage object.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> map = remoteMessage.getData();
//The standard message is inside the "body" field
String msg = map.get("body");
if(map.containsKey("custom")) {
String custom = map.get("custom");
if(custom != null){
try {
JSONObject jCustom = new JSONObject(custom);
//Take out the data you inserted inside the notification and create your notification with Android SDK.
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
To create a notification with the Andorid SDK refer this documentation.
To unsubscribe from a push topic you'll need to call the unregisterTopics() API with the same fields you used with registerTopics(). You can also unsubscribe to all topics you subscribed by calling:
MBurgerPushTasks.unregisterAllTopics(context, getDeviceId())
This way you'll no more receive push messages from any topic you registered the device before.