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 trial
Dee Greene
8,508 PointsParse.com Push Notifications
I want to send push notifications in my app once a user receives a message. Right now I cannot get any notifications to show for a user within the app. If send a push it shows in my push logs on parse but "pushes sent" always shows 0. Even when I create a push from the dashboard I still get 0 pushes sent. I know this can be a complicated task so any help would be much appreciated!
UPDATE
This is my code where I am sending the push.
- (void)uploadMessage {
NSData *fileData;
NSString *fileName;
NSString *fileType;
// check if image or video
if (self.image != nil) {
// if image, shrink it
// future: detect size of iphone and resize accordingly
UIImage *newImage = [self resizeImage:self.image toWidth:320.0f andHeight:480.0f];
fileData = UIImagePNGRepresentation(newImage);
fileName = @"image.png";
fileType = @"image";
} else {
// video
fileData = [NSData dataWithContentsOfFile:self.videoFilePath];
fileName = @"video.mov";
fileType = @"video";
}
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// error = [[NSError alloc] init]; // to check for errors... comment to run!!!!
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occured" message:@"Please try sending your message again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
} else {
// file is on Parse.com!
PFObject *message = [PFObject objectWithClassName:@"Messages"];
[message setObject:file forKey:@"file"];
[message setObject:fileType forKey:@"fileType"];
[message setObject:self.imageOrVideoRecipients forKey:@"recipientIds"];
[message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
[message setObject:[[PFUser currentUser] username] forKey:@"senderEmail"];
NSString *firstName = [[PFUser currentUser] objectForKey:@"firstName"];
NSString *lastName = [[PFUser currentUser] objectForKey:@"lastName"];
[message setValue:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"senderFullName"];
[message setValue:[[PFUser currentUser] objectForKey:@"companyName"] forKey:@"senderCompany"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occured" message:@"Please try sending your message again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
} else {
// everything was successful!
//push code
PFQuery *pushQuery = [PFInstallation query];
PFQuery *recipients = [PFUser query];
[recipients whereKey:@"objectId" containedIn:self.imageOrVideoRecipients];
NSLog(@"%@", recipients);
//send to msg recipients
[pushQuery whereKey:@"user" matchesQuery:recipients];
NSLog(@"query %@", pushQuery);
NSLog(@"sent to: %@", self.imageOrVideoRecipients);
// Send push notification to query
NSString *companyName = [[PFUser currentUser] objectForKey:@"companyName"];
NSString *firstName = [[PFUser currentUser] objectForKey:@"firstName"];
NSString *lastName = [[PFUser currentUser] objectForKey:@"lastName"];
NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
NSDictionary *data = @{
@"alert" : [NSString stringWithFormat:@"%@ : %@", fullName,companyName],
@"badge" : @"Increment",
@"sound" : UILocalNotificationDefaultSoundName
};
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our Installation query
[push setData:data];
[push sendPushInBackground];
//end push code
// dont do anything other than display a success message...user is already taken to inbox tab
[self reset];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Message sent successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
}];
}
2 Answers
Stepan Ulyanin
11,318 PointsFollow this documentation and it will work, I have just implemented push notifications the other day with Parse: https://www.parse.com/tutorials/ios-push-notifications
Dee Greene
8,508 PointsThank you! It works! My problem was I selected Ad Hoc and App Store when I first created my provisioning profile.
Dee Greene
8,508 PointsOnly issue I have now is that the notifications are not consistent. Sometimes they go through and sometimes they don't. Is this a known bug or an error within my code i should look into?
Stepan Ulyanin
11,318 PointsWell, push notifications are notorious for being inconsistent, I believe I even read it in apple documentation. Server is a very big part of it and if something wrong then your notifications won't be delivered, however I never experienced inconsistencies with Parse, 100% of my notifications were delivered, you probably would want to go through your code once again
Dee Greene
8,508 Pointsupdated my question to include my code
Stepan Ulyanin
11,318 PointsSo my recent code for PFPush looks like this:
//SEND PUSH NOTIFICATIONS
//query on installation
PFQuery *pushQuery = [PFInstallation query];
if (self.taggedUsers.count != 0) {
for (PFUser *user in self.taggedUsers) {
[pushQuery whereKey:@"user" equalTo:user];
}
// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery withMessage:[NSString stringWithFormat:@"%@ tagged you!", [[PFUser currentUser] username]]];
}
//------------------------------
Judging by your code I am pretty sure it is fine and it should work but I don't know why your notifications don't go through the server:/
landonferrier
25,097 PointsDee, I recently released a social network template with this functionality. It might be of some use to your product: Lion - An Advanced Social Network Template
If you have any questions, feel free to ask!
Dee Greene
8,508 PointsDee Greene
8,508 PointsCan you help with this Gabe Nadel ?