gurwinder singh
gurwinder singh

Reputation: 31

Update device token in installation table in parse ios

I want to update device token in installation table on parse using iOS. To save a device token I did:

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setDeviceTokenFromData:(NSData*)[AppHelper userDefaultsForKey:@"token"]];
[currentInstallation setObject:[PFUser currentUser].objectId forKey:@"user"];
NSArray *channels = [NSArray arrayWithObjects:@"AnyString",nil];
currentInstallation.channels=channels;
[currentInstallation saveInBackground];

I want to update this device token. I know to update token I have to use rest API i.e. https://api.parse.com/1/installations. How to update the row as I also don't have installation id.

Please provide proper syntax.

Upvotes: 1

Views: 759

Answers (2)

Sushil Sharma
Sushil Sharma

Reputation: 2361

In AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken method, set deviceToken to installation table and save device token to NSUserDefaults ,like this:

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    currentInstallation.channels = @[@"global"];
    [currentInstallation saveInBackground];
    [[NSUserDefaults standardUserDefaults]setObject:deviceToken forKey:@"deviceToken"];

And on login or signup ,set user like this :

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
[currentInstallation setObject:[PFUser currentUser] forKey:@"User"];
[currentInstallation setDeviceTokenFromData:[[NSUserDefaults standardUserDefaults] valueForKey:@"deviceToken"]];
currentInstallation.channels = @[@"global"];
[currentInstallation saveInBackground];

UPDATE:

you need to make additions to installation table. Add column userID to installation and then get query installation table with current user's userID. You can refer this https://www.parse.com/questions/retrieve-objectid-from-installation-table link for better understanding
Hope it helps :)

Upvotes: 0

Hardik Shekhat
Hardik Shekhat

Reputation: 1878

Write below code in didRegisterForRemoteNotificationsWithDeviceToken method in AppDelegate .

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    PFInstallation *currnentInstallation = [PFInstallation currentInstallation];
    [currnentInstallation setDeviceTokenFromData:deviceToken];
    [currnentInstallation saveInBackground];
}

For Register user in channels use below code in Login Screen

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if ([PFUser currentUser].objectId)
{
    currentInstallation[@"user"] = [PFUser currentUser];

    currentInstallation.channels = @[[NSString stringWithFormat:@"user_%@",[PFUser currentUser].objectId]];
    NSLog(@"Saving Installation channel = %@",currentInstallation.channels);

    [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
    {
           NSLog(@"Current installation updated: Error: %@",error);
    }];
}

For more details , refer this link https://www.parse.com/docs/ios/guide#push-notifications-installations

Upvotes: 1

Related Questions