0

I'm currently implementing push notifications to follow user. Apparently, I managed to get push notifications done and responsive well.Hence, The notifications were sent to everyone.I would like to create push notifications and received the notification only by one respective user each time when other users have followed their user account.

I haven't create a pointer that should associate with User. Even If I create, is there any amendments that I should amends on my Cloudcode? I would like to send push notifications to a specific user whenever other user has followed that user.

eg: Test 1 followed you.

    Parse.Cloud.define("FollowersAndFollowing", function(request,result){
  var query = new Parse.Query(Parse.User);
  var message = request.params.message;
   var pushQuery = new Parse.Query(Parse.Installation);
  query.equalTo('userLink',request.params.User);

  Parse.Push.send({
    where: pushQuery,
    data : { 
      alert: message,
      badge: "Increment",
      sound: "",
    }
    }, {
    success: function(result) {
    console.log(JSON.stringify(result));
    response.success(result);
    },
    error: function(error) {
    console.error(JSON.stringify(error));
    response.error(error)
    }
  });
});

Above this ^ is my cloud code in .JS

if (status == false) {

                                // Create the push notification message.s
                                let pushMessage = "\(PFUser.currentUser()!.username!) has followed you."

                                // Submit the push notification.
                                PFCloud.callFunctionInBackground("FollowersAndFollowing", withParameters: ["message" : pushMessage, "User" : "\(userData.username!)"])
                            }

and above this is in swift code for frontend.

enter image description here

and the second the url is my class and subclasses of how I setting up

enter image description here

1 Answer 1

1

Use a cloud code beforeSave trigger on the Installation class to keep User pointers up to date.

// Make sure all installations point to the current user
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {

    Parse.Cloud.useMasterKey();
    if (request.user) {
        request.object.set("user", request.user);
    } else {
        request.object.unset("user");
    }
    response.success();
});

You may also want to use an afterSave trigger on your Follow class to send out the push notification instead of calling a cloud function. Without knowing the structure of that class or how you have implemented a follower/following scheme it's hard to give any further information.

Sign up to request clarification or add additional context in comments.

6 Comments

Actually, Ive 'implemented beforeSave trigger. and i also added this on the frontend
[[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:@"user"]; [[PFInstallation currentInstallation] saveEventually];
Is everything else working then? When triggering the push notification for a new follower, using an afterSave trigger on the follow class would be more efficient than calling a cloud function. Calling the cloud function will use 1 API request, whereas afterSave triggers are free.
Russell, It seems that I've implemented all of them but the target is still everyone.. Additional of that, I also added this on appDelegate [[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:@"user"]; [[PFInstallation currentInstallation] saveEventually] and the app stucks on app lock screen. Do you know why this happened?
Try print out request.params.User to make sure that it's correct. Use console.log("request.params.User = " + JSON.stringify(request.params.User)); It's possible that it may not be set properly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.