简体   繁体   中英

Sending push notifications to a targeted device

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM