# 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-Swift) to understand the concepts and to start the push integration. In order to use `MBMessagesSwift` instead of `MPushSwift` you have to do the following changes:

Set the push token like this:

```swift
MBMessages.pushToken = "YOUR_PUSH_TOKEN"
```

And then register your device to topics (all the other function have a similar syntax change):

```swift
MBMessages.registerDeviceToPush(deviceToken: deviceToken, success: {
    MBMessages.registerPushMessages(toTopic: MBPTopic("YOUR_TOPIC"))
})
```

MBurger has 2 default topics that you should use in order to guarantee the correct functtionality 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

```swift
MBMessages.registerPushMessages(toTopics:[MBMessages.projectPushTopic,
                                          MBMessages.devicePushTopic,
                                          MBPTopic("OTHER_TOPIC")])
```

#### MBPTopic additional parameters

When creating topic you can specify additional parameters:

* `title`: a title fot that topic that will be displayed in the dashboard, if not specified it has the same value as the topic id
* `single`: If the topic identify a single user or a group of users, defaults to `false`

## User interaction with a push

With `MBMessagesSwift` you can setup a callback that will be called when the user interacts with a push notification or opens the app from a push. You can setup the code like this, the payload variable will be the payload of the push:

```swift
MBMessages.userDidInteractWithNotificationBlock = { payload in
    // Do actions in response
    print("Notification arrived:\n\(payload)")
}
```

In order to this to function you will have to tell `MBMessagesSwift` that a notification has arrived so you need to add this in those lines in your `UNUserNotificationCenterDelegate` class, often the `AppDelegate`.

```swift
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler
    completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    // Add this line
    MBMessages.userNotificationCenter(willPresent: notification)
    completionHandler(UNNotificationPresentationOptions.alert)
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler
    completionHandler: @escaping () -> Void) {
    // Add this line
    MBMessages.userNotificationCenter(didReceive: response)
    completionHandler()
}
```
