# Push notifications

With this plugin you can also manage the push notification section of MBurger, this is a wrapper around MPush, the underlying platform, so you should refer to the [MPush documentation ](https://github.com/Mumble-SRL/MPush-Flutter) to understand the concepts and to start the push integration. In order to use `MBMessages` instead of `MPush` you have to do the following changes:

Set the push token like this:

```dart
MBPush.pushToken = "YOUR_PUSH_TOKEN";
```

Configure the callbacks and Android native interface like this:

```dart
MBPush.configure(
  onNotificationArrival: (notification) {
    print("Notification arrived: $notification");
  },
  onNotificationTap: (notification) {
    print("Notification tapped: $notification");
  },
  androidNotificationsSettings: MPAndroidNotificationsSettings(
    channelId: 'messages_example',
    channelName: 'mbmessages',
    channelDescription: 'mbmessages',
    icon: '@mipmap/icon_notif',
  ),
);
```

To configure the Android part you need to pass a `MPAndroidNotificationsSettings` to the configure sections, it has 2 parameters:

* `channelId`: the id of the channel
* `channelName`: the name for the channel
* `channelDescription`: the description for the channel
* `icon`: the default icon for the notification, in the example application the icon is in the res folder as a mipmap, so it's adressed as `@mipmap/icon_notif`, iff the icon is a drawable use `@drawable/icon_notif`.

### Request a token

To request a notification token you need to do the following things:

1. Set a callback that will be called once the token is received correctly from APNS/FCM&#x20;

```dart
MBPush.onToken = (token) {
    print("Token retrieved: $token");
}
```

1. Request the token using MPush:

```dart
MBPush.requestToken();
```

### Register to topics

Once you have a notification token you can register this device to push notifications and register to topics:

```dart
MBPush.onToken = (token) async {
  print("Token received $token");
  await MBPush.registerDevice(token).catchError(
    (error) => print(error),
  );
  await MBPush.registerToTopic(MPTopic(code: 'Topic')).catchError(
    (error) => print(error),
  );
  print('Registered');
};
```

The topic are instances of the `MPTopic` class which has 3 properties:

* `code`: the id of the topic
* *\[Optional]* `title`: the readable title of the topic that will be displayed in the dashboard, if this is not set it will be equal to `code`.
* *\[Optional]* `single`: if this topic represents a single device or a group of devices, by default `false`.

### MBurger topics

MBurger has 2 default topics that you should use in order to guarantee the correct functionality of the engagement platform:

* `MBMessages.projectPushTopic()`: this topic represents all devices registred to push notifications for this project
* `MBMessages.devicePushTopic()`: this topic represents the current device

```dart
await MBPush.registerToTopics(
  [
    await MBMessages.projectPushTopic(),
    await MBMessages.devicePushTopic(),
    MPTopic(code: 'Topic'),
  ],
);
```

### Launch notification

If the application was launched from a notification you can retrieve the data of the notification like this, this will be `null` if the application was launched normally:

```dart
Map<String, dynamic> launchNotification = await MBPush.launchNotification();
print(launchNotification);
```
