我正在尝试在解析中设置设备 token 。官方文档给出了如下代码来实现这一点
官方文档( objective-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
谁能帮我看看这是为什么?我很困惑,因为我只是将原始 objective-c 代码转换为 swift。
提前致谢。
最佳答案
我刚刚设置了我的 Parse 通知,没有任何问题。你有错误的那一行正是我的错误所在,所以我认为这不是问题所在。这是我在 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)
}
关于ios - 解析 - 设置设备 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901004/