5

I have a Parse-Server (hosted by heroku) that I have configured to send push notifications. It appears set up correctly on the dashboard, but when I got to actually send one it just says the push is "Saved" but when I go to check its status, it has failed to send. Here is the index.js for the set up:

var api = new ParseServer({
  databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
  cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
  appId: process.env.APP_ID || 'myAppId',
  masterKey: process.env.MASTER_KEY || '', //Add your master key here. Keep it $
  serverURL: process.env.SERVER_URL || 'http://localhost:1337/parse',  // Don't$
  liveQuery: {
    classNames: ["Posts", "Comments"] // List of classes to support for query s$
  },
  push: JSON.parse(process.env.SERVER_PUSH || "{}"),
});

The PARSE_SERVER_PUSH (as well as the appId, masterKey and serverURL) is configured on my config vars on the heroku site as follows:

{ "ios": 
   { "pfx": "/Users/path/to/folder/Prod\Cert.p12 ", 
    "passphrase": "******", 
    "bundleId": "com.parse.app",
    "production": true
    }
}

I am not sure why the dashboard appears to have the server set up to correctly send the push notifications, but they all fail?

Edit: I also tried to send the push via an API request as follows:

curl -X POST \
    -H "X-Parse-Application-Id: myAppsID" \
    -H "X-Parse-Master-Key: ***************" \
    -H "Content-Type: application/json" \
    -d '{ 
        "where": { 
            "deviceType": { "$in": [ "ios",  "android"  ]  }  
        },
        "data": {
            "title": "Ant-man",
            "alert": "This is awesome. It is awesome."
        }
    }' \
    https://appName.herokuapp.com/parse/push

This returned {"result":true} on my terminal, and yet it still showed that the push failed on the dashboard.
Edit2: I have also noticed that despite selecting a specific audience that I created for the pushes, the parse-server always reports that I attempted to send it to the default "Everyone". Not sure if that provides any additional hints or not.

Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • Did you make your bundle id com.parse.app in xcode, or did you forget to swap in your own? And of course you should not have a space in your path, though I assume you've just inserted that to redact your actual path? – Jake T. Jan 19 '18 at 20:51
  • @JakeT. I do have com.parse.app in Xcode as my bundle ID and it was initially incorrect with the spacing in the file path, but I fixed it and that did not amend the issue. Any other ideas? – Runeaway3 Jan 21 '18 at 21:25
  • @JakeT. as a sidenote, it says PARSER and not PARSE in `PARSER_SERVER_PUSH` because using the latter causes the dashboard to not open – Runeaway3 Jan 21 '18 at 22:02
  • Did you type the index.js properly then? Because it has `process.env.PARSE_SERVER_PURSH`, not `PARSER_SERVER_PUSH`, which would lead to a failure as it would be passing in undefined. – Jake T. Jan 22 '18 at 19:51
  • @JakeT. I already tried that. Changing it to `PARSER_SERVER_PUSH` causes the dashboard to be unable to connect to the server – Runeaway3 Jan 23 '18 at 05:03
  • @JakeT. I also instead set the config var on Heroku to `PARSE_SERVER_PUSH` and the value the `index.js` config file to just be `process.env.SERVER_PUSH` and the dashboard opens correctly but the push still fails – Runeaway3 Jan 23 '18 at 05:20
  • Wait, are you saying that you are not using the variable name that you are setting up? Looking at your JSON, are you referencing a local file on your machine? The file with the cert needs to be pushed to the server, and the reference path needs to be local to the server, not your dev machine. Generally you use `__dirname///`, which should be configured to be the directory of your project, making the reference local to your project instead of your machine, in case it moves around. – Jake T. Jan 23 '18 at 15:40
  • If you create that JSON object and name it `PARSE_SERVER_PUSH` in your config, you should be using `env.PARSE_SERVER_PUSH` in your index.js. If this is causing an error at runtime, it's because it can't find that file and fails. If you don't use the same name, you're referencing nothing. These env variables don't know what you want them to be automatically, you're explicitly setting them by name. What is the error output when you use the same names? – Jake T. Jan 23 '18 at 15:42
  • @JakeT. how do I push the file to the server? It is currently just sitting in a folder on my desktop (well... really in a local repo that I use for source control of the Xcode project). Do I just put it into the heroku repo and deploy the repo back to heroku after dropping the cert into it? – Runeaway3 Jan 24 '18 at 01:57
  • @JakeT. I think I am following what you are saying though, as I said with the previous comment, I am not sure exactly how to go about that. I have a cloned repo from heroku (lets call it `test`) on my dev machine. If I put the cert into a `certfolder` folder in this cloned repo, what *exactly* do I write for the file path? (Sorry if I am a bit confused) – Runeaway3 Jan 24 '18 at 02:23
  • @JakeT. another side note... not sure if "directory of my project" refers to the cloned repo from heroku (which contains the parse-server configurations, etc...) or if it refers to the repo that I pull to and push from to source control my project with github (which contains all of the xcode project files, swift files, etc...) – Runeaway3 Jan 24 '18 at 02:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163761/discussion-between-alek-piasecki-and-jake-t). – Runeaway3 Jan 24 '18 at 03:39
  • Not sure, but `com.parse.app` probably in use, or blocked in APNS/GCM, Try having unique bundle id – Medet Tleukabiluly Jan 28 '18 at 02:43
  • @MedetTleukabiluly that is just a placeholder for my actual bundle id that I didn't post – Runeaway3 Jan 28 '18 at 19:12

