didRegisterForRemoteNotificationsWithDeviceToken 没有 return deviceToken
didRegisterForRemoteNotificationsWithDeviceToken doesn't return deviceToken
我以这种方式通过 Parse 注册推送通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("RfSGbVes0FGIX1sfxTEb3iybVsKgKPrfDuxco3vC", clientKey: "3pFBMar6vO6iUJouqTMt4VJVKZaXUc6p9RgHzTep")
if application.applicationState != UIApplicationState.Background {
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.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
}
}
let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.badge = 0
println(installation.deviceToken) //deviceToken is nil
let standardUserDefaults = NSUserDefaults.standardUserDefaults()
standardUserDefaults.setObject(installation.deviceToken, forKey: "parseDeviceToken")
standardUserDefaults.synchronize()
delegate?.didFinishSettingToken()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackgroundWithBlock(nil)
}
当我 运行 应用程序时,我从 println(installation.deviceToken)
得到一个 nil 值
也许还有其他方法可以处理deviceToken
?
编辑:如果我停止应用程序并再次 运行 它,它会提供 deviceToken。我只有在第一次 运行 申请时才收到 deviceToken
。
根据苹果documentation:
The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver
notifications and stores the user’s response. The system does not
prompt the user during subsequent registration attempts. The user can
always change the notification preferences using the Settings app.
optional func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
application.registerForRemoteNotifications()
}
您还必须添加
UIApplication.sharedApplication().registerForRemoteNotificationSettings(settings)
对于 iOS 7 和 8 版本问题,请检查此 tutorial。
希望对您有所帮助。
我以这种方式通过 Parse 注册推送通知:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.setApplicationId("RfSGbVes0FGIX1sfxTEb3iybVsKgKPrfDuxco3vC", clientKey: "3pFBMar6vO6iUJouqTMt4VJVKZaXUc6p9RgHzTep")
if application.applicationState != UIApplicationState.Background {
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.trackAppOpenedWithLaunchOptionsInBackground(launchOptions, block: nil)
}
}
let userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.badge = 0
println(installation.deviceToken) //deviceToken is nil
let standardUserDefaults = NSUserDefaults.standardUserDefaults()
standardUserDefaults.setObject(installation.deviceToken, forKey: "parseDeviceToken")
standardUserDefaults.synchronize()
delegate?.didFinishSettingToken()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackgroundWithBlock(nil)
}
当我 运行 应用程序时,我从 println(installation.deviceToken)
也许还有其他方法可以处理deviceToken
?
编辑:如果我停止应用程序并再次 运行 它,它会提供 deviceToken。我只有在第一次 运行 申请时才收到 deviceToken
。
根据苹果documentation:
The first time you register your app’s preferred notification types, the system asks the user whether your app should be allowed to deliver notifications and stores the user’s response. The system does not prompt the user during subsequent registration attempts. The user can always change the notification preferences using the Settings app.
optional func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
application.registerForRemoteNotifications()
}
您还必须添加
UIApplication.sharedApplication().registerForRemoteNotificationSettings(settings)
对于 iOS 7 和 8 版本问题,请检查此 tutorial。
希望对您有所帮助。