Browse Source

荣耀推送

peng 1 year ago
parent
commit
a74b757c5f

+ 8 - 0
AnsjerPush/cn_config/cn_formal_config.py

@@ -198,4 +198,12 @@ MEIZUPUSH_CONFIG = {
         'ID': 151022,
         'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
     }
+}
+
+HONORPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'client_id': 'b5dab836effb48d09f08178a26e0ecd6',
+        'client_secret': '7z8RazyCVmPYx4icpIqYr6KLFnEowkag',
+        'app_id': 104435107
+    }
 }

+ 9 - 0
AnsjerPush/dev_config/local_config.py

@@ -178,5 +178,14 @@ MEIZUPUSH_CONFIG = {
         'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
     }
 }
+
+HONORPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'client_id': 'b5dab836effb48d09f08178a26e0ecd6',
+        'client_secret': '7z8RazyCVmPYx4icpIqYr6KLFnEowkag',
+        'app_id': 104435107
+    }
+}
+
 APNS_MODE = 'dev'
 REDIS_ADDRESS = '127.0.0.1'

+ 8 - 0
AnsjerPush/test_config/test_config.py

@@ -175,4 +175,12 @@ MEIZUPUSH_CONFIG = {
         'ID': 151022,
         'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
     }
+}
+
+HONORPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'client_id': 'b5dab836effb48d09f08178a26e0ecd6',
+        'client_secret': '7z8RazyCVmPYx4icpIqYr6KLFnEowkag',
+        'app_id': 104435107
+    }
 }

+ 1 - 1
Model/models.py

@@ -1508,7 +1508,7 @@ class UidPushModel(models.Model):
     uid_set = models.ForeignKey(UidSetModel, to_field='id', on_delete=models.CASCADE)
     appBundleId = models.CharField(blank=True, max_length=32, verbose_name=u'appID')
     app_type = models.IntegerField(default=0, verbose_name=u'app类型 1:ios,2:安卓')
-    push_type = models.IntegerField(default=0, verbose_name=u'推送类型')  # 0: apns, 1: 安卓gcm, 2: 极光, 3:华为, 4:小米, 5:vivo, 6:oppo, 7:魅族
+    push_type = models.IntegerField(default=0, verbose_name=u'推送类型')  # 0: apns, 1: 安卓gcm, 2: 极光, 3:华为, 4:小米, 5:vivo, 6:oppo, 7:魅族, 8:荣耀
     token_val = models.CharField(default='', max_length=160, verbose_name=u'设备验证令牌')
     jg_token_val = models.CharField(default='', max_length=500, verbose_name=u'极光推送令牌')
     m_code = models.CharField(default='', max_length=64, verbose_name='手机唯一标识')

+ 2 - 0
Service/DevicePushService.py

@@ -395,6 +395,8 @@ class DevicePushService:
                     push_result = True
                 elif push_type == 7:  # android meizupush
                     push_result = PushObject.android_meizupush(**push_kwargs)
+                elif push_type == 8:  # android honorpush
+                    push_result = PushObject.android_honorpush(**push_kwargs)
             return push_result
         except Exception as e:
             LOGGING.info('发送推送异常,error_line:{},error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

+ 64 - 1
Service/PushService.py

@@ -17,7 +17,7 @@ import requests
 from pyfcm import FCMNotification
 
 from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG, XMPUSH_CONFIG \
-    , VIVOPUSH_CONFIG, OPPOPUSH_CONFIG, MEIZUPUSH_CONFIG, CONFIG_INFO
+    , VIVOPUSH_CONFIG, OPPOPUSH_CONFIG, MEIZUPUSH_CONFIG, CONFIG_INFO, HONORPUSH_CONFIG
 from Model.models import UidPushModel
 from Object.RedisObject import RedisObject
 from Object.S3Email import S3Email
@@ -480,6 +480,69 @@ class PushObject:
             LOGGER.info("魅族推送异常:{}".format(repr(e)))
             return False
 
+    @staticmethod
+    def android_honorpush(nickname, token_val, n_time, event_type, msg_title, msg_text,
+                          uid='', channel='1', image='', app_bundle_id='', appBundleId='', channel_id=''):
+        """
+        android honor 推送
+        @param channel_id: 通知通道id
+        @param nickname: 设备昵称
+        @param app_bundle_id: app包id
+        @param appBundleId: app包id
+        @param token_val: 推送token
+        @param n_time: 当前时间
+        @param event_type: 事件类型
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @param uid: uid
+        @param channel: 通道
+        @param image: 推送图片链接
+        @return: bool
+        """
+        app_bundle_id = appBundleId if appBundleId else app_bundle_id
+        try:
+            client_id = HONORPUSH_CONFIG[app_bundle_id]['client_id']
+            client_secret = HONORPUSH_CONFIG[app_bundle_id]['client_secret']
+            app_id = HONORPUSH_CONFIG[app_bundle_id]['app_id']
+            get_access_token_url = 'https://iam.developer.hihonor.com/auth/token'
+            post_data = {
+                'grant_type': 'client_credentials',
+                'client_id': client_id,
+                'client_secret': client_secret
+            }
+            headers = {'Content-Type': 'application/x-www-form-urlencoded'}
+            access_token_response = requests.post(get_access_token_url, data=post_data, headers=headers)
+            access_result = access_token_response.json()
+            authorization_token = 'Bearer ' + access_result['access_token']
+            # 发送推送
+            push_url = 'https://push-api.cloud.hihonor.com/api/v1/{}/sendMessage'.format(app_id)
+            headers = {'Content-Type': 'application/json', 'Authorization': authorization_token,
+                       'timestamp': str(n_time * 1000)}
+            extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
+                          'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
+                          'uid': uid, 'channel': channel
+                          }
+            push_data = {
+                "android": {
+                    "data": json.dumps(extra_data),
+                    "notification": {
+                        "body": msg_text,
+                        "title": msg_title,
+                        "clickAction": {
+                            "type": 3
+                        }
+                    }
+                },
+                "notification": {},
+                "token": [token_val]
+            }
+            response = requests.post(push_url, json=push_data, headers=headers)
+            LOGGER.info("荣耀推送返回值:{}".format(response.json()))
+            return True
+        except Exception as e:
+            LOGGER.info("荣耀推送异常:{}".format(repr(e)))
+            return False
+
     @staticmethod
     def jpush_transparent_transmission(msg_title, msg_text, app_bundle_id, token_val, extra_data):
         """