Browse Source

修复小米、oppo不推送不打印错误日志的问题

linhaohong 11 months ago
parent
commit
af11543459
1 changed files with 96 additions and 85 deletions
  1. 96 85
      Service/DevicePushService.py

+ 96 - 85
Service/DevicePushService.py

@@ -450,14 +450,12 @@ class DevicePushService:
                     else:
                         push_channel = 'device_reminder'
                     channel_id = XM_PUSH_CHANNEL_ID[push_channel]
-                    cls.do_xmpush(channel_id=channel_id, **push_kwargs)
-                    push_result = True
+                    push_result = cls.do_xmpush(channel_id=channel_id, **push_kwargs)
                 elif push_type == 5:  # android vivopush
                     push_result = PushObject.android_vivopush(**push_kwargs)
                 elif push_type == 6:  # android oppopush
                     channel_id = 'DEVICE_REMINDER'
-                    cls.do_oppopush(channel_id=channel_id, **push_kwargs)
-                    push_result = True
+                    push_result = cls.do_oppopush(channel_id=channel_id, **push_kwargs)
                 elif push_type == 7:  # android meizupush
                     push_result = PushObject.android_meizupush(**push_kwargs)
                 elif push_type == 8:  # android honorpush
@@ -647,38 +645,45 @@ class DevicePushService:
         """
         android 国内小米APP消息提醒推送
         """
-        url = 'https://api.xmpush.xiaomi.com/v3/message/regid'
-        app_secret = XMPUSH_CONFIG[appBundleId]
-        # payload = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
-        #            'received_at': n_time, 'event_time': n_time, 'event_type': event_type,
-        #            'uid': uid, 'channel': channel
-        #            }
-        data = {
-            'title': msg_title,
-            'description': msg_text,
-            'payload': 'payload',
-            'restricted_package_name': appBundleId,
-            'registration_id': token_val,
-            'extra.channel_id': channel_id,
-            'extra.alert': 'Motion',
-            'extra.msg': '',
-            'extra.sound': 'sound.aif',
-            'extra.zpush': '1',
-            'extra.received_at': n_time,
-            'extra.event_time': n_time,
-            'extra.event_type': event_type,
-            'extra.uid': uid,
-            'extra.channel': channel,
-        }
-        if event_type in [606, 607]:
-            data['extra.sound_uri'] = 'android.resource://com.ansjer.zccloud_ab/raw/phone_call'
-        headers = {
-            'Authorization': 'key={}'.format(app_secret)
-        }
-        response = requests.post(url, data=data, headers=headers)
-        if response.status_code == 200:
-            LOGGING.info('uid:{},时间:{}小米推送结果:{}'.format(uid, n_time, response.json()))
-            return response.json()
+        try:
+            url = 'https://api.xmpush.xiaomi.com/v3/message/regid'
+            app_secret = XMPUSH_CONFIG[appBundleId]
+            # payload = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
+            #            'received_at': n_time, 'event_time': n_time, 'event_type': event_type,
+            #            'uid': uid, 'channel': channel
+            #            }
+            data = {
+                'title': msg_title,
+                'description': msg_text,
+                'payload': 'payload',
+                'restricted_package_name': appBundleId,
+                'registration_id': token_val,
+                'extra.channel_id': channel_id,
+                'extra.alert': 'Motion',
+                'extra.msg': '',
+                'extra.sound': 'sound.aif',
+                'extra.zpush': '1',
+                'extra.received_at': n_time,
+                'extra.event_time': n_time,
+                'extra.event_type': event_type,
+                'extra.uid': uid,
+                'extra.channel': channel,
+            }
+            if event_type in [606, 607]:
+                data['extra.sound_uri'] = 'android.resource://com.ansjer.zccloud_ab/raw/phone_call'
+            headers = {
+                'Authorization': 'key={}'.format(app_secret)
+            }
+            response = requests.post(url, data=data, headers=headers)
+            if response.status_code == 200:
+                LOGGING.info('uid:{},时间:{}小米推送结果:{}'.format(uid, n_time, response.json()))
+                return True
+            ERROR_INFO_LOGGER.info('小米推送异常,uid:{},状态码{},时间:{},结果:{},'
+                                   .format(uid, response.status_code, n_time, response.json()))
+        except Exception as e:
+            ERROR_INFO_LOGGER.info('小米推送异常,uid:{},时间:{},error_line:{},error_msg:{}'.
+                                   format(uid, n_time, e.__traceback__.tb_lineno, repr(e)))
+            return False
 
     @staticmethod
     def do_oppopush(channel_id, uid, channel, appBundleId, token_val, event_type, n_time,
@@ -686,56 +691,62 @@ class DevicePushService:
         """
         android 国内oppo APP消息提醒推送
         """
-        if event_type in [606, 607]:
-            channel_id = XM_PUSH_CHANNEL_ID['push_to_talk']
-        app_key = OPPOPUSH_CONFIG[appBundleId]['Key']
-        master_secret = OPPOPUSH_CONFIG[appBundleId]['Secret']
-        url = 'https://api.push.oppomobile.com/'
-        now_time = str(round(time.time() * 1000))
-        # 1、实例化一个sha256对象
-        sha256 = hashlib.sha256()
-        # 2、调用update方法进行加密
-        sha256.update((app_key + now_time + master_secret).encode('utf-8'))
-        # 3、调用hexdigest方法,获取加密结果
-        sign = sha256.hexdigest()
-        # 获取auth_token
-        get_token_url = url + 'server/v1/auth'
-        post_data = {
-            'app_key': app_key,
-            'sign': sign,
-            'timestamp': now_time
-        }
-        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
-        response = requests.post(get_token_url, data=post_data, headers=headers)
-        result = response.json()
-        # 发送推送
-        push_url = url + 'server/v1/message/notification/unicast'
-        extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
-                      'received_at': n_time, 'event_time': n_time, 'event_type': event_type,
-                      'uid': uid, 'channel': channel}
-        message = {
-            "target_type": 2,
-            "target_value": token_val,
-            "notification": {
-                "title": msg_title,
-                "content": msg_text,
-                'channel_id': channel_id,
-                'action_parameters': extra_data,
-                'click_action_type': 4,
-                'click_action_activity': 'com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity'
-            }
-        }
-        push_data = {
-            'auth_token': result['data']['auth_token'],
-            'message': json.dumps(message)
-        }
-
-        response = requests.post(push_url, data=push_data, headers=headers)
-        if response.status_code == 200:
-            LOGGING.info("oppo推送返回值:{},uid:{},time:{},event:{}".format(response.json(), uid, now_time, event_type))
+        try:
             if event_type in [606, 607]:
