Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlberto Jauregui
2,253 PointsHow can implement push notifications on Ribbit app?
I follow the tutorial in Parse to implement push notifications, but it is for everyone, and I want to only send a push notification when someone send you a picture/video, help me.
3 Answers
Dimitris Sotiris Tsolis
28,141 PointsYou could try the following steps
Step 1. Go to the LoginViewController.m and add the following code after login is completed
[[PFInstallation currentInstallation] setObject:[PFUser currentUser].objectId forKey:@"userId"];
[[PFInstallation currentInstallation] saveInBackground];
Similar with SignupViewController.m after sign up is completed.
Step 2. Go to InboxViewController.m and after [PFUser logout]; add this
[[PFInstallation currentInstallation] removeObjectForKey:@"userId"];
[[PFInstallation currentInstallation] saveInBackground];
So far, you have a user-device match. So even if user have multiple devices, he will get the notification in every device.
Step 3. Go to CameraViewController.m and after the succesful save of the message, you can do something like this
NSString *msg = [NSString stringWithFormat:@"%@ send you a new message!", [message objectForKey:@"senderName"]];
NSDictionary *data = @{@"alert": @"You have a new message!",
@"badge": @"Increment",
@"msgId": message.objectId };
PFQuery *query = [PFInstallation query];
[query whereKey:@"userId" containedIn:self.recipients];
// Send the notification.
PFPush *push = [[PFPush alloc] init];
[push setQuery:query];
[push setData:data];
[push sendPushInBackground];
Passing the object's id into the payload, gives the receipients the ability to open the message directly while opening the app. It's not necessary but it might be useful for future projects.
PS. you have to enable "push notifications from client" via settings at your parse.com account
Guillaume Maka
Courses Plus Student 10,224 PointsCreate a channel for each user (like the objectId of the user as a channel name), subscribe to it then each time someone send a photo/video collect the recipient user objectId and send it a push through the channel.
I don't know if it's the best solution (1 user = 1 channel ... 10000 user = 10000 channels).
Alberto Jauregui
2,253 PointsI don't know if it's a good idea, I mean, make so many channels is not going to affect the performance or is irrelevant?
Guillaume Maka
Courses Plus Student 10,224 PointsI don't think its affect the performance, the fact of creating a channel is not expensive, I mean if you take the Parse Free plan for example you get
Requests: 1 million/month Pushes: 1 million/month Burst Limit: 20/second
A background fetch operation check on the server for a given interval (like every hour for instance), that mean
1 fetch = 1 request at each 1 hour => 720 request/month for one device. => 720 000 request/month for 1000 devices.
However the channel idea do a request only when needed.
In conclusion I think the solution should be design according to your budget.
Evan Hurst
13,266 PointsI did this using the background fetch capabilities in iOS 7: basically when your app wakes up in the background on the iOS configured interval ( I always use the minimumFetchInterval), check for messages on the server, if a new message is available, download the message and queue a local notification for the user to indicate a new message is available. You have the flexibility here to either just notify the user of a new message, or provide some more detailed information about the message received.
Take a look at the following methods - implemented in the App Delegate:
(void) application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
and the local notification class: UILocalNotification
Alberto Jauregui
2,253 PointsJust what I was looking for.
Thank you.