我正在通过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
。
答案 0 :(得分:2)
根据苹果http://www.mvps.org/dmcritchie/excel/event.htm:
首次注册应用的首选通知类型时,系统会询问用户是否应允许您的应用发送 通知并存储用户的响应。系统没有 在后续注册尝试期间提示用户。用户可以 始终使用“设置”应用更改通知首选项。
optional func application(_ application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
application.registerForRemoteNotifications()
}
您还必须添加
UIApplication.sharedApplication().registerForRemoteNotificationSettings(settings)
对于iOS 7和8版本问题,请检查此documentation。
希望它有所帮助。