Python to build APNS related modules of the apple push notification PUSH Service User Guide, pythonapns

Source: Internet
Author: User

Python to build APNS related modules of the apple push notification PUSH Service User Guide, pythonapns

APNS is a Push service provided by Apple for IOS devices ). You should be familiar with mobile Internet-related development.

Next, I will give you a brief introduction to some APNS related modules in Python and their features.

Module introduction:

PyAPNs

Project address: https://github.com/djacobs/PyAPNs
PyAPNs is the first APNS Module I used. It should be the simplest of all modules I want to introduce. The latest source code is only 384 lines, implementing the basic functions of APNS, this includes sending and pushing, Frame mass pushing, and feedback APIs.

All its verifications are performed on the client. For example, each Payload must not exceed 256 bytes.
To put it simply, try to reuse your links as much as possible. Do not set up or disconnect them frequently. Otherwise, they will be treated as DoS attacks. So we should do this when using it to send push:

...... # Reuse this gateway_serverapns.gateway_server.send_notification (token_hex, payload)

Reuse this gateway_server is the connection, but the connection to the APNS Server is unstable. In many cases, the connection is disconnected, such as the network reason and an invalid token is sent. So we need a reconnection mechanism.

However, the PyAPNs module does not handle these errors for you. Therefore, you need to handle errors by yourself. This is also the most inconvenient place to use this module.

So my suggestion is that unless you need to write an APNS Provider, you can start with this module. Otherwise, if you want to quickly use the PUSH Service in your project, we recommend that you select another module.

Example:

Import timefrom apns import APNs, Frame, Payload apns = APNs (use_sandbox = True, cert_file = 'cert. pem', key_file = 'Key. pem ') # Send a icationicationtoken_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87 'payload = payload (alert = "Hello World! ", Sound =" default ", badge = 1) apns. gateway_server.send_notification (token_hex, payload) # Send multiple events in a single transmissionframe = Frame () identifier = 1 expiry = time. time () + 3600 priority = 10frame. add_item ('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87 ', payload, identifier, expiry, priority) apns. gateway_server.send_notification_multiple (frame) # Get feedback messagesfor (Token_hex, fail_time) in apns. feedback_server.items (): # do stuff with token_hex and fail_time for more complex alerts, such as custom buttons, you can use the PayloadAlert class alert = PayloadAlert ("Hello world! ", Action_loc_key =" Click me ") payload = Payload (alert = alert, sound =" default ")

Pyapns (twisted)

Project address: https://github.com/samuraisam/pyapns
They use this project as the provider of their push service, so I migrated the push from PyAPNs to this project. It is actually quite good to use it, the main features of this project are:

It is actually a twisted-based server. All push requests are sent through it to interact with Apple's server.
Native support for Django and Pylons.
Supports multiple apps.
Because the push server and apple are long connections maintained by this provider, every time you send a push, you directly call this provier for interaction, the advantage of this is that every interface call returns fast, and the process of actually pushing to the Apple Server is done asynchronously by this provider.

However, this module has not been maintained for a long time. In fact, the Apple protocol has been updated during this time. But this module does not keep up.

The biggest problem I encountered when using this module is that the effect of group push is not guaranteed.

Although the demo of this module provides support for Batch Sending and pushing, my experience is that the mass pushing effect of this module is relatively poor, in addition, the logic for getting error feedback from the Apple Server is missing.

Because the Twisted code style is really not very popular, I began to look for other solutions when I encountered problems in the group.

Apns-client

Project address: https://bitbucket.org/sardarnl/apns-client/
Summary:

Maintain a persistent link. The handshake of the SSL protocol is slow. After each connection is established, it should be kept for at least a few minutes to wait for the next push.
Supports improved protocol formats. Apple programmers have designed a notorious push protocol. They have updated a version, which allows you to know which individual message is faulty in each batch push.
Clear Python API
Instead of writing verification into the code, the system directly returns the error message of APNS.
Using this module to send push is also very easy:

From apnsclient import * # You can use the Session object to maintain the connection pool session = Session () con = session. get_connection ("push_sandbox", cert_file = "sandbox. pem ") # Send push and get feedback messge = Message ([" my "," device "," tokens "], alert =" My message ", badge = 10) # Send the message. srv = APNs (con) res = srv. send (message) # Check failures. check codes in APNs reference docs. for token, reason in res. failed. items (): code, errmsg = reason print "Device faled: {0}, reason: {1 }". format (token, errmsg) # Check failures not related to devices. for code, errmsg in res. errors: print "Error:", errmsg

For me, the biggest advantage of this module is that the connection may be abnormally disconnected and reconnected. The code is not as obscure as pyapns, more intuitive and more readable. So there is no problem if you want to make some modifications based on it.

After my experience, there is no problem in using apns-client to handle million-level push, and the arrival rate is also good.

Therefore, if you have no special requirements, apns-client should be your best choice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.