標題解釋發生了什麼。應用程序正在運行但已最小化。通知會顯示給用戶。當用戶點擊通知時,應用程序被帶到前臺並且方法didReceiveRemoteNotification被輸入。但是,如果用戶單擊應用程序圖標而不是恢復應用程序,則方法didReceiveRemoteNotification不會執行。didReceiveRemoteNotification沒有被調用,除非申請從通知中恢復
這裏是我的代碼:
在didFinishLaunchingWithOptions:
//FOR ALLOWING NOTIFICATIONS
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let deviceTokenStr = convertDeviceTokenToString(deviceToken)
GlobalVariables.UserDefaults.setValue(deviceTokenStr,forKey: "Push_Notification_Reg_ID");
RZLog.VIP("The PushNotification Device Token is: \(deviceTokenStr)")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
RZLog.Error(error.description)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
RZLog.Debug("A NEW NOTIFICATION HAS BEEN RECEIVED")
//handle
completionHandler(UIBackgroundFetchResult.NoData)
}
}
什麼是你通知的有效載荷?它有'內容可用:'? – Paulw11
我剛剛添加「內容可用」:「1」,大聲笑,它的工作。但現在它被調用兩次,以防我按下通知 –