如何将PFUser与PFInstallations动态关联以发送推送通知?

时间:2014-01-27 19:52:59

标签: ios iphone parse-platform

根据Parse guide使用针对推送通知的高级定位,我了解为了将PFInstallations当前用户相关联,我应该按照以下方式:

PFInstallation *installation = [PFInstallation currentInstallation];
installation[@"user"] = [PFUser currentUser];
[installation saveInBackground];

我的问题是,如何做到这一点"在飞行中"对于其他用户(即不是当前用户)。例如,我有一个Message对象,我想发送给用户Sally,Alex和Ben:如何能够根据用户的objectIds获得这些用户的PFInstallations,然后设置他们的PFInstallation&# 39; s这样

//Sally, Alex and Ben's PFInstallations:
[installation setObject:YES forKey:message.objectId];

...所以当我发出消息时,只有Sally,Alex和Ben会收到推送通知他们收到了我的消息?

谢谢!

2 个答案:

答案 0 :(得分:6)

以下是我设法做到的。请注意,recipientIDs是我发送推送通知的用户列表(确切地说是用户objectIds)。

if (recipientIDs.count > 0){

        //Find the users that were just sent the message
        PFQuery *userQuery = [PFUser query];
        [userQuery whereKey:@"objectId" containedIn:recipientIDs];

        //Find the devices associated with these users
        PFQuery *pushQuery = [PFInstallation query];
        [pushQuery whereKey:@"user" matchesQuery:userQuery];

        //Send push to these users
        PFPush *push = [[PFPush alloc] init];
        NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSString stringWithFormat:@"New message from %@", [PFUser currentUser].username], @"alert",
                              @"Increment", @"badge",
                              nil];
        [push setQuery:pushQuery]; // Set our Installation query
        [push setData:data];
        [push sendPushInBackground];
    }

答案 1 :(得分:0)

不确定这是否可行 - 文档说

  

有效的PFInstallation只能通过[PFInstallation]实例化   currentInstallation]因为所需的标识符字段是   只读的。

https://parse.com/docs/ios/api/Classes/PFInstallation.html