When Apple's official iOS Messages app is open and in the foreground, new messages from other contacts trigger a stock, native 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:
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 data while in the foreground. But I see no way to present the native iOS notification alert UI.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// I know we still receive the notification `userInfo` payload 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, native iOS Local or Push Notification alerts UI while your application is open and in the foreground.
iOS 10 adds the UNUserNotificationCenterDelegate
protocol for handling notifications while your app is in the foreground.
The
UNUserNotificationCenterDelegate
protocol defines methods for receiving notifications and for handling actions. When your app is in the foreground, arriving notifications are delivered to your delegate object instead of displayed automatically using the system interfaces.
Swift:
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
Objective-C:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
The UNNotificationPresentationOptions flags allow you to specify UNNotificationPresentationOptionAlert
to display an alert using the text provided by the notification.
This is key as it allows you to display the alert while your app is open and in the foreground, which is new for iOS 10.
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Set UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().delegate = self
return true
}
}
// Conform to UNUserNotificationCenterDelegate
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler(.alert)
}
}