Skip to content

How to display messages when app is running in the foreground?

Andrey Kadochnikov edited this page Jun 27, 2016 · 22 revisions

In order to display incoming messages (both pushed by APNs and pulled from the server) while your application is running in the foreground, you have to:

  1. Subscribe to MMNotificationMessageReceived notification:

    //Objective-C
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(handleNewMessageReceivedNotification:)
                                                 name: MMNotificationMessageReceived
                                               object: nil];
    //Swift
    NSNotificationCenter.defaultCenter().addObserver(self,
                                                     selector: #selector(self.handleNewMessageReceivedNotification(_:)),
                                                     name: MMNotificationMessageReceived,
                                                     object: nil)
  2. Handle the notifications:

    //Objective-C
    -(void)handleNewMessageReceivedNotification:(NSNotification *)notification {
    	// Only display the message if the application is in the foreground
    	if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
    		id messagePayload = notification.userInfo[MMNotificationKeyMessagePayload];
    		if ([messagePayload isKindOfClass:[NSDictionary class]]) {
    		
    			NSString * messageText = [messagePayload mm_apsAlertBody];
    			
    			// here comes your custom UI, i.e. standard UIAlertController:
    			UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"New message" message:messageText preferredStyle:UIAlertControllerStyleAlert];
    			[alertController addAction: [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:nil]];
    			UIViewController * presentingVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    			[presentingVC presentViewController:alertController animated:YES completion:nil];
    		}
    	}
    }
    //Swift
    func handleNewMessageReceivedNotification(notification: NSNotification) {
    	// Only display the message if the application is in the foreground
    	guard UIApplication.sharedApplication().applicationState == .Active else {
    		return
    	}
    	guard let userInfo = notification.userInfo,
    		let messagePayload = userInfo[MMNotificationKeyMessagePayload] as? [NSObject : AnyObject],
    		let messageText = messagePayload.mm_apsAlertBody else {
    			return
    	}
    	
    	// here comes your custom UI, i.e. standard UIAlertController:
    	let alertController = UIAlertController(title: "New message", message: messageText, preferredStyle: .Alert)
    	alertController.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: nil))
    	let presentingVC = UIApplication.sharedApplication().keyWindow?.rootViewController {
    		presentingVC.presentViewController(alertController, animated: true, completion: nil)
    	}
    }

mm_apsAlertBody is a public method of NSDictionary extension. In case the receiver is a message payload dictionary1, it returns the text of the remote message.


1: A dictionary that contains information related to the remote notification, potentially including a badge number for the app icon, an alert sound, an alert message to display to the user, a notification identifier, and custom data.

Clone this wiki locally