Skip to content

How to implement custom message handler

Davor Komušanac edited this page Dec 5, 2022 · 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: MMMessageHandlingDelegate {
	func didReceiveNewMessage(message: MM_MTMessage) {
		UIApplication.shared.presentLocalNotificationNow(localNotification(with: message))
	}
		
	func localNotification(with message: MM_MTMessage) -> UILocalNotification {
		let localNotification = UILocalNotification()
		localNotification.alertBody = message.text
		localNotification.soundName = message.sound
		return localNotification
	}
}
//Objective-C
@interface CustomMessageHandler : NSObject<MMMessageHandlingDelegate>
@end

@implementation CustomMessageHandler
- (void)didReceiveNewMessageWithMessage:(MM_MTMessage *)message {
	[[UIApplication sharedApplication] presentLocalNotificationNow:[self localNotificationWithMesage:message]];
}

- (UILocalNotification*)localNotificationWithMesage:(MM_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.messageHandlingDelegate = CustomMessageHandler()
//Objective-C
[MobileMessaging setMessageHandlingDelegate:[CustomMessageHandler new]];
Clone this wiki locally