我尝试使用Parse.com推送通知服务向我的iOS应用添加推送通知,但我遇到了一些设备未接收通知的问题。
当前代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("*****", clientKey: "*****")
// Register for Push Notitications
if application.applicationState != UIApplicationState.Background {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced in iOS 7).
// In that case, we skip tracking here to avoid double counting the app-open.
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var pushPayload = false
if let options = launchOptions {
pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
}
if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}
}
if application.respondsToSelector("registerUserNotificationSettings:") {
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}else {
let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
application.registerForRemoteNotificationTypes(types)
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
// Store the deviceToken in the current Installation and save it to Parse
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
这似乎适用于某些设备(在同事的iPhone 5上测试过 - 在我的老板和iPhone 6上测试过 - 没有工作)
我目前使用的代码也给了我2个警告,这是(我怀疑的)推送通知的原因不适用于每个iDevice。
警告1:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:43:25:' UIRemoteNotificationType'在iOS 8.0中已弃用:使用UIUserNotificationType进行用户通知,使用registerForRemoteNotifications接收远程通知。
第43行:
let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
警告2:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:44:25:' registerForRemoteNotificationTypes'在iOS 8.0中已弃用:请使用registerForRemoteNotifications和registerUserNotificationSettings:而不是
第44行:
application.registerForRemoteNotificationTypes(types)
现在我对这些警告感到困惑,因为我从Parse.com文档中复制了代码 - 所以我做错了什么?看起来这个else子句为运行旧版iOS的设备提供了向后兼容性。
在我的Parse.com Push控制台中,我也遇到了一些错误:
非常感谢任何帮助。
答案 0 :(得分:1)
在didRegisterForRemoteNotificationsWithDeviceToken
中尝试添加此行代码。
installation.saveInBackground()
为了摆脱我建议使用此代码的警告(为了方便和重用的能力,我把它放在静态函数中)
static func askForPushNotifications(){
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}