只是后台对接华为推送服务接口对接,前端集成还是请参考官方文档
- 根据AppID和AppSecret 获取请求token
-
根据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']
}
}
华为推送官方文档:华为推送官方文档