Skip to content

How to implement custom message handler

Olga Koroleva edited this page Mar 17, 2017 · 3 revisions
  1. Create custom message handler class, it should implement MessageHandling protocol. 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
  1. Set your message handler to MobileMessaging property
//Swift
MobileMessaging.messageHandling = CustomMessageHandler()
//Objective-C
[MobileMessaging setMessageHandling:[CustomMessageHandler new]];

Clone this wiki locally