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.
- Store received notification payload in UserDefault and try to fetch them but no success.
- Added alert in function, but not showing
- This working properly when app is in background & foreground
I'm not too familiar with Flutter... Does
FlutterAppDelegateimplementUNNotificationCenterDelegate?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
UNUserNotificationCenterDelegatedirectly in that extension, and made sure to instantiate that class in myAppDelegate.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
AppDelegate.swift
Hope that helps!