iOS Notification When Application Is Closed

IOS notification when Application is closed

didReceiveRemoteNotification method will not call when the application is closed.

But you can check launchOptions to know weather the application has been launched from notification or not in didFinishLaunchingWithOptions method in appdelegate and do your task accordingly.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
// Do your task here
}

}

Receive push when app is closed

The following is taken from the apple documentation...

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a push notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

Note the highlighted text, the app will not be started if it is completely closed

Open specific ViewController from push notification when application closed

If you want to open vc from the closed state of your app, you can check if app was opened from click on Notification in this method of AppDelegate.

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

It looks like:

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

// When the app launch after user tap on notification (originally was not running / not in background)
if launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil {

goToEventDetails(userInfo: launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any])

}
}

Where goToEventDetails is method which instantiates VC and pushed id, and [UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] is content of Notification.

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()
}

}

Push Notification acts different when app is closed

When the app is closed, and you press on a pushnoitification, the push notifictaion recieve messages don't get called. Instead, the application:didFinishLaunchingWithOptions: method gets called(as it is supposed to).

The trick is to check for notofication in the launchOptions dictionary.

if let remoteNotificationInfo = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
dealWithRemoteNotification(remoteNotificationInfo)
}

func dealWithRemoteNotification(_ userInfo:[AnyHashable : Any]) {
}

How to open VC on local notification when app is closed

So, your problem is when the app is killed or inactive and then when user tap the notification the reminder screen will show up, right?

Here's the case:
Notification shows (inactive/killed) -> tap notification -> splash -> reminder screen.

You should save your data that you want to show in notification. iOS will save any notification data in remoteNotification.

So, when user opens the app from inactive, the first thing that will be called is launchOption in AppDelegate.

Here's the example:

    if launchOptions != nil {
// get data from notificaioton when app is killed or incative
if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
// Do what you want, you can set set the trigger to move it the screen as you want, for your case is to reminder screen
}
}


Related Topics



Leave a reply



Submit