Save Parse notification - iOS

时间:2015-10-31 00:05:44

标签: ios swift uitableview parse-platform notifications

I want to have all the notification sent in a UITableView. Because the user can just delete the notification (in notification center) without launching the app, I will have to store all notification in Parse database. So how can I programmatically save the notification I'm sending in a database. I don't want to send the notification and then enter manually the notification in the database. Hope it's clear. Thanks

1 个答案:

答案 0 :(得分:0)

这是在发送推送通知之前将推送通知保存到解析类的代码。

    PFObject *messageActivity = [PFObject objectWithClassName:kPAPActivityClassKey];
    [messageActivity setObject:kPAPActivityTypeReceivedMessage forKey:kPAPActivityTypeKey];
    [messageActivity setObject:[PFUser currentUser] forKey:kPAPActivityFromUserKey];
    [messageActivity setObject:self.user forKey:kPAPActivityToUserKey];
    [messageActivity setObject:textField.text forKey:kPAPActivityContentKey];

    PFACL *messageACL = [PFACL ACLWithUser:[PFUser currentUser]];
    [messageACL setPublicReadAccess:YES];
    [messageACL setWriteAccess:YES forUser:self.user];
    messageActivity.ACL = messageACL;

    // 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:@{@"message": messageActivity} repeats:NO];

    [messageActivity save];
    [timer invalidate];

    PFObject *sentMessageActivity = [PFObject objectWithClassName:kPAPActivityClassKey];
    [sentMessageActivity setObject:kPAPActivityTypeSentMessage forKey:kPAPActivityTypeKey];
    [sentMessageActivity setObject:self.user forKey:kPAPActivityFromUserKey];
    [sentMessageActivity setObject:[PFUser currentUser] forKey:kPAPActivityToUserKey];
    [sentMessageActivity setObject:textField.text forKey:kPAPActivityContentKey];

    PFACL *sentMessageACL = [PFACL ACLWithUser:[PFUser currentUser]];
    [sentMessageACL setPublicReadAccess:YES];
    [sentMessageACL setWriteAccess:YES forUser:[PFUser currentUser]];
    sentMessageActivity.ACL = sentMessageACL;

    [sentMessageActivity save];


    //even if the message is not saved to log, still send the message
    //ideally should just add this text message activity to the log and the rest of the notification system should send out a notification but since we don't have a centralized place to insert to activity/log, we have to duplicate code here
    // Create our Installation query
    PFQuery *pushQuery = [PFInstallation query];
    [pushQuery whereKey:@"UserObjectId" equalTo:self.user.objectId];
    // Send push notification to query
    [PFPush sendPushMessageToQueryInBackground:pushQuery
                                   withMessage:[[[@"From " stringByAppendingString:[[PFUser currentUser] objectForKey:@"displayName"]] stringByAppendingString:@": " ] stringByAppendingString:textField.text]
     ];