Jelajahi Sumber

修改fcm和apns函数

locky 3 tahun lalu
induk
melakukan
21197c2606
2 mengubah file dengan 11 tambahan dan 8 penghapusan
  1. 4 4
      Controller/DetectControllerV2.py
  2. 7 4
      Service/GatewayService.py

+ 4 - 4
Controller/DetectControllerV2.py

@@ -246,7 +246,7 @@ class NotificationView(View):
                     if not have_dkey:
                         logger.info('准备推送:{}, {}'.format(uid, request_dict))
                         # if (is_st == 1 or is_st == 3) and (push_type == 0 or push_type == 1):
-                        push_thread = threading.Thread(target=self.push_thread_test, args=(push_type, aws_s3_client, uid, appBundleId, token_val, event_type, n_time, msg_title, msg_text))
+                        push_thread = threading.Thread(target=self.push_thread_test, args=(push_type, aws_s3_client, uid, appBundleId, token_val, event_type, n_time, msg_title, msg_text, channel))
                         push_thread.start()
                         if push_type == 0:  # ios apns
                             do_apns_code = self.do_apns(**kwag_args)
@@ -371,7 +371,7 @@ class NotificationView(View):
         elif push_type == 1:
             GatewayPushService.android_fcm_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, image_url)
 
-    def push_thread_test(self, push_type, aws_s3_client, uid, appBundleId, token_val, event_type, n_time, msg_title, msg_text):
+    def push_thread_test(self, push_type, aws_s3_client, uid, appBundleId, token_val, event_type, n_time, msg_title, msg_text, channel):
         logger = logging.getLogger('info')
         logger.info('推送图片测试:{} {} {} {} {} {} {} {}'.format(push_type, uid, appBundleId, token_val, event_type, n_time, msg_title, msg_text))
         time.sleep(3)
@@ -381,9 +381,9 @@ class NotificationView(View):
             image_url = aws_s3_client.generate_presigned_url('get_object', Params={'Bucket': bucket, 'Key': thumbspng}, ExpiresIn=86400)
             logger.info('推送图片url:{}'.format(image_url))
             if push_type == 0:
-                GatewayPushService.ios_apns_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, image_url)
+                GatewayPushService.ios_apns_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, uid, channel, image_url)
             elif push_type == 1:
-                GatewayPushService.android_fcm_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, image_url)
+                GatewayPushService.android_fcm_push(uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, uid, channel, image_url)
         except Exception as e:
             logger.info('推送图片测试异常:{}'.format(e))
 

+ 7 - 4
Service/GatewayService.py

@@ -38,13 +38,14 @@ class GatewayPushService:
 
     # ios apns 推送
     @staticmethod
-    def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, launch_image=None):
+    def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
+                      uid='', channel='1', launch_image=None):
         logger = logging.getLogger('info')
         try:
             pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
             cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
             alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
-            push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1',
+            push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
                          'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
                          }
             payload = apns2.Payload(alert=alert, custom=push_data, sound='default', mutable_content=True)
@@ -57,13 +58,15 @@ class GatewayPushService:
 
     # android fcm 推送
     @staticmethod
-    def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, image=''):
+    def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
+                         uid='', channel='1', image=''):
         logger = logging.getLogger('info')
         try:
             serverKey = FCM_CONFIG[app_bundle_id]
             push_service = FCMNotification(api_key=serverKey)
             push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
-                         'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
+                         'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
+                         'uid': uid, 'channel': channel
                          }
             result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
                                                        message_body=msg_text, data_message=push_data,