在解析ios中更新安装表中的设备令牌

时间:2015-10-10 11:42:43

标签: ios objective-c parse-platform ios9

我想使用iOS在解析时更新安装表中的设备令牌。 为了保存设备令牌,我做了:

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];

我想更新此设备令牌。我知道更新令牌我必须使用rest API,即https://api.parse.com/1/installations。如何更新行,因为我也没有安装ID。

请提供正确的语法。

2 个答案:

答案 0 :(得分:1)

在AppDelegate中的didRegisterForRemoteNotificationsWithDeviceToken方法中编写以下代码。

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

对于频道中的注册用户,请使用“登录屏幕”中的以下代码

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);
    }];
}

有关详细信息,请参阅此链接https://www.parse.com/docs/ios/guide#push-notifications-installations

答案 1 :(得分:0)

AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken方法中,将deviceToken设置为安装表并将设备令牌保存到NSUserDefaults,如下所示:

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

在登录或注册时,像这样设置用户:

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

更新:

您需要添加安装表。将列userID添加到安装中,然后获取包含当前用户userID的查询安装表。 您可以参考此https://www.parse.com/questions/retrieve-objectid-from-installation-table链接以便更好地理解 希望它有所帮助:)