Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Nic Huang
PLUS
Nic Huang
Courses Plus Student 10,573 Points

Ribbit with push notification

I try to take Ribbit further and add more features to it. I can now successfully send push notification (server - to client) from my Parse console. Devices can receive it. However, my client-to-client push has some issues. When I'm sending a photo to one of my friends, I'm sending notification as well. The notification is sent successful, but none of devices receive it. Parse console always shows 0 recipients and subscribers. I don't understand why. Hope someone with the experience can give me some help. Thanks in advance.

The below are my client to client push code. I think I have everything ready, but devices still can't receive it......

 // set PFInstallation in appDelegate

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

 // handle recipient push after sending message

    NSArray *megRecipients = [message objectForKey:@"recipientIds"]; 

    PFQuery *pushQuery = [PFInstallation query];
    [pushQuery whereKey:@"owner" containedIn:megRecipients];

    PFPush *push = [[PFPush alloc] init];
    [push setQuery:pushQuery];
    [push setMessage:[NSString stringWithFormat: @"%@ send you a new message",  [PFUser currentUser].username ]];
    [push sendPushInBackground];

4 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Great discussion in here! I missed this one but am so glad you all got it working. Paul Dunahoo, I might turn this into an add-on workshop because this is really cool and would complement the course nicely.

This is a really great idea. Is there a way for students to submit video tutorials that might complement courses like this?

Alberto Huerdo
Alberto Huerdo
2,650 Points

Hi Ben, di you had time to work on this?

I came here to ask the exact same thing. I have been working on adding push notifications successfully with Parse for the past 4 hours, and I am at the exact same stage as you.

I'll keep working on it and update if I have anything new.

EDIT: Are you testing with multiple devices? I think that you might not be able to send a push notification to a user account that is not currently signed into the app. I have not tested this on multiple devices, but will do so and check back.

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

And I just posted on StackOverflow: http://stackoverflow.com/questions/20626513/sending-a-push-notification-from-one-user-to-another-user-with-parse

Some links that are leading me on the right track:

https://parse.com/docs/ios/api/Classes/PFPush.html

https://parse.com/questions/sending-push-notifications-to-a-specific-user

https://www.parse.com/docs/push_guide#sending-queries/iOS

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

Hey Taylor,

I know what's wrong with our target push. When We are setting value to owner, we should use "[PFUser currentUser].objectId", instead of [PFUser currentUser]. The latter gives you a pointer to user, but we need a string to set Push query.

my Target push now works successfully, and Yes , I'm testing with multiple devices. I also left the answer to your question on StackOverflow. Please give me a thumb-up if it works. I'm new to StackOverflow :)

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

Hey Nic, Thanks.

Do you mean changing this:

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

to this:

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

I tried that and I get an error like this: Error: invalid type for key owner, expected *_User, but got string

This means that a string is not a valid object to use in the "setObject:" method.

Can you help me out by explaining your solution in a bit more detail? Thanks.

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

You have that error because you trying to assign a string into a non-string column "owner";

You can drop the "owner" column in parse and created another column like "userId", and most importantly, give it a string type. And make sure your device save PFInstallation with new userId string.

Thanks Nic. I think I have it working now. I really appreciate it!

Too bad it isn't part of the course.

Did you get it to work. Please post code. Karma is watching the webb.

Yep. Nic Huang figured it out. There is a bunch of code and explanations over at:

http://stackoverflow.com/questions/20626513/sending-a-push-notification-from-one-user-to-another-user-with-parse