gpt4 book ai didi

ios - didReceiveRemoteNotification 未调用 - 为什么?

转载 作者:行者123 更新时间:2023-11-30 12:25:52 25 4
gpt4 key购买 nike

我的应用程序和 UserNotifications 框架出现问题。我有一个带有 sendNotification() 函数的主视图 Controller ,如下所示:

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 秒后,我收到了通知。到那时,一切都很好。问题是:当它收到提醒时,它应该调用应用程序委托(delegate)中的 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")")

即使我已经收到通知,它也会给我错误的信息。这怎么可能?

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

感谢您的帮助...

最佳答案

添加此行。

UserDefaults.standard.synchronize()

From Apple Docs:由于该方法会定期自动调用,因此仅当您无法等待自动同步时才使用该方法

**更新:**对于 IOS 10,请使用 UserNotifications 框架

(1)导入UserNotifications框架

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

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

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

}
application.registerForRemoteNotifications()

(4) 对于设备 token

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()


}

关于ios - didReceiveRemoteNotification 未调用 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44225606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com