【问题标题】:didReceiveRemoteNotification not called - why?didReceiveRemoteNotification 未调用 - 为什么?
【发布时间】:2017-05-28 09:13:52
【问题描述】:

我的应用和 UserNotifications 框架有问题。我有一个带有 sendNotification() 函数的主视图控制器,如下所示:

let content = UNMutableNotificationContent()
        content.title = "Water Reminder"
        content.body = "What about a glass of water?"
        content.sound = UNNotificationSound.default()

        let testTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: false)

        let identifier = Int(arc4random_uniform(999999999))

        let request = UNNotificationRequest(identifier: "\(identifier)", content: content, trigger: testTrigger)

        center.add(request, withCompletionHandler: {
            (error) in
            if let error = error {
                print("Didn't add notification request. \(error)")
            }

        })

触发器仅用于测试。好吧,30 秒后,我收到了通知。到那时,一切都很好。问题是:当它收到提醒时,它应该调用应用程序委托中的 didReceiveRemoteNotification() 函数,但它没有。这是我的功能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")
}

所以,我收到了通知,在didFinishLaunchingWithOptions()函数中插入了这行代码:

print("\(UserDefaults.standard.bool(forKey: "didReceiveRemoteNotification")")

它给了我错误,即使我收到了通知。这怎么可能?

在功能部分中,通知和后台获取的后台模式以及推送通知被激活。应用程序委托和视图控制器都导入了 UserNotifications,添加为 UNUserNotificationCenterDelegate 并且都有一个 center.delegate = self 代码之一。为什么它不起作用?为什么我的 didReceiveRemoteNotification() 函数没有被调用?

感谢您的帮助...

【问题讨论】:

    标签: ios swift notifications nsusernotification


    【解决方案1】:

    添加这一行。

    UserDefaults.standard.synchronize()
    

    From Apple Docs:因为这个方法是周期性自动调用的,所以只能在等不及自动同步的情况下才使用这个方法

    **更新:** 对于 IOS 10 使用 UserNotifications framework

    (1) 导入 UserNotifications 框架

    (2) 在AppDelegate.swift中添加协议UNUserNotificationCenterDelegate

    (3) 在didFinishLaunchingWithOptions 启用/禁用功能

    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
    
    }
    application.registerForRemoteNotifications()
    

    (4) 用于设备令牌

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
        let deviceTokenAsString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenAsString)
    
    
    }
    

    (5) 如果出错

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    
            print(error)
    }
    

    如果收到通知,应该调用的委托是:

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
        completionHandler(UIBackgroundFetchResult.noData)
    
    UserDefaults.standard.set(true, forKey: "didReceiveRemoteNotification")
    UserDefaults.standard.synchronize()
    
    
    }
    

    【讨论】:

    • 谢谢,但它也不起作用...我不明白为什么不调用 didreceiveremotenotification...:/
    • 你收到通知了吗?如是。那么你用的是哪个版本的IOS呢?
    • 最新的,10.3.3 什么的......是的,我收到了通知,这部分工作完美,问题是当我将值保存在 didReceiveRemoteNotification (同步)然后直接启动应用程序虽然 Xcode,但它说是假的,即使它必须是真的,因为我确实收到了通知......我不明白
    • 在didRegisterForRemoteNotificationsWithDeviceToken 和didFailToRegisterForRemoteNotificationsWithError 中添加一条带有成功和失败消息的警报。它将显示这些委托是否被调用。
    • 查看更新的答案可能是因为 IOS 版本,即 ios>10
    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 2020-06-30
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多