使用Parse从一个用户向另一个用户发送推送通知

tag*_*bek 11 xcode objective-c push-notification ios parse-platform

我已经构建了一个类似于Snapchat的消息传递应用程序 - 一个用户可以发送另一个用户图片.我正在尝试向应用添加推送通知,以便当从UserA向UserB发送消息时,UserB会收到"来自UserA的新消息"的推送通知.

我一直在研究这几个小时,我觉得我很亲密.

我正在尝试使用Parse发送推送通知.我希望它的工作方式如下:当UserA向UserB发送消息时,还会向UserB发送一条推送通知,其中显示"来自UserA的新消息".我成功地使用Parse网站向使用该应用程序的设备发送推送通知,但是无法在应用程序内(当用户发送消息时)向接收用户的设备成功发送推送通知.

推送通知显然是成功发送的,因为我的Parse帐户显示了我发送的消息.但是,没有消息实际到达目标设备,推送通知列表显示每个推送通知的0个订阅者.

我可以点击其中一个来查看详细信息.

此外,我正在使用分发/生产供应配置文件和证书.

这是我在UserA向UserB发送消息后发送推送通知的代码 - message对象是已上传到Parse的消息,messageRecipients是消息发送到的用户:

// 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];
Run Code Online (Sandbox Code Playgroud)

这是我的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];
}
Run Code Online (Sandbox Code Playgroud)

我还在Parse.com论坛上提交了一篇文章:https://parse.com/questions/sending-a-push-notification-from-one-user-to-another-user

有什么东西我错过了或做错了吗?

编辑:我现在能够在我的Parse帐户中看到订阅者,但我实际上并没有在我的设备上收到推送通知.当我尝试从Parse网站发送推送通知测试时也是如此. 在此输入图像描述

Nic*_*ang 3

我想我已经有了答案。我遇到了同样的问题,我刚刚解决了。

当您为所有者设置值时,应使用“[PFUser currentUser].objectId”,而不是[PFUser currentUser]。后者为您提供了一个指向所有者的指针,但我们需要一个字符串来在这种情况下设置推送查询。

当我们第一次将所有者设置为 Installation 时,我们应该将 objectId 设置为所有者的字符串,而不是像下面这样设置 [PFUser currentUser]。

[currentInstallation setObject:[PFUser currentUser].objectId forKey:@"owner"];
Run Code Online (Sandbox Code Playgroud)

之后我们可以将所有者(字符串)设置为我们的pushQuery。