2

I would like to set up a response, via Push Notification, in my app.

User A sends User B a message.

User B opens the app (through the push) to a new page.

User B sends User A a response (as a push).

Right now I can open to the page, but I'm not sure how to get the data I need.

Here's what I get from Parse from the first push:

userInfo: {
    aps =     {
        alert = "demo says HELLO WORLD";
    };

In this case, demo is the username of the user that sent the first push. I'd like to get that, so that the app for User B knows who to send the reply to.

Here's my Push Code:

    PFPush *push = [[PFPush alloc] init];
    [push setQuery:pushQuery];
    [push setMessage:[NSString stringWithFormat:@"%@ says HELLO WORLD", [PFUser currentUser].username]];
    [push sendPushInBackground];

2 Answers 2

2

You will have to handle this from AppDelegate. There are 2 ways to receive data from the push notification.

//Receive Push Notification when the app is active in foreground or background
- (void)application:(UIApplication *)application 
           didReceiveRemoteNotification:(NSDictionary *)userInfo {

  if(userInfo){
   //TODO: Handle the userInfo here
   }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Get the push notification when app is not open
    NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

    if(remoteNotif){
        [self handleRemoteNotification:application userInfo:remoteNotif];
    }

    return YES;
}

-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{

    if(userInfo){
       //TODO: Handle the userInfo here
    }
}

Updated answer below:-

In that case, I think you should use setData instead if setMessage.

NSString * message =[NSString stringWithFormat:@"%@ says HELLO WORLD", [PFUser currentUser].username];
NSString * userID =  @"userid"; //TODO: set Your userID here

NSMutableDictionary * dataDict = [[NSMutableDictionary alloc]init];
[dataDict setObject:message forKey:@"message"];
[dataDict setObject:userID forKey:@"userID"];

PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setData:dataDict];
[push sendPushInBackground];
Sign up to request clarification or add additional context in comments.

2 Comments

Right. I have that set up, but what I showed above is all I am getting in userInfo. How do I get the objectId of the user that sent the push?
See the Update above.
0

A notification payload can contain dictionary entries in order to provide additional data to the receiving application. This is documented in the Apple Local and Push Notification Programming Guide.

When you generate your Push message in Parse, you can add the objectID of the sending user, so that your payload would look like this

userInfo: {
    aps =     {
        alert = "demo says HELLO WORLD";
    },
    sendingUserObject:142Xyd23
};

Your Cloud Code would be something like this -

var pushMsg=user.get("username")+" says HELLO WORLD";
var pushData={alert: pushMsg, sendingUserObject: user.id};
var pushMap= {};
pushMap["data"]=pushData;
var deviceQuery=new Parse.Query("installations"); 
deviceQuery.equalTo("currentUser",destination);
deviceQuery.exists("deviceToken");
pushMap["where"]=deviceQuery;
Parse.Push.send(pushMap);

Then when you receive the notification in your app you can retrieve the sending object id from the userInfo dictionary -

NSString senderID=userInfo[@"sendingUserObject"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.