Ответ 1
Мои поиски продолжали возвращаться сюда, но здесь ничего не объяснялось. Итак, вот как я получил свою работу:
В моем AppDelegate.m у меня есть:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
//save the installation
PFInstallation *currentInstallation = [PFInstallation currentInstallation];
currentInstallation[@"installationUser"] = [[PFUser currentUser]objectId];
// here we add a column to the installation table and store the current user’s ID
// this way we can target specific users later
// while we’re at it, this is a good place to reset our app’s badge count
// you have to do this locally as well as on the parse server by updating
// the PFInstallation object
if (currentInstallation.badge != 0) {
currentInstallation.badge = 0;
[currentInstallation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
// only update locally if the remote update succeeded so they always match
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
NSLog(@"updated badge");
}
}];
}
} else {
[PFUser logOut];
// show the signup screen here....
}
}
в viewController, где я отправляю push, у меня есть:
myViewController.h
@property (nonatomic, strong) NSMutableArray *recipients; // declare the array we'll use to store our recipients
myViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.recipients = [[NSMutableArray alloc] init]; // initialize the array we'll use to hold our recipients
}
// in another part of the code (not shown here) we set up a tableView with all of the current user friends in it
// when the user taps a row in that tableView we add or remove the selected friend from our recipients list
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.recipients addObject:user.objectId]; // user selected a recipient, add them to the array
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.recipients removeObject:user.objectId]; // user de-selected a recipient, remove them from the array
}
}
- (void)uploadMessage
{
UIImage *newImage = [self resizeImage:self.image toWidth:640.0f andHeight:960.0f];
NSData *fileData= UIImageJPEGRepresentation(newImage, 1.0);
NSString *fileName= @"image.jpg";;
NSString *fileType= @"image";
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
PFObject *message = [PFObject objectWithClassName:@"Messages"];
[message setObject:file forKey:@"file"];
[message setObject:fileType forKey:@"fileType"];
[message setObject:self.recipients forKey:@"recipientIds"];
// self.recipients is an NSMutableArray of the objectIds for each
// user the message will go to
[message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
[message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
// Handle error here with an alert…
}
else {
// Everything was successful! Reset UI… do other stuff
// Here’s where we will send the push
//set our options
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
@"Ne messages available!!", @"alert",
@"Increment", @"badge",
nil];
// Now we’ll need to query all saved installations to find those of our recipients
// Create our Installation query using the self.recipients array we already have
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"installationUser" containedIn:self.recipients];
// Send push notification to our query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setData:data];
[push sendPushInBackground];
}
}];
}
}];
}