1 Answers1

2

client push is not supported in parse-server you can read more about it in here:

http://docs.parseplatform.org/parse-server/guide/#push-notifications

the reason that it is not supported its because you must provide your masterKey and this is can be done only in cloud code. So in order to push notify your users you must create a cloud code function that will perform the push and there you need to use your masterKey

Since cloud code is NodeJS you can read parse-server JavaScript docs in order to know how to do it. Specifically you need to read this part: http://docs.parseplatform.org/js/guide/#push-notifications

but I strongly recommend to read more in order to understand how it works.

Here is some code snippets from one of my parse-server projects:

Parse.Push.send({
    data: oPayload,
    where: oWhereQuery
}, {
        useMasterKey: true
    }).then(function () {
        console.log("Push notification sent to: " + aRecievers);
    }, function (err) {
        console.log("Failed to send push notification with error: " + err);
    });

As you can see from the above code, I sent payload which is the payload that needs to be sent (according to parse-server docs because this payload must fit to APNS/GCM). The where is parse-server feature which allows you to filter to which devices you like to send the push notification. Also notice that I set userMasterKey: true which means that I send this push as parse-server admin

After you create the cloud code function and register your devices with parse-server you can execute a CURL request to trigger the cloud code function to test it. (endpoint is: https://:/functions/

Hope it helps.

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
  • where do I set up the cloud code function? In Xcode? In the config vars on heroku? Also how would I execute it via a CURL request? Via terminal? What would be the exact syntax? – Runeaway3 Jan 24 '18 at 01:55
  • Additionally... if only I can create the push with my master key... how do I link certain user actions (like, for example, sending a message) to automatically send a push to the recipient? – Runeaway3 Jan 24 '18 at 02:26
  • Hi @AlekPiasecki . Cloud code is part of your parse-server backend installation. It's a simply main.js NodeJS file which contain custom functions and hooks which are integrated inside the parse-server runtime. I really recommend you to read the docs before doing it. I wrote a series of blogs on parse-server how to install, run and deploy although it is not cover the push notifications part it can help you to understand it better: https://medium.com/google-cloud/from-zero-to-hero-run-parse-server-on-google-cloud-platform-part-0-introduction-54e7725c7ba0 – Ran Hassid Jan 24 '18 at 08:06