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']
	}
}

华为推送官方文档:华为推送官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值