解析-设置设备令牌 [英] Parse - Setting device token
问题描述
我正在尝试在解析中设置设备令牌.官方文档为此提供了以下代码
I am trying to set a device token in parse. The official documentation gives the following code for doing this
官方文档(目标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];
}
我已将代码转换如下,但收到错误.
I have converted the code as below but receive an error.
我的代码(快速)
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()
运行代码时收到以下错误:
I receive the following error when running my code:
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.
Can anybody help me as to why this is? I'm very confused as I have simply converted the original objective C code to swift.
谢谢.
推荐答案
我刚刚设置了Parse通知,没有任何问题.错误所在的行正是我的错误所在,因此我认为这不是问题所在.这是我在App Delegate中拥有的所有内容:
I just set up my Parse notifications without any issues. The line where you have the error is exactly how I have mine, so I don't think that's what the problem is. Here's everything I have in 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)
}
这篇关于解析-设置设备令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!