pkamb pkamb - 1 year ago 443
iOS Question

Displaying a stock iOS notification banner when your app is open and in the foreground?

When Apple's official iOS Messages app is open and in the foreground, new messages from other contacts trigger a stock iOS notification alert banner. See image below.

Is this possible in 3rd party apps on the app store? Local and/or push notifications for your app while your app is open and in the foreground?

When testing my app, notifications are received but no iOS alert UI is shown.

But this behavior is seen in Apple's official Messages app:

Messages is open and in the foreground. Still shows a notification alert.

The Local and Remote Notification Programming Guide says:

When the operating system delivers a local notification or remote notification and the target app is not running in the foreground, it can present the notification to the user through an alert, icon badge number, or sound.

If the app is running in the foreground when the notification is delivered, the app delegate receives a local or remote notification.

So yes, we can receive the notification while in the foreground. But I see no way to present the stock iOS notification alert UI.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
// I know we still receive the notification in the foreground.
// This question is about displaying the stock iOS notification alert UI.

// Yes, one *could* use a 3rd party toast alert framework.
[self use3rdPartyToastAlertFrameworkFromGithub]

Is Messages then using a private API to display the alert while in the foreground?

For the purpose of this question, please do not suggest any 3rd party "toast" pop-up alerts on github or etc. I'm only interested if this can be done using the stock iOS local or push notification alerts while your application is open and in the foreground.

Answer Source

It does not seem to be possible to show the stock iOS notification alert when your app is open and in the foreground. must be using a private API.

The system does not display any alerts, badge the app’s icon, or play any sounds when the app is already frontmost. - UILocalNotification docs

The UIApplicationDelegate methods will still be called, allowing your app to respond to the local or remote notification:


However, the stock iOS notification alert banner UI will not be shown as it is in Apple's, which must be using a Private API.

The best you can do is is roll your own alert banner or use an existing framework:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 
    // Use a 3rd party toast alert framework to display a banner 
    [self toastAlertFromGithub]

I will change the accepted answer if foreground notification alerts are possible in a future version of iOS. Please answer this question if you discover a method for doing so!

I have opened a radar for this behavior here: rdar://22313177

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download