当我的应用程序处于终止/终止状态并且设备收到通知并且用户单击该通知时,不会调用 UNUserNotificationCenter 函数
didReceive
。
...
@objc class AppDelegate: MCNotificationDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
...
我的
MCNotificationDelegate
文件
class MCNotificationDelegate: FlutterAppDelegate {
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
}
}
}
我已经尝试了很多解决方案。
我对 Flutter 不太熟悉...
FlutterAppDelegate
实现了UNNotificationCenterDelegate
吗?
我遇到了同样的问题。就我而言,我为我的一个类编写了一个扩展,我想要处理通知点击并直接在该扩展中实现
UNUserNotificationCenterDelegate
,并确保在我的 AppDelegate.didFinishLaunchingWithOptions()
中实例化该类。如果您不这样做,当应用程序被终止(未在后台或处于活动状态)时,您的应用程序将不会收到任何通知点击。
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
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Instantiate MyNotificationManager here, otherwise notifications tapped while the app
// is terminated won't open the detailed result properly
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
_ = MyNotificationManager()
}
}