`didReceive` function is not called when user click on notification, when app is in kill state

40 Views Asked by At

When My app is in kill/terminate state and device received notification and user clicked on that notification, UNUserNotificationCenter function didReceive is not called.

...
@objc class AppDelegate: MCNotificationDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
       ....
       self.initNotification()
       ....
...

My MCNotificationDelegate file

class MCNotificationDelegate: FlutterAppDelegate {

   /// initialised UNUserNotificationCenter from didFinishLaunchingWithOptions in AppDelegate
   public func initNotification() {
        DispatchQueue.global().async {
            UNUserNotificationCenter.current().delegate = self
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

 override public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let notitification: UNNotification = response.notification

        let userInfo = notitification.request.content.userInfo

        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            // Code to be executed after 2 seconds
            UserDefaults.standard.set("\(userInfo)", forKey: "sfmc_didReceive")
        }

      completionHandler()
    }

override public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

  if #available(iOS 14.0, *) {
                completionHandler([.banner, .list, .sound])
            } else {
                completionHandler([.alert])
                // Fallback on earlier versions
            }

}

}

I have try lot of solution.

  1. Store received notification payload in UserDefault and try to fetch them but no success.
  2. Added alert in function, but not showing
  3. This working properly when app is in background & foreground
1

There are 1 best solutions below

3
narso310 On

I'm not too familiar with Flutter... Does FlutterAppDelegate implement UNNotificationCenterDelegate?

I ran into this same issue. In my case, I wrote an extension to one of my classes that I wanted to handle the notification tap and implemented UNUserNotificationCenterDelegate directly in that extension, and made sure to instantiate that class in my AppDelegate.didFinishLaunchingWithOptions(). If you don't do this, your app won't receive any taps on notifications when the app is killed (not backgrounded or active).

MyNotificationManager.swift

extension MyNotificationManager: UNUserNotificationCenterDelegate {
    // Called when the user taps on the notification
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) { 

         // Handle notification tap here 
    }
}

AppDelegate.swift

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

// Instantiate MyNotificationManager here, otherwise notifications tapped while the app
// is terminated won't be handled
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            _ = MyNotificationManager()
        }
}

Hope that helps!