瀏覽代碼

封装发送通知/透传推送代码为函数

locky 2 年之前
父節點
當前提交
0c6f845b71
共有 1 個文件被更改,包括 78 次插入43 次删除
  1. 78 43
      Service/HuaweiPushService/HuaweiPushService.py

+ 78 - 43
Service/HuaweiPushService/HuaweiPushService.py

@@ -39,63 +39,98 @@ class HuaweiPushObject:
             '华为推送参数: token_val:{}, msg_title:{}, msg_text:{}, image_url:{}, uid:{}, event_type:{}, n_time:{}'.format(
                 token_val, msg_title, msg_text, image_url, uid, event_type, n_time))
 
+        self.send_notify_message(msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val, logger)
         if event_type == '606':
-            message_data = {'uid': uid, 'event_type': event_type, 'event_time': n_time}
-            notification = None
-            android = messaging.AndroidConfig(
-                collapse_key=-1,
-                urgency=messaging.AndroidConfig.HIGH_PRIORITY,
-                ttl='10000s',
-                bi_tag='the_sample_bi_tag_for_receipt_service'
-            )
-        else:
-            message_data = None
-            msg_title = '设备昵称: {}'.format(msg_title)
-            notification = messaging.Notification(
-                title=msg_title,
-                body=msg_text,
-                image=image_url
-            )
+            self.send_data_message(uid, event_type, n_time, token_val, logger)
 
-            # 自定义键值对
-            data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'type': 1, 'uid': uid,
-                    'nickname': nickname, 'event_type': event_type, 'received_at': n_time, 'event_time': n_time,
-                    'intent': 'intent://com.vivo.pushvideo/detail?#Intent;scheme=vpushscheme;launchFlags=0x10000000;S.uid={};S.event_type={};S.event_time={};end'.format(
-                        uid, event_type, n_time)
-                    }
-            data = json.dumps(data)
-            # 推送通知内容配置
-            android_notification = self.android_notification(msg_title, msg_text)
-            # 安卓配置
-            android = messaging.AndroidConfig(
-                data=data,
-                collapse_key=-1,
-                urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
-                ttl='10000s',
-                bi_tag='the_sample_bi_tag_for_receipt_service',
-                notification=android_notification,
-                category='DEVICE_REMINDER'
-            )
+    def send_notify_message(self, msg_title, msg_text, image_url, uid, nickname, event_type, n_time, token_val, logger):
+        """
+        发送通知推送
+        @param msg_title:
+        @param msg_text:
+        @param image_url:
+        @param uid:
+        @param nickname:
+        @param event_type:
+        @param n_time:
+        @param token_val:
+        @param logger:
+        @return: None
+        """
+        msg_title = '设备昵称: {}'.format(msg_title)
+        notification = messaging.Notification(
+            title=msg_title,
+            body=msg_text,
+            image=image_url
+        )
+
+        # 自定义键值对
+        data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'type': 1, 'uid': uid,
+                'nickname': nickname, 'event_type': event_type, 'received_at': n_time, 'event_time': n_time,
+                'intent': 'intent://com.vivo.pushvideo/detail?#Intent;scheme=vpushscheme;launchFlags=0x10000000;S.uid={};S.event_type={};S.event_time={};end'.format(
+                    uid, event_type, n_time)
+                }
+        data = json.dumps(data)
+        # 推送通知内容配置
+        android_notification = self.android_notification(msg_title, msg_text)
+        # 安卓配置
+        android = messaging.AndroidConfig(
+            data=data,
+            collapse_key=-1,
+            urgency=messaging.AndroidConfig.NORMAL_PRIORITY,
+            ttl='10000s',
+            bi_tag='the_sample_bi_tag_for_receipt_service',
+            notification=android_notification,
+            category='DEVICE_REMINDER'
+        )
 
         message = messaging.Message(
-            data=message_data,
             notification=notification,
             android=android,
             token=[token_val]
         )
 
         try:
-            # Case 1: Local CA sample code
-            # response = messaging.send_message(message, verify_peer='../Push-CA-Root.pem')
-            # Case 2: No verification of HTTPS's certificate
-            # response = messaging.send_message(message)
-            # Case 3: use certifi Library
             import certifi
             response = messaging.send_message(message, verify_peer=certifi.where())
-            logger.info('华为推送响应: {}'.format(json.dumps(vars(response))))
+            logger.info('华为通知推送响应: {}'.format(json.dumps(vars(response))))
+            assert (response.code == '80000000')
+        except Exception as e:
+            logger.info('华为通知推送异常: {}'.format(repr(e)))
+
+    @staticmethod
+    def send_data_message(uid, event_type, n_time, token_val, logger):
+        """
+        发送透传推送
+        @param uid:
+        @param event_type:
+        @param n_time:
+        @param token_val:
+        @param logger:
+        @return: None
+        """
+        data = {'uid': uid, 'event_type': event_type, 'event_time': n_time}
+        data = json.dumps(data)
+        android = messaging.AndroidConfig(
+            collapse_key=-1,
+            urgency=messaging.AndroidConfig.HIGH_PRIORITY,
+            ttl='10000s',
+            bi_tag='the_sample_bi_tag_for_receipt_service'
+        )
+
+        message = messaging.Message(
+            data=data,
+            android=android,
+            token=[token_val]
+        )
+
+        try:
+            import certifi
+            response = messaging.send_message(message, verify_peer=certifi.where())
+            logger.info('华为透传推送响应: {}'.format(json.dumps(vars(response))))
             assert (response.code == '80000000')
         except Exception as e:
-            logger.info('华为推送异常: {}'.format(repr(e)))
+            logger.info('华为透传推送异常: {}'.format(repr(e)))
 
     @staticmethod
     def android_notification(msg_title, msg_text):