我的应用程序和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,它说假即使它必须是真的,因为我接收到通知...我不明白 –