简体   繁体   English

向目标设备发送推送通知

[英]Sending push notifications to a targeted device

I have a mobile app (both iOS and Android) created for an online shop.我有一个为在线商店创建的移动应用程序(iOS 和 Android)。 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.我可以有某种 cron 工作来定期检查买家打开的新案例。 I'm hoping to use Parse to handle push notifications.我希望使用Parse来处理推送通知。

在此处输入图片说明

Now where I'm a stuck at is how to associate my database server with Parse.现在我遇到的问题是如何将我的数据库服务器与 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. Parse SDK 向其服务器注册设备。 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.除此之外,使用单独的 Web 服务,我希望保存 Parse 生成的唯一 ID(经过研究,我发现合适的唯一 ID 称为Installation ID )以及我的数据库中登录的卖家 ID。

  2. When the cron job finds an open case, it retrieves the seller's ID that it should go to.当 cron 作业找到一个打开的案例时,它会检索它应该去的卖家 ID。 Along with it, that Installation ID.连同它,安装 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.我的服务器发出一个 HTTP 请求,以使用 Parse 的 REST API 向具有该安装 ID 的设备发布推送通知。

One issue.一题。 They haven't specified a way to pass an Installation ID in their docs .他们没有在他们的文档中指定传递安装 ID 的方法。

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 :首先,你可以得到installationId通过PFInstallation

PFInstallation.currentInstallation().installationId // swift

or或者

[PFInstallation currentInstallation].installationId  // Objective-C

Dont know about android but probably similar不知道android,但可能类似

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

https://parse.com/docs/osx/api/Classes/PFInstallation.html https://parse.com/docs/android/api/com/parse/ParseInstallation.html 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".然后您实际上必须发送推送:查看“使用高级定位”下的Parse REST 指南

You can use a curl query like the following:您可以使用如下所示的 curl 查询:

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.这里的重要部分是{ "where": { "installationId": "%installationId%" }它将推送消息仅发送到与查询匹配的安装 - 正是具有给定 ID 的安装。

I just tried and got a push message only on specific device :)我刚刚尝试并仅在特定设备上收到推送消息:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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