Subscribe to 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 MBTopic Arraylist with the topic names, then call the API:
1
ArrayList<MBTopic> topics = new ArrayList<MBTopic>()
2
topics.add(new MBTopic(
3
TOPIC_CODE, //The main topic String to which subscribe (eg. sport)
4
TOPIC_NAME, //A familiar name to give your topic (eg. Sport Club)
5
false //If it's a topic where only one user should subscribe
6
))
7
​
8
MBurgerPushTasks.registerTopics(context,
9
getDeviceId(context), topics)
Copied!
From the MBurger Push dashboard then you can send push notification only to some topics of making 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 maintain 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.
1
@Override
2
public void onMessageReceived(RemoteMessage remoteMessage) {
3
Map<String, String> map = remoteMessage.getData();
4
​
5
//The standard message is inside the "body" field
6
String msg = map.get("body");
7
if(map.containsKey("custom")) {
8
String custom = map.get("custom");
9
if(custom != null){
10
try {
11
JSONObject jCustom = new JSONObject(custom);
12
//Take out the data you inserted inside the notification and create your notification with Android SDK.
13
} catch (JSONException e) {
14
e.printStackTrace();
15
}
16
}
17
}
18
}
Copied!
To create a notification with the Android SDK refer to this documentation.

Push topics management

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:
1
MBurgerPushTasks.unregisterAllTopics(context, getDeviceId())
Copied!
This way you'll no more receive push messages from any topic you registered the device before.
Last modified 10mo ago
Copy link