2017-05-28 115 views
0

我的应用程序和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()函数没有被调用?

感谢您的帮助...

回答

0

添加此行。

UserDefaults.standard.synchronize() 

From Apple Docs:因为这个方法每隔一段时间自动调用,使用此方法仅如果你不能等待自动同步

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

(1)导入用户通知框架

(2)添加协议UNUserNotificationCenterDelegate in AppDelegate.swift

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

在情况通知接收delgate应该被称为:

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


} 
+0

谢谢,但它也行不通...我不明白为什么didreceiveremotenotification不称为...:/ –

+0

您是否收到通知?如是。那么如果IOS使用哪个版本? – Maddy

+0

最新的,10.3.3或其他东西...是的,我收到通知,这部分工作完美,事情是,当我将值保存在didReceiveRemoteNotification(与同步),然后直接启动应用程序尽管Xcode,它说假即使它必须是真的,因为我接收到通知...我不明白 –

相关问题