How to send a collapsible push notification using Parse.com's cloud code - parse-platform

I am using Parse.com to send push notifications via Cloud Code. These notifications are "send to sync" so I want them to be collapsible. Collapsible means that if a device is turned off or otherwise not receiving push notifications, that these notifications should not build up. When my phone turns on, I don't need a bunch of undelivered pushes showing up telling me to sync. All I need is one. I see no way to do this in Cloud Code. Is there a way to make your push notifications collapsible? Here is my CloudCode.
Parse.Cloud.afterSave("Tagnames", function (request) {
//Get the Customer that is pointed to in the AlarmDefinitions object.
query = new Parse.Query("Customers");
query.get(request.object.get("customer").id, {
success : function (cust) {
//We have the customer pointed to by the AlarmDefinition.
//Create the json payload data we will send to our clients.
console.log("Customer=" + cust.get("customer"));
console.log("action:" + "com.jrb.scadaalarm.rcvr.UPDATE_TAGNAMES");
//send the push so that all customers can get notified.
Parse.Push.send({
channels : [cust.get("customer")],
data: {
action: "com.jrb.scadaalarm.rcvr.UPDATE_TAGNAMES"
}
}, {
success : function () {
// Push was successful
console.log("Push successful.");
},
error : function (error) {
// Handle error
console.error("Push failed: " + error.code + " : " + error.message);
}
});
//
},
error : function (error) {
console.error("Got an error " + error.code + " : " + error.message);
}
});
});

Unfortunately, it doesn't look like Parse supports stackable notifications. Take a look at this answer here, from the Parse archives.
https://parse.com/questions/android-stack-push-notifications

Related

Parse Push's not delivered cloud code only

I'm running parse-server on ubuntu and can't seem to get push notifications working when sent in cloud code.
Push's work when using a REST api call (passing master key) but don't work when cloud code calls them.
What's interesting is that the cloud code Parse.Push() method returns a success and thus no error message.
My hypothesis is that this is a configuration problem, and the Parse.Push() method is referencing somethign I have incorrectly configured on the server.
here is my cloud function. This call works when sent via REST. and the success callback in cloud is always called.
Parse.Push.send(
{
// where: pushQueryClient,
channels: ["user_tkP7gurGzc"],
data:
{
alert: pushTextClient
}
},
{
success:function(){
console.log("push sent");
},
error: function(error){
console.log("push failed");
console.dir(error);
},
useMasterKey: true});
i think you have an issue with the useMasterKey parameter.
Please try to use this code in order to send the push notification:
Parse.Push.send({
where: whereQuery,
data: {
alert: {
title: request.params.title,
body: request.params.body
},
type: request.params.type,
sound: 'default'
}
}, {
useMasterKey: true
}).then(function() {
response.success();
}, function(error) {
response.error("Push failed " + error);
});
In this code i use Promises which is the best practice and also wrap useMasterKey in a separate object

Parse Cloud Code Push + Client-initiated push isn't enabled

I am trying to run a parse cloud code function that sends a push to a user that is associated with an installation object. When I run the following code from within the cloud function I get Error 115 - Client-initiated push isn't enabled.
// SEND PUSH TO PARENT
var query = new Parse.Query(Parse.Installation);
query.equalTo('user', reservation.get("parent"));
Parse.Push.send({
where: query, // Set our Installation query
data: {
alert: "Your request has been accepted!"
}
}, {
success: function () {
console.log("push worked");
return result;
},
error: function (error) {
console.log("Error: " + error.code + " " + error.message);
return result;
}
});
I don't see anywhere that client side pushes need to be enabled for cloud code? Am I missing something or do I need it enabled?
Navigate to your app's settings in the Parse Dashboard, then enable client side push under Push Notifications.

Parse.com: Retrieving object by objectId does not work. Why? It is very simple query

