Update device token in installation table in parse ios

非 Y 不嫁゛ 提交于 2019-12-11 14:49:34

问题


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.


回答1:


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




回答2:


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 :)



来源:https://stackoverflow.com/questions/33053592/update-device-token-in-installation-table-in-parse-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!