Guide: Local Notifications in Foreground in iOS




Guide: Local Notifications in Foreground in iOS



In iOS 11, scheduling a notification will always result in delivery (i.e. the user sees it) as long as the user is not using the app at delivery time (and the iPhone is on). This means that either the user has the iPhone locked, is using a different app, or is on a home screen.


Below are the steps to present a local notification in the foreground (i.e. while the user is in your app).



1. Add the file "CustomNotifications.swift" (or whatever name you want) to your project (File > New > Swift file > ...). You will use this class in AppDelegate.swift in order to handle the presentation of local notifications while the app is running in the foreground.
import Foundation import UserNotifications class CustomNotifications: NSObject, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { //Play sound, show alert (will occur if enabled by user AND scheduled in the notification request). completionHandler([.alert, .sound]) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { //Note that any categories you may wish to use will need to be handled in the completionHandler. completionHandler() } }
2. Here is the regular AppDelegate.swift file. It was automatically created with your project. You will need to modify it to the following: The changes are namely to import UserNotifications, create a CustomNotifications object in the AppDelegate class, and add the two lines of code to the application function.
import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let notificationDelegate = CustomNotifications() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let center = UNUserNotificationCenter.current() center.delegate = notificationDelegate return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Saves changes in the application's managed object context before the application terminates. self.saveContext() } }

At this point, any scheduled notifications will be delivered even when the app is running in the foreground.

Comments

Popular posts from this blog

NULL vs None in sqlite3 for Python

Complete SQLite IMDb Database with Python