Sending a Push Notification from one user to another user with Parse

Viewed 15993

I have built a messaging app that is similar to Snapchat - one user can send another user pictures. I am trying to add push notifications to the app, so that when a message is sent from UserA to UserB, UserB receives a push notification of "New Message from UserA".

I have been researching this for hours now, and I feel like I am very close.

I am trying to use Parse to send push notifications. I would like it to work like this: When UserA sends UserB a message, UserB is also sent a push notification that says "New Message from UserA". I was successfully able to use the Parse website to send a push notification to devices using the application, but am NOT able to successfully send a push notification from within the app (when a user sends a message) to the receiving user's device.

The push notifications are apparently being sent successfully, as my Parse account shows the messages that I have sent. However, no messages actually reaches the intended device and the list of push notifications shows 0 subscribers for each push notification.

And I can click on one of those to see the details.

Also, I am using a Distribution/Production Provisioning Profile & Certificate.

Here's the code that I am using the send the push notification after UserA would send a message to UserB - the message object is the message that has been uploaded to Parse, and the messageRecipients are the users that the message is being sent to:

// 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];

Here are my AppDelegate.m related methods:

- (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];
}

I have also submitted a post on the Parse.com forums: https://parse.com/questions/sending-a-push-notification-from-one-user-to-another-user

Is there something that I am missing or doing wrong?

EDIT: I am now able to see subscribers in my Parse account, but I am not actually receiving the push notifications on my device. The same goes for when I try to send a push notification test from the Parse website. enter image description here

5 Answers
Related