通知在iOS上发表评论的用户

时间:2015-10-14 21:10:02

标签: ios objective-c facebook parse-platform push-notification

我有一个带有Facebook功能和Parse后端的iOS应用程序。在社交媒体应用程序中,有一个ActivityViewController,显示:喜欢,评论,关注者。该代码通过Parse连接到我的js云代码以进行推送通知。

虽然如果userA对用户B的帖子发表评论(通知用户B),我的评论通知效果很好,但我想为" UserB添加回复评论的通知"活动部分(其中通知用户A在他们的帖子上有来自UserB的回复)。可能解释得非常糟糕,但基本上与评论部分中的面子书通知非常类似,但现在通知只是单方面的。我一直在努力,但我似乎只是让用户通知自己他们回复了帖子。

我在实现这一目标的最佳方式上遇到了一些麻烦。应该正确设置常量,云代码和缓存。代码如下。

     - (BOOL)textFieldShouldReturn:(UITextField *)textField {
// Comment Portion, works fine
      NSString *trimmedComment = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (trimmedComment.length != 0 && [self.photo objectForKey:kPAPPhotoUserKey]) {
            PFObject *comment = [PFObject objectWithClassName:kPAPActivityClassKey];
            [comment setObject:trimmedComment forKey:kPAPActivityContentKey]; // Set comment text
            [comment setObject:[self.photo objectForKey:kPAPPhotoUserKey] forKey:kPAPActivityToUserKey]; // Set toUser
            [comment setObject:[PFUser currentUser] forKey:kPAPActivityFromUserKey]; // Set fromUser
            [comment setObject:kPAPActivityTypeComment forKey:kPAPActivityTypeKey];
            [comment setObject:self.photo forKey:kPAPActivityPhotoKey];

    PFACL *ACL = [PFACL ACLWithUser:[PFUser currentUser]];
            [ACL setPublicReadAccess:YES];
            [ACL setWriteAccess:YES forUser:[self.photo objectForKey:kPAPPhotoUserKey]];
            comment.ACL = ACL;

            [[PAPCache sharedCache] incrementCommentCountForPhoto:self.photo];

            // Show HUD view
            [MBProgressHUD showHUDAddedTo:self.view.superview animated:YES];

            // If more than 5 seconds pass since we post a comment, stop waiting for the server to respond
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(handleCommentTimeout:) userInfo:@{@"comment": comment} repeats:NO];

            [comment saveEventually:^(BOOL succeeded, NSError *error) {
                [timer invalidate];

                if (error && error.code == kPFErrorObjectNotFound) {
                    [[PAPCache sharedCache] decrementCommentCountForPhoto:self.photo];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Could not post comment", nil) message:NSLocalizedString(@"This photo is no longer available", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                    [alert show];
                    [self.navigationController popViewControllerAnimated:YES];
                }

                [[NSNotificationCenter defaultCenter] postNotificationName:PAPPhotoDetailsViewControllerUserCommentedOnPhotoNotification object:self.photo userInfo:@{@"comments": @(self.objects.count + 1)}];

                [MBProgressHUD hideHUDForView:self.view.superview animated:YES];
                [self loadObjects];
            }];
        }

        //Reply portion is getting me stuck
        NSString *trimmedReply = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (trimmedReply.length != 0 && [self.photo objectForKey:kPAPPhotoUserKey]) {
            PFObject *reply = [PFObject objectWithClassName:kPAPActivityClassKey];
            [reply setObject:trimmedReply forKey:kPAPActivityContentKey]; // Set reply text
            [reply setObject:[self.photo objectForKey:kPAPPhotoUserKey] forKey:kPAPActivityFromUserKey]; //CHANGED TO FromUser
            [reply setObject:[PFUser currentUser] forKey:kPAPActivityToUserKey]; // Changed ToUser
            [reply setObject:kPAPActivityTypeReply forKey:kPAPActivityTypeKey];
            [reply setObject:self.photo forKey:kPAPActivityPhotoKey];

            PFACL *ACL = [PFACL ACLWithUser:[PFUser currentUser]];
            [ACL setPublicReadAccess:YES];
            [ACL setWriteAccess:YES forUser:[self.photo objectForKey:kPAPPhotoUserKey]];
            reply.ACL = ACL;

            [[PAPCache sharedCache] incrementReplyCountForPhoto:self.photo];

            // Show HUD view
            [MBProgressHUD showHUDAddedTo:self.view.superview animated:YES];

            // If more than 5 seconds pass since we post a reply, stop waiting for the server to respond
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(handleReplyTimeout:) userInfo:@{@"reply": reply} repeats:NO];

            [reply saveEventually:^(BOOL succeeded, NSError *error) {
                [timer invalidate];

                if (error && error.code == kPFErrorObjectNotFound) {
                    [[PAPCache sharedCache] decrementReplyCountForPhoto:self.photo];
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Could not post reply", nil) message:NSLocalizedString(@"This photo is no longer available", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                    [alert show];
                    [self.navigationController popViewControllerAnimated:YES];
                }

                [[NSNotificationCenter defaultCenter] postNotificationName:PAPPhotoDetailsViewControllerUserRepliedOnPhotoNotification object:self.photo userInfo:@{@"replies": @(self.objects.count + 1)}];

                [MBProgressHUD hideHUDForView:self.view.superview animated:YES];
                [self loadObjects];
            }];
        }
        ///^^^

        [textField setText:@""];
        return [textField resignFirstResponder];
    }

0 个答案:

没有答案