我已经研究了几个小时,并且感觉非常接近。我正在尝试使用Parse发送推送通知。我希望它能像这样工作:当用户A向用户B发送消息时,用户B也会收到一条推送通知,上面显示“用户A的新消息”。我已经成功地使用Parse网站向使用该应用程序的设备发送推送通知,但是在应用程序内部(当用户发送消息)无法成功发送推送通知给接收用户的设备。
推送通知显然已经成功发送,因为我的Parse帐户显示了我发送的消息。然而,没有消息实际到达预定设备,推送通知列表显示每个推送通知的0个订阅者。
并且我可以点击其中一个以查看详细信息。 此外,我正在使用发布/生产配置文件和证书。
以下是我用于在用户A向用户B发送消息后发送推送通知的代码-
message
对象是已上传到 Parse 的消息,而 messageRecipients 则是正在发送消息的用户:
```
// 使用 Parse 发送推送通知
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"user" containedIn:messageRecipients];PFPush *push = [[PFPush alloc] init]; [push setQuery:pushQuery]; [push setMessage:@"New Message from UserA!"]; [push sendPushInBackground]; ```
// Send Push Notification to recipients
NSArray *messageRecipients = [message objectForKey:@"recipientIds"];
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"owner" containedIn:messageRecipients];
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setMessage:[NSString stringWithFormat: @"New Message from %@!", [PFUser currentUser].username]];
[push sendPushInBackground];
这里是我的AppDelegate.m相关方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Parse setApplicationId:@"[This is where my app Id is]"
clientKey:@"[This is where client Id is]"];
[self customizeUserInterface];
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[PFPush storeDeviceToken:deviceToken];
[PFPush subscribeToChannelInBackground:@""];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Did fail to register for push, %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[PFPush handlePush:userInfo];
}
我还在Parse.com论坛上发布了一篇帖子:https://parse.com/questions/sending-a-push-notification-from-one-user-to-another-user
有什么我忽略或做错的地方吗?
编辑:我现在能够在我的Parse账户中看到订阅者,但我实际上没有在我的设备上收到推送通知。当我尝试从Parse网站发送推送通知测试时也是如此。