Skip to content

Instantly share code, notes, and snippets.

@OneCricketeer
Last active January 25, 2022 06:36
Show Gist options
  • Save OneCricketeer/10001273 to your computer and use it in GitHub Desktop.
Save OneCricketeer/10001273 to your computer and use it in GitHub Desktop.
Targeted Push Notifications: Parse.com Cloud Code
// Use Parse.Cloud.define to define as many cloud functions as you want.
// For example:
Parse.Cloud.define("hello", function(request, response) {
response.success("Hello world!");
});
Parse.Cloud.define("userWithEmailExists", function(request, response) {
var email = request.params.email;
if (email != null && email !== "") {
email = email.trim();
} else {
response.error("Must provide \"email\" in JSON data");
return;
}
var logMessage = "Checking for email \"{0}\"".format(email);
console.log(logMessage);
// Find users with a given email
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("email", email);
userQuery.find({
success: function(results) {
if (results.length > 0) {
response.success('true');
} else {
response.success('false');
}
},
error: function(error) {
response.error(error);
}
});
});
Parse.Cloud.define("getUserWithEmail", function(request, response) {
var emailParam = request.params.email;
// Check if email exists and return associated user
Parse.Cloud.run("userWithEmailExists", {
email: emailParam
}, {
success: function(booleanObj) {
if (JSON.parse(booleanObj)) {
// Find users with a given email
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("email", emailParam);
userQuery.first({
success: function(object) {
response.success(object);
}
});
} else {
response.error("User with email does not exist");
}
},
error: function(error) {
response.error(error);
}
});
});
Parse.Cloud.define("pushToEmail", function(request, response) {
var emailParam = request.params.email;
var message = request.params.message;
if (message != null && message !== "") {
message = message.trim();
} else {
response.error("Must provide \"message\" in JSON data");
return;
}
// Can see this at https://www.parse.com/apps/{APP_NAME}/cloud_code/log
var logMessage = "Sending \"{0}\" to {1}".format(message, emailParam);
console.log(logMessage);
// Check if email exists and send notification
Parse.Cloud.run("userWithEmailExists", {
email: emailParam
}, {
success: function(booleanObj) {
if (JSON.parse(booleanObj)) {
// Find users with a given email
var userQuery = new Parse.Query(Parse.User);
userQuery.equalTo("email", emailParam);
var pushQuery = new Parse.Query(Parse.Installation);
// pushQuery.containedIn("deviceType", ["ios", "android"]); // errors if no iOS certificate
pushQuery.exists("user"); // filter out installations without users
pushQuery.include('user'); // expand the user pointer
pushQuery.matchesQuery("user", userQuery);
// Send push notification to query
Parse.Push.send({
where: pushQuery, // Set our installation query
data: {
alert: message
}
}, {
success: function() {
// Push was successful
console.log("Message was sent successfully");
response.success('true');
},
error: function(error) {
response.error(error);
}
});
} else {
response.error("User with email does not exist");
}
},
error: function(error) {
response.error(error);
}
});
});
Parse.Cloud.define("pushToAll", function(request, response) {
var message = request.params.message;
if (message != null && message !== "") {
message = message.trim();
} else {
response.error("Must provide \"message\" in JSON data");
return;
}
// Can see this at https://www.parse.com/apps/{APP_NAME}/cloud_code/log
var logMessage = "Sending \"{0}\" to all installations".format(message);
console.log(logMessage);
var pushQuery = new Parse.Query(Parse.Installation);
// pushQuery.containedIn("deviceType", ["ios", "android"]); // errors if no iOS certificate
// Send push notification to query
Parse.Push.send({
where: pushQuery, // Set our installation query
data: {
alert: message
}
}, {
success: function() {
// Push was successful
console.log("Message was sent successfully");
response.success('true');
},
error: function(error) {
response.error(error);
}
});
});
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ?
args[number] :
match;
});
};
}
@OneCricketeer
Copy link
Author

OneCricketeer commented Mar 6, 2016

Notice

For those finding this post-shutdown of Parse.com, there is no guarantee it'll work on the new Parse Server (or any service that hosts it)

Contact me with questions

Instead of commenting below, if you would like assistance or have questions, preferably ask on StackOverflow. My username is cricket_007, and you can tag @cricket_007 in the comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment