parsing – 设置设备标记

我正在尝试在parsing中设置设备标记。 官方文档给出下面的代码来做到这一点

官方文件(目标C)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // Store the deviceToken in the current installation and save it to Parse. PFInstallation *currentInstallation = [PFInstallation currentInstallation]; [currentInstallation setDeviceTokenFromData:deviceToken]; currentInstallation.channels = @[ @"global" ]; [currentInstallation saveInBackground]; } 

我已经转换了下面的代码,但收到一个错误。

我的代码(Swift)

 func application( applcation: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) { println(deviceToken) let currentInstallation = PFInstallation.currentInstallation() currentInstallation .setDeviceTokenFromData(deviceToken) currentInstallation .setObject(PFUser.currentUser(), forKey: "owner") currentInstallation .addUniqueObject("Test", forKey: "channels") currentInstallation .save() 

运行我的代码时收到以下错误:

  Break on warnBlockingOperationOnMainThread() to debug. 2014-11-13 03:44:01.306 Meetr[8855:2084537] Error: invalid type for key deviceToken, expected array, but got string (Code: 111, Version: 1.4.2) sent 

有人能帮助我,为什么这是? 我很困惑,因为我只是将原始的目标C代码转换为swift。

提前致谢。

我只是设置我的parsing通知没有任何问题。 你有错误的地方就是我的,所以我不认为这就是问题所在。 以下是我在App Delegate中的所有内容:

  func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool { // Override point for customization after application launch. //ENABLE PUSH NOTIFICATIONS 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!) { //Store the deviceToken in the current installation and save it to Parse. let currentInstallation: PFInstallation = PFInstallation.currentInstallation() currentInstallation.setDeviceTokenFromData(deviceToken) currentInstallation.saveInBackground() } func application(application: UIApplication!, didReceiveRemoteNotification userInfo: NSDictionary!) { PFPush.handlePush(userInfo) } 

错误表示types错误:

 Error: invalid type for key deviceToken, expected array, but got string (Code: 111, Version: 1.4.2) 

只需将您的数据从String(“deviceToken”)更改为Array([“deviceToken”])。

不知道是什么问题开始,但是,放弃类和重新创buildparsing似乎修复它。 很奇怪..

使用此代码swift 3.0

 let installation = PFInstallation.current() installation.setObject(Token, forKey: "deviceToken") if(installation.badge != 0) { installation.badge = 0 } installation.saveEventually()