Reputation: 5525
I am trying to set a device token in parse. The official documentation gives the following code for doing this
Official Documentation(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];
}
I have converted the code as below but receive an error.
My code (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()
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
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.
Thanks in advance.
Upvotes: 3
Views: 2901
Reputation: 1
Use this code for swift 3.0
let installation = PFInstallation.current()
installation.setObject(Token, forKey: "deviceToken")
if(installation.badge != 0)
{
installation.badge = 0
}
installation.saveEventually()
Upvotes: 0
Reputation: 5525
Not sure what the issue was to begin with but, dropping the class and re-creating in parse appeared to fix it. Very strange..
Upvotes: 0
Reputation: 675
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)
}
Upvotes: 3
Reputation: 139
The error indicated that the type is wrong:
Error: invalid type for key deviceToken, expected array, but got string (Code: 111, Version: 1.4.2)
Just change your data from String ("deviceToken") to Array (["deviceToken"]).
Upvotes: 0