Parameter - Setting device icon

I am trying to set a device token in parsing. The official documentation gives the following code for 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 converted the code as shown below, but I get 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()

      

The following error appears 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 someone help me why this is so? I am very confused because I just converted the original C object code to swift.

Thanks in advance.

+3


source to share


4 answers


I just set my Parse notifications without any problem. The line where you have the error is exactly the same as mine, so I don't think that's the problem. 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)
}

      

+3


source


The error indicated that the type was invalid:

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

      



Just change the data from String ("deviceToken") to Array (["deviceToken"]).

0


source


Not sure what the problem is before starting, but dumping the class and re-creating in parsing seems to fix it. Very strange..

0


source


Use this code for quick 3.0

let installation = PFInstallation.current()
installation.setObject(Token, forKey: "deviceToken")

if(installation.badge != 0)
{
  installation.badge = 0
}
installation.saveEventually()

      

0


source







All Articles