Parse.com 注册推送通知
Parse.com register for Push Notifications
我正在尝试使用 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 - 没有t 工作)
我目前使用的代码也给了我 2 个警告,这是(我怀疑的)推送通知无法在每个 iDevice 上工作的原因。
警告 1:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:43:25: 'UIRemoteNotificationType' was deprecated in iOS 8.0: Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
第 43 行:
let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
警告 2:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:44:25: 'registerForRemoteNotificationTypes' was deprecated in iOS 8.0: Please use registerForRemoteNotifications and registerUserNotificationSettings: instead
第 44 行:
application.registerForRemoteNotificationTypes(types)
现在我对这些警告感到困惑,因为我从 Parse.com 文档中复制了代码 - 那么我做错了什么?似乎这个 else 子句为 运行 旧版本 iOS 的设备提供了向后兼容性。
在我的 Parse.com 推送控制台中,我也遇到了一些错误:
如有任何帮助,我们将不胜感激。
在didRegisterForRemoteNotificationsWithDeviceToken
中尝试添加这行代码。
installation.saveInBackground()
为了消除警告,我建议使用此代码(为了方便和重复使用,我将其放在静态函数中)
static func askForPushNotifications(){
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}
我正在尝试使用 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 - 没有t 工作)
我目前使用的代码也给了我 2 个警告,这是(我怀疑的)推送通知无法在每个 iDevice 上工作的原因。
警告 1:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:43:25: 'UIRemoteNotificationType' was deprecated in iOS 8.0: Use UIUserNotificationType for user notifications and registerForRemoteNotifications for receiving remote notifications instead.
第 43 行:
let types : UIRemoteNotificationType = [.Badge, .Alert, .Sound]
警告 2:
/Users/ds/code/rp-iOS/rp/AppDelegate.swift:44:25: 'registerForRemoteNotificationTypes' was deprecated in iOS 8.0: Please use registerForRemoteNotifications and registerUserNotificationSettings: instead
第 44 行:
application.registerForRemoteNotificationTypes(types)
现在我对这些警告感到困惑,因为我从 Parse.com 文档中复制了代码 - 那么我做错了什么?似乎这个 else 子句为 运行 旧版本 iOS 的设备提供了向后兼容性。
在我的 Parse.com 推送控制台中,我也遇到了一些错误:
如有任何帮助,我们将不胜感激。
在didRegisterForRemoteNotificationsWithDeviceToken
中尝试添加这行代码。
installation.saveInBackground()
为了消除警告,我建议使用此代码(为了方便和重复使用,我将其放在静态函数中)
static func askForPushNotifications(){
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()
}