python3 three lines of code to achieve APNS HTTP2 perfect push-based [Detailed]

    First time Apple APNS ( the Apple the Push the Notification Service ) Push, push on APNS certificate of principle and the way the Internet has acquired a lot of information, and is not described in detail here, to note that the certificate is divided into two test certificate and an official certificate species, the proposed official certificate directly with the generic version

We can refer to this blog online: Click here

To download from Apple's certificate is .cer format, push to complete, needs to be converted to .pem format, python has built openssl, this step requires a password set before:

openssl pkcs12 -in Development.p12 -out developent.pem

To push the python, python natural and ultimately install the appropriate package:

pip install applepush

At this point you can try to push:

from applepush Import ApplePush 

the APNS = ApplePush ( ' certificate file name ' , ' bundle ID ' ) 

RESP = apns.single_push ( ' Apple device token ' , " push content " )

If successful push, Congratulations, you have not stepped pit, if the push fails, please refer to the following solutions:

1. If a test environment, verify the official certificate DeviceToken under test environment + test certificate / general purpose test server +

If the environment is the official App Store to download, please confirm whether the official certificate is DeviceToken + generic + official server environment under official

About an official test server and server addresses, can be compared with reference to the official website: Click here to view

2. If it is a formal environment, will return BadDeviceTocken, need to make changes in the download package python:

Open the installation directory Anacoda3 \ Lib \ site-packages \ applepush \ core.py file, the following url address to formally server address:

def __init__(self, cert, apns_topic):
        self.cert = cert
        self.headers = {"apns-topic": apns_topic}
        self.api_url = 'api.push.apple.com:443'
        self.api_path = '/3/device/%s'
  • Development server: api.development.push.apple.com:443

  • Production server: api.push.apple.com:443

You can also add their own push parameters or modify the code to make it compatible with the official server test server:

DEF single_push (Self, token, Alert, Content, badge =. 1 ):
         "" " 
            Send a single device 
            : param token: Equipment 
            : param alert: pop-up message 
            : param badge: red dot numbers 
            : return: 
            " "" 
        token = Self .handle_token (token) 
        payload = {  
            " APS " : {  
               " Alert " : {  
                  " title " : Alert, # iOS7,8 setting is invalid, iOS9 generally set appName 
                 # "SUBTITLE": EXCEPTIONTYPES,# Generally use the title will be able to meet demand 
                 " body":content
              },
              "badge":1, #角标数
              "sound":"default", #声音
              "userinfo":{ #通知内容信息
                   "playid":"123",
                   "username":"tom",
                   "sex":1
              }
           }
        }
        self.api_url1 = 'api.development.push.apple.com:443'
        conn = HTTPConnection(self.api_url, ssl_context=tls.init_context(cert=self.cert))
        conn.request('POST', self.get_api_path(token), body=json.dumps(payload), headers=self.headers)
        resp = conn.get_response()
        if resp.status==200:
            return self.make_response(resp)
        else:
            conn = HTTPConnection(self.api_url1, ssl_context=tls.init_context(cert=self.cert))
            conn.request('POST', self.get_api_path(token), body=json.dumps(payload), headers=self.headers)
            resp = conn.get_response()
            return self.make_response(resp)

Guess you like

Origin www.cnblogs.com/snow-leopard/p/12179927.html