代码之家  ›  专栏  ›  技术社区  ›  Vyacheslav

为什么没有触发userNotificationCenter didReceive?

  •  0
  • Vyacheslav  · 技术社区  · 6 年前

    这是我代码的一部分。我想用 UNUserNotificationCenter UNUserNotificationCenterDelegate

    此代码捕获应用程序处于前台状态时的通知事件。但是 "didReceive" 不是为后台状态激发的。

    func application(_ application: UIApplication,
                             didFinishLaunchingWithOptions launchOptions:
                [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
                UNUserNotificationCenter.current().delegate = self
        application.registerForRemoteNotifications()
        return true
        }    
            func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        willPresent notification: UNNotification,
                                        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
            {
                print("willPresent") /// This works
                completionHandler([.alert, .badge, .sound])
            }
            func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        didReceive response: UNNotificationResponse,
                                        withCompletionHandler completionHandler: @escaping () -> Void) {
                print("didReceive") /// This doesn't work
                completionHandler()
            }
    

    但如果我不使用 UNUserNotificationCenter.current().delegate = self 委托方法在后台正确激发。

    func application(_ application: UIApplication,
                         didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                         fetchCompletionHandler completionHandler: @escaping FetchCompletionHandler) {
            print("didReceiveRemoteNotification... \(userInfo)")
    }
    

    3 回复  |  直到 6 年前
        1
  •  2
  •   quellish    6 年前

    application(_:didReceiveRemoteNotification:fetchCompletionHandler:) 当应用程序收到静默推送通知时调用。静默推送通知可以在中传递。应用程序处于后台状态时,iOS会唤醒应用程序以执行用户不可见的后台处理。

    无声推送通知具有 content-available

    静默推送通知不应包含警报、徽章或声音。无声推送并不意味着对用户可见,它只是对应用程序的提示,即新的远程内容可用。

    移除 提供的内容 来自推送通知负载的标志将导致iOS将其作为常规通知处理。将调用用户通知中心委托方法,而不是 但您的应用程序将无法执行由通知触发的后台处理。

    this tool

        2
  •  1
  •   Hasti Ranjkesh    6 年前

    这就是我在应用程序中处理推送通知的方式。我认为您需要实现所有这些方法来处理前台和后台模式下的所有iOS版本。

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        if #available(iOS 10.0, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound,.alert,.badge], completionHandler: { (granted, error) in
                if error == nil {
                    DispatchQueue.main.async {
                        application.registerForRemoteNotifications()
                    }
                }
            })
        }
        else {
            let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
            let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
            application.registerUserNotificationSettings(pushNotificationSettings)
            application.registerForRemoteNotifications()
        }
    
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to get token; error: \(error)")
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert,.sound])
         //do sth
    }
    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
         //do sth
    }
    
    // for iOS < 10
    func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    }
    
    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    }
    
        3
  •  -1
  •   nitin.agam    6 年前

    • willPresent:当你的应用在前台时收到通知时,会调用此方法。如果应用程序在后台,则此方法将不会调用。

    • didRecieve:当用户单击通知时调用此方法。

    在后台状态下,当用户单击通知时,只调用“didRecieve”。