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

11
我已经建立了一个类似于 Snapchat 的消息应用程序-一个用户可以向另一个用户发送图片。我正在尝试添加推送通知到应用程序中,以便当用户A向用户B发送消息时,用户B会收到一条来自“用户A”的推送通知。
我已经研究了几个小时,并且感觉非常接近。我正在尝试使用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网站发送推送通知测试时也是如此。 enter image description here


“owner” 是否存储在您的 Installation 对象中?您确定 [message objectForKey:@"recipientIds"] 返回正确的数组吗? - Lyndsey Scott
是的,我认为是这样的。我记录了数组,它返回了正确的数组。 - tagabek
“owner” 实际上在 Parse 网站上的数据浏览器安装表中吗? - Lyndsey Scott
它肯定在安装表中,类型是字符串。 - tagabek
@tagabek 你最终解决了这个问题吗?我也遇到了完全相同的问题。 - user379468
5个回答

4

我发现很多搜索结果都涉及到这里,但没有一篇能真正让我明白。因此,以下是我成功使它工作的方法:

在我的AppDelegate.m文件中,我有如下代码:

- (void)applicationDidBecomeActive:(UIApplication *)application
{

    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
        //save the installation
        PFInstallation *currentInstallation = [PFInstallation currentInstallation];
        currentInstallation[@"installationUser"] = [[PFUser currentUser]objectId];
        // here we add a column to the installation table and store the current user’s ID
        // this way we can target specific users later

        // while we’re at it, this is a good place to reset our app’s badge count
        // you have to do this locally as well as on the parse server by updating
        // the PFInstallation object
        if (currentInstallation.badge != 0) {
            currentInstallation.badge = 0;
            [currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    // Handle error here with an alert…
                }
                else {
                    // only update locally if the remote update succeeded so they always match
                    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
                    NSLog(@"updated badge");
                }
            }];
        }
    } else {

        [PFUser logOut];
        // show the signup screen here....
    }
}

在我发送推送的视图控制器中,我有以下内容:
myViewController.h
@property (nonatomic, strong) NSMutableArray *recipients; // declare the array we'll use to store our recipients

myViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.recipients = [[NSMutableArray alloc] init]; // initialize the array we'll use to hold our recipients
}


// in another part of the code (not shown here) we set up a tableView with all of the current user's friends in it
// when the user taps a row in that tableView we add or remove the selected friend from our recipients list
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    PFUser *user = [self.friends objectAtIndex:indexPath.row];

    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.recipients addObject:user.objectId]; //  user selected a recipient, add them to the array
    }
    else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.recipients removeObject:user.objectId]; //  user de-selected a recipient, remove them from the array
    }

}




- (void)uploadMessage 
{
    UIImage *newImage = [self resizeImage:self.image toWidth:640.0f andHeight:960.0f];
    NSData *fileData= UIImageJPEGRepresentation(newImage, 1.0);
    NSString *fileName= @"image.jpg";;
    NSString *fileType= @"image";

    PFFile *file = [PFFile fileWithName:fileName data:fileData];

    [file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
                // Handle error here with an alert…
        }
        else {
            PFObject *message = [PFObject objectWithClassName:@"Messages"];
            [message setObject:file forKey:@"file"];
            [message setObject:fileType forKey:@"fileType"];
            [message setObject:self.recipients forKey:@"recipientIds"];
                  // self.recipients is an NSMutableArray of the objectIds for each 
                  // user the message will go to 
            [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
            [message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
            [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error) {
                    // Handle error here with an alert…
                }
                else {
                    // Everything was successful! Reset UI… do other stuff
                    // Here’s where we will send the push
                    //set our options
                    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                          @“Ne messages available!!”, @"alert",
                                          @"Increment", @"badge",
                                          nil];

                    // Now we’ll need to query all saved installations to find those of our recipients 
                    // Create our Installation query using the self.recipients array we already have
                    PFQuery *pushQuery = [PFInstallation query];
                    [pushQuery whereKey:@"installationUser" containedIn:self.recipients];

                    // Send push notification to our query
                    PFPush *push = [[PFPush alloc] init];
                    [push setQuery:pushQuery];
                    [push setData:data];
                    [push sendPushInBackground];
                }
            }];
        }
    }];
}

