How to Handle Local Notification When App Has Been Terminated

Local notification on application termination

You will not receive any notice when it gets terminated, when your app is suspended in the background.

iOS will send a kill -9 signal for your apps progress and you app is just killed, this is the same thing that happens when the user kills your app from the quicklaunch tray.

From the Apple documentation:

Even if you develop your app using iOS SDK 4 and later, you must still
be prepared for your app to be killed without any notification. The
user can kill apps explicitly using the multitasking UI. In addition,
if memory becomes constrained, the system might remove apps from
memory to make more room. Suspended apps are not notified of
termination but if your app is currently running in the background
state (and not suspended), the system calls the
applicationWillTerminate: method of your app delegate. Your app cannot
request additional background execution time from this method.

Handle tap on local notification when app is terminated - iOS - Swift

When you tap on Push Notification on Notification Center.
OS launches your app (terminated before) then STILL delivers the action to this (Function 1)

func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)

You can get the notification object from response like what you did.
But you may need to save temporary this data from the selected push notification, then handle it later when app is completely active with user.

Remember to call completionHandler() when you finish your logic. And, you must set UNUserNotificationCenter.current().delegate = self, before you return true in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool`.

self here is your AppDelegate, which means you set UserNotification's delegate to a live object or initialized object.
It will be able to handle the above delegate function call (Function 1) to get your selected push notification's data.

Will local notification in iOS work even if the app is terminated?

Yes, local Notification will work even if your app is terminated. You just need to register a Local Notification with a date on which you want to fire that Notification.

Local Notification will fire on the registered date whether your app is terminated or not.

Scheduling a local notification when the app is terminated

You are pretty close to the strategy I would use. Rather than schedule the notification when you enter the background, clear and schedule every time the app becomes active.

func applicationDidBecomeActive(_ application: UIApplication) { 
UNUserNotificationCenter.current().removePendingNotificationRequests( withIdentifiers: ["notification"]
NotificationManager.shared.schedule(notification)
}

Every time you launch, you should setup a notification for 2 weeks in the future. If they launch tomorrow, delete the old notification, and setup a new one for two week after that.

Sending local notifications after the app has been terminated

Sure it is possible, if you schedule a local notification it will fire even if you terminate the app.

        UILocalNotification *_localNotification = [[UILocalNotification alloc] init];
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:_value];
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
_localNotification.alertBody = @"Beep... Beep... Beep...";
_localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];

... works like a charm for me.

How to handle localNotifications when app is closed

Have you registered for notifications?
In didFinishLaunchingWithOptions():

    // Register Notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { granted, error in

if granted {
print("User notifications are allowed")
} else {
print("User notifications are NOT allowed")
}
})
application.registerForRemoteNotification()
UNUserNotificationCenter.current().delegate = self

Then you should catch local notifications in UNUserNotificationCenterDelegate method:

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

completionHandler()
}

}


Related Topics



Leave a reply



Submit