0

I'm trying to make a push notification, I had set my .p12 files in parse-server/certs folder. Here is my code in my index.js:

var api = new ParseServer({ 
 databaseURI: process.env.DATABASE_URI || 'mongodb://localhost:27017/dev', 
 cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
 appId: 'xx', 
 masterKey: 'xx',
 fileKey: 'xx', 
 clientKey: 'xx',
 serverURL: 'xx',
 push: {
 ios: [
 {
 pdx: 'certs/ParsePushDevelopmentCertificate.p12', // Dev PFX or P12
 bundleId: 'bundleId',
 production: false // Dev
 }
 ]
 }
});

I would like to send push notification by cloud code. So here is my main.js:

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);
 }
 , useMasterKey: true});
});

And I call it in my Xcode project:

[PFCloud callFunctionInBackground:@"pushToAll" withParameters:@{@"message" : @"test"} block:^(id object, NSError *error) {
 if (!error) {
 NSLog(@"YES");
 } else {
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Try Again !" message:@"Check your network" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
 [alert show];
 }
}];

But that doesn't work: [Error]: {"code":1,"message":"Internal server error."} (Code: 1, Version: 1.12.0)

On Parse.com, I've "Installation" field in my DB: Screenshot

Have you got any idea ?

asked Apr 12, 2016 at 22:07
3
  • Make sure that you are able to connect to Parse - Server first. Send something up or pull something down to make sure that your app is connected correctly. Commented Apr 13, 2016 at 11:43
  • @DanL Yes it works fine : add/delete object, other PFCloud method works fine, but PFCloud for notification pushes doesn't work. Commented Apr 13, 2016 at 11:47
  • If you set both production and development cert in the config you will get at least one error everytime in console because the server tries to send it both ways and obviously one of them always fail, you should have seperate parse server for dev and production in this moment... Provide some logs what happens when the cloud code runs Commented Apr 14, 2016 at 7:06

1 Answer 1

2

My cloud code wasn't right, here is the good code:

Parse.Cloud.define("pushToAll", function (request, response) {
 var message = request.params.message;
 console.log(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 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);
 }
 , useMasterKey: true});
});
answered Apr 14, 2016 at 11:02
Sign up to request clarification or add additional context in comments.

2 Comments

@cricket_007 , Thanks, but I chnged it, I improved it for my case
@Viny76, can you share your cURL syntax to make this cloud push send? I copied your func as is except took out var message & used a simple string. My cURL = curl -X POST -H "X-Parse-Application-Id: myAppId" -H "Content-Type: application/json" -d {} h t t p s ://__myApp__.herokuapp.com/parse/functions/pushToAll. Tried different syntax also but getting errors. The latest error: "XMLHttpRequest failed: \"Unable to connect to the Parse API\". I am able to reach the cloud code as the default sample code returns {"result":"Hi"}. Thx.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.