当应用程序处于终止状态时,当用户单击通知时,不会调用`didReceive`函数

问题描述 投票:0回答:1

当我的应用程序处于终止/终止状态并且设备收到通知并且用户单击该通知时,不会调用 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
            }

}

}

我已经尝试了很多解决方案。

  1. 将收到的通知负载存储在 UserDefault 中并尝试获取它们,但没有成功。
  2. 功能中添加了提醒,但不显示
  3. 当应用程序处于后台和前台时,此功能可以正常工作
ios swift objective-c push-notification apple-push-notifications
1个回答
0
投票

我对 Flutter 不太熟悉...

FlutterAppDelegate
实现了
UNNotificationCenterDelegate
吗?

我遇到了同样的问题。就我而言,我为我的一个类编写了一个扩展,我想要处理通知点击并直接在该扩展中实现

UNUserNotificationCenterDelegate
,并确保在我的
AppDelegate.didFinishLaunchingWithOptions()
中实例化该类。如果您不这样做,当应用程序被终止(未在后台或处于活动状态)时,您的应用程序将不会收到任何通知点击。

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 open the detailed result properly
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            _ = MyNotificationManager()
        }
}
© www.soinside.com 2019 - 2024. All rights reserved.