@DelightedD0D [[PFUser currentUser]objectId] 获取到了空值。有什么想法吗 :( - Bhavesh Nayi
@BhaveshNai 你确定有用户登录吗?你调用了 [PFUser logInWithUsernameInBackground:@"myname" password:@"mypass"......] 吗? - Wesley Smith
@DelightedD0D 感谢您的回复。还有一个问题,如何发送多个用户通知,例如群组讨论。提前感谢 :) - Bhavesh Nayi
@BhaveshNai,请注意上述代码中的// self.recipients is an NSMutableArray of the objectIds for each注释。此代码将向一组收件人发送通知。请确保self.recipients是包含您想要发送消息的所有用户的解析对象ID的NSArray或NSMutableArray。 - Wesley Smith
@DelightedD0D 谢谢亲 :) - Bhavesh Nayi
显示剩余3条评论

3

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

当你设置owner的值时,你应该使用"[PFUser currentUser].objectId"而不是[PFUser currentUser]。后者会给你一个指向owner的指针,但在这种情况下我们需要一个字符串来设置推送查询。

当我们首次将owner设置为Installation时,我们应该像下面这样将objectId设置为owner的字符串,而不仅仅是[PFUser currentUser]。

[currentInstallation setObject:[PFUser currentUser].objectId forKey:@"owner"];

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


嘿,Nic,谢谢。当我将它更改为那样时,我收到了这个错误:“_Error: invalid type for key owner, expected *_User, but got string_”。 - tagabek
1
你遇到了这个错误是因为你试图将一个字符串赋值给非字符串列“owner”;你可以在解析中删除“owner”列并创建另一个列,例如“userId”,最重要的是,将其设置为字符串类型。并确保你的设备使用新的userId字符串保存PFInstallation。 - Nic Huang
我也卡在这里了:如果你创建了一个错误的数据类型字段,你需要在尝试插入新数据之前删除该列。 - Jason McDermott
我也遇到了困难,我在我的安装表中将用户ID存储为字符串,然后将其传递给我的Push查询。它似乎出现在在线推送浏览器中,但就像原始问题一样,显示没有发送任何内容。有没有办法测试Push查询? - user379468
@user379468,你是将确切的userId分配给了pushQuery还是只是[PFUser currentUser]?这是不同的。还有另一种测试方法。尝试从Parse推送控制台进行推送。选择segment并从您的安装中选择userId列,并将其“equals”到您的userId(要发送的用户)。 - Nic Huang

0

你尝试过在安装中存储所有者吗?

- (void)application:(UIApplication *)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation setObject:[PFUser currentUser] forKey:@"owner"];
    [currentInstallation saveInBackground];
}

来自:点击这里


0

根据您目前发布的内容,我不确定为什么您的代码无法正常工作。看起来似乎是您的安装表格中没有包含“所有者”列或者包含了与您的[message objectForKey:@"recipientIds"]数组中不同的值/类型的“所有者”列;但是,以防您需要/想要尝试备用方法,这里有另一种使用Parse从一个设备向另一个设备发送推送通知的方法:

首先订阅接收推送通知的用户/用户组到他们自己的唯一频道(除了主频道),例如:

[PFPush subscribeToChannelInBackground:taylors_channel];

然后从发送推送的设备中,将通道设置为接收者的通道:

PFPush *push = [[PFPush alloc] init];
[push setChannel:taylors_channel];
[push setMessage:[NSString stringWithFormat: @"New Message from %@!",  [PFUser currentUser].username]];
[push sendPushInBackground];

此外,您还可以使用[push setChannels:channel_array]为多个对等方设置相同的通道或一次向多个通道发送消息;


0
进一步解释djshiow的回答,一旦您将当前安装保存为这样。
PFQuery * pushQuery = [PFInstallation query];
PFUser * userReceivingPush; 
[pushQuery whereKey:@"owner" equalTo:userReceivingPush]; 

NSString * alert = [NSString stringWithFormat:@"You have a new message from %@!", [PFUser currentUser].username];
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                  alert, @"alert",
                                  @"default", @"sound",
                                  @"Increment", @"badge",
                                  nil];
[PFPush sendPushDataToQueryInBackground:pushQuery withData:data block:^(BOOL succeeded, NSError *error) {
                if (!error) {

                }
                else {
                }
            }];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接