【发布时间】:2015-12-04 12:26:25
【问题描述】:
标题解释了正在发生的事情。应用程序正在运行但已最小化。通知显示给用户。当用户点击通知时,应用程序被带到前台并输入方法 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)
}
}
【问题讨论】:
-
通知的有效负载中有什么?有
content-available:吗? -
我刚刚添加了“content-available”:“1”,哈哈,它起作用了。但现在它会被调用两次,以防我按下通知