-                PushObject.jpush_transparent_transmission(msg_title, msg_text, appBundleId, jg_token_val, extra_data)
-            return response.json()
+                channel_id = XM_PUSH_CHANNEL_ID['push_to_talk']
+            app_key = OPPOPUSH_CONFIG[appBundleId]['Key']
+            master_secret = OPPOPUSH_CONFIG[appBundleId]['Secret']
+            url = 'https://api.push.oppomobile.com/'
+            now_time = str(round(time.time() * 1000))
+            # 1、实例化一个sha256对象
+            sha256 = hashlib.sha256()
+            # 2、调用update方法进行加密
+            sha256.update((app_key + now_time + master_secret).encode('utf-8'))
+            # 3、调用hexdigest方法,获取加密结果
+            sign = sha256.hexdigest()
+            # 获取auth_token
+            get_token_url = url + 'server/v1/auth'
+            post_data = {
+                'app_key': app_key,
+                'sign': sign,
+                'timestamp': now_time
+            }
+            headers = {'Content-Type': 'application/x-www-form-urlencoded'}
+            response = requests.post(get_token_url, data=post_data, headers=headers)
+            result = response.json()
+            # 发送推送
+            push_url = url + 'server/v1/message/notification/unicast'
+            extra_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
+                          'received_at': n_time, 'event_time': n_time, 'event_type': event_type,
+                          'uid': uid, 'channel': channel}
+            message = {
+                "target_type": 2,
+                "target_value": token_val,
+                "notification": {
+                    "title": msg_title,
+                    "content": msg_text,
+                    'channel_id': channel_id,
+                    'action_parameters': extra_data,
+                    'click_action_type': 4,
+                    'click_action_activity': 'com.ansjer.zccloud_a.AJ_MainView.AJ_Home.AJMainActivity'
+                }
+            }
+            push_data = {
+                'auth_token': result['data']['auth_token'],
+                'message': json.dumps(message)
+            }
+
+            response = requests.post(push_url, data=push_data, headers=headers)
+            if response.status_code == 200:
+                LOGGING.info("oppo推送返回值:{},uid:{},time:{},event:{}".format(response.json(), uid, now_time, event_type))
+                if event_type in [606, 607]:
+                    PushObject.jpush_transparent_transmission(msg_title, msg_text, appBundleId, jg_token_val, extra_data)
+                return True
+            return False
+        except Exception as e:
+            ERROR_INFO_LOGGER.info('oppo推送异常,uid:{},time:{},error_line:{},error_msg:{}'.
+                                   format(uid, n_time, e.__traceback__.tb_lineno, repr(e)))
+            return False
 
     @classmethod
     def async_send_picture_push(cls, push_type, aws_s3_client, bucket, key, uid, appBundleId, token_val,