# 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:

```java
ArrayList<MBTopic> topics = new ArrayList<MBTopic>()
topics.add(new MBTopic(
    TOPIC_CODE, //The main topic String to which subscribe (eg. sport)
    TOPIC_NAME, //A familiar name to give your topic (eg. Sport Club)
    false       //If it's a topic where only one user should subscribe
))

MBurgerPushTasks.registerTopics(context, 
        getDeviceId(context), topics)
```

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.

```bash
@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 Android SDK refer to [this documentation](https://developer.android.com/training/notify-user/build-notification).

#### Push topics management <a href="#push-topics-management" id="push-topics-management"></a>

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:

```java
MBurgerPushTasks.unregisterAllTopics(context, getDeviceId())
```

This way you'll no more receive push messages from any topic you registered the device before.
