2017-05-28 164 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,它說假即使它必須是真的,因爲我接收到通知...我不明白 –

相關問題