当PFObject保存到我的Parse后端时,如何收到通知?

时间:2015-08-27 02:52:47

标签: ios parse-platform

我已按照快速入门指南进行解析: https://parse.com/apps/quickstart#parse_data/mobile/ios/native/existing

我使用此代码保存PFObject:

PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
testObject[@"Bookmarks"] = @"Restore Successful";
[testObject saveInBackground];

下面是我的PFObject保存到Parse后端时的样子: enter image description here

我希望在发生这种情况时收到一些分类通知。像电子邮件,文本或推送通知。任何有用的东西。

2 个答案:

答案 0 :(得分:0)

Parse推荐的方法是使用Cloud Code。在main.js文件中,configure it一次添加以下函数:

Parse.Cloud.afterSave("Posts", function(request) {

    // Gets called when a new object is added to the 'Posts' class.
    // From here you can proceed to send push notifications     

});

将新对象添加到指定类后触发的afterSave方法。如果您想管理,无论是否应保存对象,请查看beforeSave

答案 1 :(得分:0)

我刚刚放在一起的简单示例:例如:

-(void)saveLesson {
    lesson = [PFObject objectWithClassName:@"lessons"];
    PFObject *photo = [PFObject objectWithClassName:@"photoslessons"];
    [photo setObject:[PFUser currentUser] forKey:@"userId"];
    [photo setObject:self.photoFile forKey:@"lessonimage"];
    [photo setObject:self.smallPhotoFile forKey:@"smalllessonimage"];
    [photo setObject:self.thumbnailFile forKey:@"thumblessonimage"];
    [photo setObject:self.originalFile forKey:@"originallessonimage"];
    PFACL *publicACL = [PFACL ACL];
    [publicACL setPublicWriteAccess:YES];
    [publicACL setPublicReadAccess:YES];
    photo.ACL = publicACL;
    self.photoPostBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
    }];
    [photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"Photo uploaded");
            NSNumber *userCount = [[NSNumber alloc] initWithInt:0];
            lesson[@"textBody"] = [[[self contentView] descriptionText] text];
            lesson[@"title"] =  [[[self contentView] title] text];
            lesson[@"likes"] =  userCount;
            lesson[@"times_viewed"] =  userCount;
            lesson[@"featured"] =  @"false";
            lesson[@"userId"] = [PFObject objectWithoutDataWithClassName:@"_User" objectId:[PFUser currentUser].objectId];
            lesson[@"lessonimage"]= photo;
            [lesson saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    PFQuery *query = [PFQuery queryWithClassName:@"activesessions"];
                    [query whereKey:@"teacherid" equalTo:[PFUser currentUser]];
                    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
                        if (object!=nil) {
                            [object setObject:lesson forKey:@"lessonid"];
                            [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                                if (succeeded) {
                                    [(NSHLiveStudentProfileViewController*)self.navigationController.viewControllers[0] thisIsAHack];
                                    [[self navigationController] popToRootViewControllerAnimated:true];
                                }
                            }];
                        }}];
                } else if (error) {
                    NSString *  alertTitle = @"Error Saving Lesson";
                    UIAlertController * alert =  [UIAlertController alertControllerWithTitle:alertTitle message:@"Please make sure to include both a title and description." preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }];
                    [alert addAction:ok];
                    [self presentViewController:alert animated:YES completion:^{}];
                }
            }];
        } else {
            NSLog(@"Photo failed to save: %@", error);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
            [alert show];
        }
        [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
    }];
}

所以,这是很多代码,但这是我们要做的事情来帮助你,让我们添加一个方法给自己发一封电子邮件:

首先,下载加载pod mailgun:

然后这样做:

 Mailgun *mailgun = [Mailgun clientWithDomain:kMailGunAPIClientDomain apiKey:kMailGunAPIKey];

此外,注册一个邮件枪帐户,并发送电子邮件给自己

然后在上面的代码中,每当和块中的“成功”保存对象时,如下所示:

首先,明显改变这一点:

 @"Peter Pan <peterpan.notarealemailaddress@gmail.com>" 

到你的邮箱地址,然后填写需要你的API密钥的部分,以及你从MailGun获得的邮件枪是便宜的,一旦你设置了邮件枪帐户,这就立即为你工作(我不工作mailgun,我是一个快乐的用户)

[lesson saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
       if (succeeded) {

         [mailgun sendMessageTo:@"Peter Pan <peterpan.notarealemailaddress@gmail.com>"
                            from:@"Excited User <apeterpan.notarealemailaddress@gmail.com>>"
                         subject:@"Mailgun is awesome!"
                            body:@"and life is great!"];
热潮!,

这是MailGun广告连播:https://github.com/rackerlabs/objc-mailgun

注册免费帐户:http://www.mailgun.com/

测试一下,关于这一点的好处是,它是自动触发器,你可以在我放在这里的电子邮件调用中放入你想要的任何内容,你还需要获得AFNetworking:

在此处获取:https://github.com/AFNetworking/AFNetworking

此方法不需要任何类型的JavaScript云编码,您甚至可以将电子邮件正文替换为您要发送给自己的信息。如果您想向自己发送刚刚触发您的方法的用户ID,那么您就可以发送该电子邮件。

所以你有这个:

PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
testObject[@"Bookmarks"] = @"Restore Successful";
[testObject saveInBackground];

将其更改为:

PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
testObject[@"Bookmarks"] = @"Restore Successful";
[testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        NSLog(@"successfully SAVED!");
    }
}];

这是您的自定义保存功能:

-(void)customSaveFunction {
    PFObject *testObject = [PFObject objectWithClassName:@"TestObject"];
    testObject[@"Bookmarks"] = @"Restore Successful";
    [testObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
             NSLog(@"successfully SAVED!");
             Mailgun *mailgun = [Mailgun clientWithDomain:kMailGunAPIClientDomain apiKey:kMailGunAPIKey];
            [mailgun sendMessageTo:@"Peter Pan <peterpan.notarealemailaddress@gmail.com>"
                                from:@"Excited User <apeterpan.notarealemailaddress@gmail.com>>"
                             subject:@"Mailgun is awesome!"
                                body:@"and life is great!"];
        }
    }];
 }

我展示了保存课程示例,您可以在电子邮件中发送多封包含多个信息的电子邮件,以获取不同的保存触发器,但此代码对我来说没用,这仅用于主要测试目的,因此自定义保存我在这几句话上面发布的函数就是你自己如何做,或者只是将相同的代码放在你的viewcontroller中或者保存你的对象。