Sending push notifications to a targeted device

2019-05-24 12:43发布

问题:

I have a mobile app (both iOS and Android) created for an online shop. This particular version of the app is for the sellers only. There is a section in it that function as a helpdesk. Basically buyers can open tickets, send messages to sellers etc. So what I want to do is every time when a buyer does that, the seller should receive a push notification on his device.

The database that keeps records of buyers, sellers and everything else runs on a separate server. I can have some sort of a cron job to periodically check for new cases opened by buyers. I'm hoping to use Parse to handle push notifications.

Now where I'm a stuck at is how to associate my database server with Parse.

My current plan is something like this.

  1. The app is launched and the seller is logged in for the first time. Parse SDK registers the device with its servers. Alongside this, using a separate web service, I was hoping to save the unique ID generated by Parse (Upon research, I found the suitable unique ID is called Installation ID) and the logged in seller's ID in my database as well.

  2. When the cron job finds an open case, it retrieves the seller's ID that it should go to. Along with it, that Installation ID.

  3. My server sends out a HTTP request to post a push notification to the device with that Installation ID using Parse's REST API.

One issue. They haven't specified a way to pass an Installation ID in their docs.

I want to know if this is possible? Or if there is a better way to go about this?

回答1:

Pretty sure that is possible. First you can get the installationId via the PFInstallation:

PFInstallation.currentInstallation().installationId // swift

or

[PFInstallation currentInstallation].installationId  // Objective-C

Dont know about android but probably similar

ParseInstallation.getCurrentInstallation().getInstallationId() // Java

https://parse.com/docs/osx/api/Classes/PFInstallation.html https://parse.com/docs/android/api/com/parse/ParseInstallation.html

Send the push

And then you actually have to send the push: Take a look at the Parse REST guide under "using advanced targeting".

You can use a curl query like the following:

curl -X POST -H "X-Parse-Application-Id: %appId%" -H "X-Parse-REST-API-Key: %api key%" -H "Content-Type: application/json" -d '{ "where": { "installationId": "%installationId%" }, "data": { "alert": "The Giants scored a run! The score is now 2-2." } }' https://api.parse.com/1/push

The important part here is the { "where": { "installationId": "%installationId%" } which sends the push message to only the Installations that match the query - exactly the one installation with the given id.

I just tried and got a push message only on specific device :)