- 
                Notifications
    
You must be signed in to change notification settings  - Fork 22
 
Custom message payload
By using Custom Payload you can send custom data together with broadcast push messages from New Broadcast campaign page. The custom payload can be made of key-value pairs or plain JSON:
| Key-value pairs | Plain JSON | 
|---|---|
![]()  | 
![]()  | 
It is also possible to send Custom Payload using Single PUSH message and Multiple PUSH messages APIs.
The following example code shows how you can retrieve the custom payload from within your application by subscribing to MMNotificationMessageReceived notification. The MMNotificationMessageReceived notification is posted whenever a new message received by the application:
//Objective-C
// 1. Subscribe for MMNotificationsMessageReceived notification:
[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleNewMessageReceivedNotification:)
                                             name: MMNotificationMessageReceived
                                           object: nil];
// 2. Handle the notification:
-(void)handleNewMessageReceivedNotification:(NSNotification *)notification {
    id object = notification.userInfo[MMNotificationKeyMessage];
    if ([object isKindOfClass:[MM_MTMessage class]]) {
        MM_MTMessage * message = object;
        NSLog(@"Dish is %@", message.customPayload[@"dish"]);
        NSLog(@"URL is %@", message.customPayload[@"url"]);
    }
}//Swift
// 1. Subscribe for MMNotificationsMessageReceived notification:
NotificationCenter.default.addObserver(self,
                                        selector: #selector(self.handleNewMessageReceivedNotification(_:)),
                                        name: NSNotification.Name(rawValue:  MMNotificationMessageReceived),
                                        object: nil)
...
// 2. Handle the notification:
@objc func handleNewMessageReceivedNotification(notification: Notification) {
    if let message = notification.userInfo?[MMNotificationKeyMessage] as? MM_MTMessage {
        print("Dish is \(message.customPayload?["dish"] as? String)")
        print("URL is \(message.customPayload?["url"] as? String)")
    }
}You can also retrieve the message details in such special scenario as user tapping on the notification alert/banner:
- Implement 
MMMessageHandlingDelegateprotocol and it's methoddidPerform(action:forMessage:completion:): 
class MyMessageHandlingDelegate : MMMessageHandlingDelegate {
    func didPerform(action: MMNotificationAction, forMessage message: MM_MTMessage?, notificationUserInfo: [String: Any]?, completion: @escaping () -> Void) {
        if action.isTapOnNotificationAlert { // here you check if the action is indeed a tap on alert
            print("Dish is \(message.customPayload?["dish"] as? String)")
            print("URL is \(message.customPayload?["url"] as? String)")
        }
        // don't forget to call `completion`, it's very important to tell the system that action handling is finished
        completion()
    }
}- Pass the delegate object to MobileMessaging SDK:
 
MobileMessaging.messageHandlindDelegate = MyMessageHandlingDelegate()If you have any questions or suggestions, feel free to send an email to [email protected] or create an issue.

