-
Notifications
You must be signed in to change notification settings - Fork 21
How to implement custom message handler
Olga Koroleva edited this page Mar 17, 2017
·
3 revisions
- Create custom message handler class, it should implement
MessageHandlingprotocol. Handler in example below will show local notification when it received message.
//Swift
class CustomMessageHandler: MessageHandling {
func didReceiveNewMessage(message: MTMessage) {
UIApplication.shared.presentLocalNotificationNow(localNotification(with: message))
}
func localNotification(with message: MTMessage) -> UILocalNotification {
let localNotification = UILocalNotification()
localNotification.alertBody = message.text
localNotification.soundName = message.sound
return localNotification
}
}//Objective-C
@interface CustomMessageHandler : NSObject<MessageHandling>
@end
@implementation CustomMessageHandler
- (void)didReceiveNewMessageWithMessage:(MTMessage *)message {
[[UIApplication sharedApplication] presentLocalNotificationNow:[self localNotificationWithMesage:message]];
}
- (UILocalNotification*)localNotificationWithMesage:(MTMessage *)message {
UILocalNotification* notification = [UILocalNotification new];
notification.alertBody = message.text;
notification.soundName = message.sound;
return notification;
}
@end- Set your message handler to MobileMessaging property
//Swift
MobileMessaging.messageHandling = CustomMessageHandler()//Objective-C
[MobileMessaging setMessageHandling:[CustomMessageHandler new]];If you have any questions or suggestions, feel free to send an email to [email protected] or create an issue.