python3 華爲推送系統接入

只是後臺對接華爲推送服務接口對接,前端集成還是請參考官方文檔

 

  1. 根據AppID和AppSecret 獲取請求token
  2. 根據token發送http請求 

 

獲取token

請注意token的有效時間,不避免頻繁獲取

    def get_access_token(self):
        """
        獲取access_token
        :return:
        """

        auth_url = "https://oauth-login.cloud.huawei.com/oauth2/v2/token"
        auth_headers = {"Content-Type": "application/x-www-form-urlencoded"}
        post_data = {
            "grant_type": "client_credentials",
            "client_id": AppID,
            "client_secret":AppSecret
        }
        auth_data = r.post(url=auth_url, headers=auth_headers, data=post_data).json()
        logger.info("推送華爲消息auth_data:" + str(auth_data))
        return auth_data.get("access_token")

發送http請求

 def push_message(self, msg_json: dict):

        push_headers = {
            "Authorization": "Bearer " + access_token,
            "Content-Type": "application/json"
        }
        logger.info("推送華爲消息push_headers:" + str(push_headers))
        logger.info("推送華爲消息push_url:" + str(self.push_url))
        push_return = r.post(url=self.push_url, headers=push_headers, data=json.dumps(msg_json)).json()
        return push_return

msg_json 的結構爲:

{
	'validate_only': False,
	'message': {
		'notification': {
			'title': 'title',
			'body': '您有新的消息!'
		},
		'android': {
			'notification': {
				'click_action': {
					'type': 1,
					'intent': 'intent://sxzq.ficc.codelabpush/deeplink?#Intent;scheme=fawo;launchFlags=0x4000000;i.age=180;S.name=abc;end'
				},
				'tag': 'fawo',
				'badge': {
					'num': 1,
					'class': 'sxzq.ficc.fawo001.MainActivity'
				},
				'notify_id': 1
			}
		},
		'token': ['ADVLy0yvPtAgdqx-rAr-qH4BJ8dLqCIG34_RpSSLufZU48crqzOQMXa6e2qV1eWyJGuB6un7YjiNXgbSyuXWBj0X9Dxz45T3kz34bb7wpg4l3WPPxLQj1vA6_UASsoWv7A']
	}
}

華爲推送官方文檔:華爲推送官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章