我的應用程序和UserNotifications框架出現問題。我有一個sendNotification的()主視圖控制器 - 功能如下:didReceiveRemoteNotification未被調用 - 爲什麼?
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")")
,它給了我假的,即使我已經收到通知。這怎麼可能?
在能力部分,通知和背景獲取的背景模式以及推送通知都會被激活。應用程序委託和視圖控制器都導入了用戶通知,添加爲UNUserNotificationCenterDelegate並且都有一個center.delegate = self代碼。爲什麼它不起作用?爲什麼我的didReceiveRemoteNotification()函數沒有被調用?
感謝您的幫助...
謝謝,但它也行不通...我不明白爲什麼didreceiveremotenotification不稱爲...:/ –
您是否收到通知?如是。那麼如果IOS使用哪個版本? – Maddy
最新的,10.3.3或其他東西...是的,我收到通知,這部分工作完美,事情是,當我將值保存在didReceiveRemoteNotification(與同步),然後直接啓動應用程序儘管Xcode,它說假即使它必須是真的,因爲我接收到通知...我不明白 –