简体   繁体   English

一台服务器向Android和iOS设备发送推送通知

[英]One server sending push notifications to Android and iOS devices

Our organization has an Android app and an iOS app. 我们的组织有一个Android应用程序和一个iOS应用程序。

We want to start pushing notification to these apps. 我们想开始向这些应用推送通知。

Android has GCM. Android有GCM。 Apple has APNS. Apple拥有APNS。

But we want to create an API which will work on both android and iOS. 但我们想要创建一个可以在Android和iOS上运行的API。

What is the easiest way to setup a server so that when a push notification needs to be sent, it knows exactly which server to send the message to? 设置服务器的最简单方法是什么,以便在需要发送推送通知时,它确切地知道要将消息发送到哪个服务器?

I use a service called Parse to do my notification pushes to both Android and iOS. 我使用一个名为Parse的服务将我的通知推送到Android和iOS。 They have great documentation and libraries available. 他们有很棒的文档和库。 You can get some details here: https://parse.com/products/push 您可以在此处获取一些详细信息: https//parse.com/products/push

As a little background this is for a university setting where multiple colleges apps as well as distance education may be using the service. 作为一个小背景,这是一个大学环境,多个大学的应用程序和远程教育可能正在使用该服务。 Here is the approach that we are using in our organization. 以下是我们在组织中使用的方法。 If you look at the way APNS works it can be used by just sending a web call to the APNS service with the token id. 如果您查看APNS的工作方式,只需使用令牌ID向APNS服务发送Web调用即可使用它。 GCM is very close to the same type of system. GCM非常接近同一类型的系统。 Basically create a JSON package and send it to the desired service. 基本上创建一个JSON包并将其发送到所需的服务。

Here is our steps we used to create this service. 以下是我们用于创建此服务的步骤。

  1. Server admins created a server and database that can be called that will collect the tokens from both android and ios devices. 服务器管理员创建了一个可以调用的服务器和数据库,它将从android和ios设备收集令牌。 When the device registers we also send what type of device it is. 当设备注册时,我们也发送它是什么类型的设备。 This is possible since we are just sending data to the database that is has been created. 这是可能的,因为我们只是将数据发送到已创建的数据库。

  2. From here we then created a couple of python scripts that send the data do the desired service whether it is ios or android. 从这里我们创建了几个python脚本,无论是ios还是android,都可以发送数据所需的服务。 These scripts gather the appropriate data from the database and sends the packaged data (JSON package) to APNS for ios message and GCM for google cloud. 这些脚本从数据库中收集适当的数据,并将打包的数据(JSON包)发送到APNS for ios消息和GCM for google cloud。

  3. We also created a web interface so that those who need to send messages to the devices can. 我们还创建了一个Web界面,以便那些需要向设备发送消息的人可以。

The rest of the implementation is up to you to decide the best way to utilize the service. 其余的实施由您决定使用该服务的最佳方式。 For example when to check for invalid devices, Because we are planning on using this same server for multiple applications we can send the type of device, token, application, or whatever else is needed for an application to distinguish it from others we produce so that each application that wants to use the service can. 例如,何时检查无效设备,因为我们计划将同一服务器用于多个应用程序,我们可以发送设备类型,令牌,应用程序或应用程序所需的任何其他内容,以区别于我们生成的其他应用程序,以便每个想要使用该服务的应用程序都可以。 I hope this helps and gives you some idea on how to accomplish this. 我希望这会有所帮助,并让您了解如何实现这一目标。

For APNS, Maybe you may consider this forked version of PyAPNS that has enhanced message support. 对于APNS,也许您可​​以考虑这个具有增强的消息支持的PyAPNS分叉版本。
https://github.com/jimhorng/PyAPNs https://github.com/jimhorng/PyAPNs
which means it will catch error response for failure messages and resent the message which are discarded by APNS while sending between failure messages and receiving error response. 这意味着它将捕获失败消息的错误响应,并重新发送APNS在发送失败消息和接收错误响应时丢弃的消息。

Solution: 解:

  • Non-blocking ssl socket connection to send notification without waiting for response. 非阻塞ssl套接字连接,无需等待响应即可发送通知。
  • A separate thread for constantly checking error-response from read connection. 一个单独的线程,用于不断检查读连接的错误响应。
  • A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns. 发送通知缓冲区,用于重新发送通知失败后发送的通知,或者通过apns关闭任意连接。 (Reference to non-blocking apns pull request by minorblend, enhanced message by hagino3000) (参考minorblend的非阻塞apns pull请求,hagino3000的增强消息)

Result: 结果:

  • Send notification at throughput of 1000/secs 以1000 /秒的吞吐量发送通知
  • In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. 在发送第一个通知失败的情况下,1秒后错误响应响应,APNS同时丢弃999个通知,所有丢弃的999通知将被重新发送而不会丢失任何通知。 With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one. 使用相同的逻辑,如果通知重新发送失败,它将在失败后重新发送其余的重发通知。

For GCM, you may consider https://github.com/geeknam/python-gcm 对于GCM,您可以考虑https://github.com/geeknam/python-gcm

For generic wrapper that support both or more mobile provider: https://github.com/Redth/PushSharp 对于支持两个或更多移动提供程序的通用包装器: https//github.com/Redth/PushSharp

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

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