I got an error with message "101 Object not found."
The below code is just copy from the official guide. And I changed the class name and the objectId.
I know this is very simple query but I don't know why? Help me how to debug in this case...
This code is in cloud code. I set up "applicationId" and "masterKey" in global.json.
Thanks..
require('cloud/app.js');
Parse.Cloud.define("sample", function(request, response) {
var GameScore = Parse.Object.extend("Item");
var query = new Parse.Query(GameScore);
query.get("XXXXXX", {
success: function(gameScore) {
},
error: function(object, error) {
console.error("error: " + error.code + " " + error.message);
}
});
});
I tend to use the promise method and I'd possibly rewrite it like this...
Parse.Cloud.define("sample", function(request, response) {
var query = new Parse.Query("Item");
// put this in as a debug message.
console.log("Just checking I'm here!");
query.get("XXXXXX").then (function(item) {
response.success(item);
}, function(error) {
console.error("error: " + error.code + " " + error.message);
response.error(error);
});
});
But it should work as it is. Odd. Are you sure the error message is coming from your code?
Try adding a log before it.
EDIT
It seems that permissions were not set properly on your item object.
With iOS you can specify a default ACL for objects at create time. You can also create a custom ACL object and pass it to the object when saving it.

Custom key value pairs in push message

I am currently using the Urban Airship service to send Push Messages and looking at moving to Parse.com.
In Android, one can supply a set of data using key value pairs which can be accessed once the message is tapped.
Using Parse, is it possible to include this custom payload when sending to iOS and Android?
Thanks
Yes this is possible. Here's an example of a Parse push with arguments, in CloudCode.
Parse.Push.send({
where: pushQuery, // Set our Installation query
data: {
alert: "Notification message",
keyOne: "First value",
keyTwo: "Another value"
}
},{
success: function() {
// Push was successful
},
error: function(error) {
throw "Got an error " + error.code + " : " + error.message;
}
});
Then you can access the data on the device receiving the Push.

Parse Cloud Code afterSave not working

Hey I'm using Parse as my backend and I love it but I have a problem with the afterSave hook.
Here is the Code I'm using:
Parse.Cloud.afterSave ("JGZwoelf",function (request) {
Parse.Push.send({
//Selecting the Channel
channels: [ request.object.get('JGZwoelfPush') ],
data: {
//Selecting the Key inside the Class
alert: request.object.get('AusfallInfo')
}
}, {
success: function () {
//Push was send successfully
},
error: function (error) {
//Handle error
throw "Got an error" + error.code + " : " + error.message;
}
});
});
Every time the logs console is telling me: Result:
Uncaught Got an error112 : Missing channel name.
I just don't understand what is wrong! It must be in that JavaScript code. If I enter the push notification manually everything works fine :/
Edit:
The part Parse.Push.send should look like this:
Parse.Push.send ({
//Selecting the already existing Push Channel
channels: ["JGAchtPush"], //This has to be the name of your push channel!!
data: {
//Selecting the Key inside the Class
alert: request.object.get ("AusfallInfo")
}
}, {
success: function () {
//Push was sent successfully
//nothing was loged
},
error: function (error) {
throw "Got and error" + error.code + " : " + error.message;
}
});
The channel name needs to be something like ["exampleChannel"].
Thanks in advance for any given help :)
The first argument to afterSave should be a class name, not an objectId.
following is for new folks (like me), it is the exact same code in original question, plus a few more comments, plus the correction from accepted answer. purpose is to show example of what few pieces of code need changing for this to work in your parse cloud code. thank you Constantin Jacob and bklimt.
Parse.Cloud.afterSave ("UserVideoMessage",function (request) { // name of my parse class is "UserVideoMessage"
Parse.Push.send ({
//Selecting the already existing Push Channel
channels: ["admin"], //This has to be the name of your push channel!!
data: {
//Selecting the Key inside the Class, this will be the content of the push notification
alert: request.object.get ("from")
}
}, {
success: function () {
//Push was sent successfully
//nothing was loged
},
error: function (error) {
throw "Got and error" + error.code + " : " + error.message;
}
});
});

Resources