如何通过 Parse 向特定用户发送推送通知?
How to send push notifications to a specific user through Parse?
我已经通过 Parse 和 Apple 设置了推送通知,但我似乎无法找到一种直接的方式将推送通知发送给其他用户。
就我而言,我想在用户向他们发送好友请求时向用户发送通知。这是我用来保存请求服务器的代码:
//get current user
PFQuery *query2 = [PFUser query];
[query2 whereKey:@"username" equalTo:pendingFriendName];
PFUser *userTo = (PFUser *)[query2 getFirstObject];
PFQuery *query = [PFQuery queryWithClassName:@"Follow"];
[query whereKey:@"from" equalTo:[PFUser currentUser]];
// execute the query
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFObject *follow = [PFObject objectWithClassName:@"Follow"];
[follow setObject:[PFUser currentUser] forKey:@"from"];
[follow setObject:userTo forKey:@"to"];
[follow saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(succeeded)
{
NSLog(@"success!");
[friendAddedLabel setText:@"Friend Added!"];
}
else
{
NSLog(@"error");
}
}];
}
}];
此外,有没有办法将发送请求的用户的直接引用发送给收到朋友请求的用户,以便在用户点击通知而不是去时可以快速访问信息通过另一个查询?
首先,您必须将当前用户存储到安装中 class,然后使用 PFInstallation 查询和 PFPush 发送推送通知。
//save the user into installtion class.
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);
}];
}
//Then send push notification to particular user.
PFQuery *queryInstallation = [PFInstallation query];
[queryInstallation whereKey:@"user" equalTo:user];
PFPush *push = [[PFPush alloc] init];
[push setQuery:queryInstallation];
NSDictionary * dic = @{@"alert" : text,@"badge" : @"Increment" , @"sender" : [PFUser currentUser].objectId ,@"MediaId" : mediaObject.objectId};
[push setData:dic];
[push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error != nil && !succeeded)
{
NSLog(@"SendPushNotification send error.");
}
else
{
NSLog(@"SendPushNotification send success.");
}
}];
我已经通过 Parse 和 Apple 设置了推送通知,但我似乎无法找到一种直接的方式将推送通知发送给其他用户。
就我而言,我想在用户向他们发送好友请求时向用户发送通知。这是我用来保存请求服务器的代码:
//get current user
PFQuery *query2 = [PFUser query];
[query2 whereKey:@"username" equalTo:pendingFriendName];
PFUser *userTo = (PFUser *)[query2 getFirstObject];
PFQuery *query = [PFQuery queryWithClassName:@"Follow"];
[query whereKey:@"from" equalTo:[PFUser currentUser]];
// execute the query
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFObject *follow = [PFObject objectWithClassName:@"Follow"];
[follow setObject:[PFUser currentUser] forKey:@"from"];
[follow setObject:userTo forKey:@"to"];
[follow saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if(succeeded)
{
NSLog(@"success!");
[friendAddedLabel setText:@"Friend Added!"];
}
else
{
NSLog(@"error");
}
}];
}
}];
此外,有没有办法将发送请求的用户的直接引用发送给收到朋友请求的用户,以便在用户点击通知而不是去时可以快速访问信息通过另一个查询?
首先,您必须将当前用户存储到安装中 class,然后使用 PFInstallation 查询和 PFPush 发送推送通知。
//save the user into installtion class.
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);
}];
}
//Then send push notification to particular user.
PFQuery *queryInstallation = [PFInstallation query];
[queryInstallation whereKey:@"user" equalTo:user];
PFPush *push = [[PFPush alloc] init];
[push setQuery:queryInstallation];
NSDictionary * dic = @{@"alert" : text,@"badge" : @"Increment" , @"sender" : [PFUser currentUser].objectId ,@"MediaId" : mediaObject.objectId};
[push setData:dic];
[push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error != nil && !succeeded)
{
NSLog(@"SendPushNotification send error.");
}
else
{
NSLog(@"SendPushNotification send success.");
}
}];