Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNUserNotificationCenterDelegate is not working when app is removed from background

UNUserNotificationCenterDelegate is not working when is remove from background. It is working fine while app stays in foreground and background.

Here is code

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }

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


        let rootViewController = self.window!.rootViewController as! UINavigationController
        let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let profileViewController = mainStoryboard.instantiateViewController(withIdentifier: "SampleViewController") as! SampleViewController
        rootViewController.pushViewController(profileViewController, animated: true)

        completionHandler()
    }
}

When app is remove from background delegate method is not called. Please help me out

like image 994
Prashant Sharma Avatar asked Oct 15 '25 15:10

Prashant Sharma


1 Answers

When adding UserNotifications framework to a project, here are 5 things to check:

1) If you are dealing with remote notifications, be sure you have enabled that in your project's capabilities settings page of Xcode.

See figure 1

If that gives you an error, you probably need to log into your apple developer account and create a key with push notification credentials. The end result gives you a .p8 file to download. The creation of this file will clear up the error. The file itself is needed on the server that sends push notifications to your app/device. If you need help with this, check out this tutorial.

2) Add import line to the top of your AppDelegate file

import UserNotifications

3) Add UNUserNotificationCenterDelegate to your AppDelegate class declaration line.

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {...

4) Set UNUserNotificationCenter delegate to self in the application:didFinishLaunchingWithOptions method.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    //...
    UNUserNotificationCenter.current().delegate = self
    //...
    return true
}

5) Add your delegate functions (if you want to use them)

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    NSLog("Application delegate method userNotificationCenter:didReceive:withCompletionHandler: is called with user info: %@", response.notification.request.content.userInfo)
    //...
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    NSLog("userNotificationCenter:willPresent")
    //...
    completionHandler([.alert])
}

Hope that helps.

like image 125
Douglas Putnam Avatar answered Oct 18 '25 21:10

Douglas Putnam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!