極光推送Python簡化版
阿新 • • 發佈:2019-02-17
極光推送官方的Python-sdk一大堆麻煩的東西都用不到,還要在機器上裝模組,蛋疼
實際上就一個http-post請求就能解決大部分問題的東西,v2,v3api都以簡化,可以直接拿去用,不謝
#! /usr/bin/env python2 # encoding=utf-8 import time,random,hashlib,json,urllib2,urllib,sys,os sys.path.insert(0,os.path.dirname(sys.path[0])) reload(sys) sys.setdefaultencoding('utf8') import requests #蘋果的測試環境0,生產環境 v2 apns_production = 1 #蘋果的測試環境0,生產環境 v3 apns_production_boolean = True ''' 極光key配置 ''' apps={ 'test':{ "app_key" : u'app_key', "master_secret" : u'master_secret' }, 'product':{ "app_key" : u'app_key', "master_secret" : u'master_secret' } } ''' https request jpush v3 ''' def https_request(app_key,body, url, content_type=None,version=None, params=None): https = requests.Session() https.auth = (app_key['app_key'], app_key['master_secret']) headers = {} headers['user-agent'] = 'jpush-api-python-client' headers['connection'] = 'keep-alive' headers['content-type'] = 'application/json;charset:utf-8' #print url,body response = https.request('POST', url, data=body, params=params, headers=headers) #合併返回 return dict(json.loads(response.content) , **{'status_code':response.status_code}) ''' jpush v3 params 支援離線訊息,線上通知同時傳送 ''' def push_params_v3(content,receiver_value=None,n_extras=None,platform="ios,android"): global apns_production_boolean sendno = int(time.time()+random.randint(10000000,99999900)) payload =dict() payload['platform'] =platform payload['audience'] ={ "alias" : receiver_value } #離線訊息 payload['message'] = { "msg_content" : content, "extras" : n_extras } #線上通知 payload['notification'] = { "android" : {"alert" : content,"extras" : n_extras}, "ios" : {"alert" : content,"sound":"default","extras" : n_extras}, #"badge":1, } payload['options'] ={"apns_production":apns_production_boolean,"time_to_live":86400*3,'sendno':sendno} return payload ''' jpush v3 request ''' def jpush_v3(app_key,payload): body = json.dumps(payload) #print body return https_request(app_key,body, "https://api.jpush.cn/v3/push",'application/json', version=1) ''' http post 簡單版,jpush v2 ''' def jpush(push_params): url = 'http://api.jpush.cn:8800/sendmsg/v2/sendmsg' content = "" if push_params: para_data = urllib.urlencode(push_params) f = urllib2.urlopen(url, para_data) content = f.read() return content ''' jpush v2 #拼推送引數 @param int $sendno 傳送編號。由開發者自己維護,標識一次傳送請求 @param int $receiver_type 接收者型別。 1、指定的 IMEI。此時必須指定 appKeys。 2、指定的 tag。 3、指定的 alias。 4、 對指定 appkey 的所有使用者推送訊息。 @param string $receiver_value 傳送範圍值,與 receiver_type相對應。 1、IMEI只支援一個 2、tag 支援多個,使用 "," 間隔。 3、alias 支援多個,使用 "," 間隔。 4、不需要填 @param int apns_production: 1 生產,0 測試 @param int $msg_type 傳送訊息的型別:1、通知 2、自定義訊息 @param string $msg_content 傳送訊息的內容。 與 msg_type 相對應的值 @param string $platform 目標使用者終端手機的平臺型別,如: android, ios 多個請使用逗號分隔 ''' def push_params(keycfg,content,match_id=None,receiver_value=None,n_extras=None,receiver_type='3',platform="ios,android"): global apns_production msg = {"n_title" :"APP名字","n_content":content,"n_extras":n_extras} msg_content = json.dumps(msg) sendno = str(int(time.time()+random.randint(10000000,99999900))) verification_code = hashlib.new("md5", sendno+receiver_type+receiver_value+keycfg['master_secret']).hexdigest() param = dict() param['sendno'] =sendno param['app_key'] =keycfg['app_key'] param['receiver_type'] =receiver_type param['receiver_value'] =receiver_value param['verification_code'] =verification_code param['msg_type'] ='1' param['msg_content'] =msg_content param['platform'] =platform param['apns_production'] =apns_production param['time_to_live'] =str(3*86400) return param