Selaa lähdekoodia

fix:更新推送配置,多项优化

chenjunkai 6 vuotta sitten
vanhempi
commit
b49b3e4c5f
3 muutettua tiedostoa jossa 85 lisäystä ja 22 poistoa
  1. 79 17
      Controller/DetectController.py
  2. 4 4
      Controller/Test.py
  3. 2 1
      requirements.txt

+ 79 - 17
Controller/DetectController.py

@@ -15,6 +15,7 @@ import json
 import os
 import time
 import urllib
+from pyfcm import FCMNotification
 
 import apns2
 import jpush as jpush
@@ -163,7 +164,7 @@ class DetectControllerView(View):
                     'push_type': push_type,
                     'token_val': token_val,
                     'updTime': nowTime,
-                    'lang':lang
+                    'lang': lang
                 }
                 uid_set_qs.update(detect_status=status, updTime=nowTime)
                 UidPushModel.objects.filter(userID_id=userID, m_code=m_code, uid_set__uid=uid). \
@@ -202,7 +203,7 @@ class DetectControllerView(View):
                     'm_code': m_code,
                     'addTime': nowTime,
                     'updTime': nowTime,
-                    'lang':lang
+                    'lang': lang
                 }
                 # 绑定设备推送
                 UidPushModel.objects.create(**uid_push_create_dict)
@@ -263,8 +264,9 @@ class NotificationView(View):
         uid_set_qs = UidSetModel.objects.filter(uid=uid, detect_status=1)
         if uid_set_qs.exists():
             uid_set_id = uid_set_qs[0].id
+            nickname = uid_set_qs[0].nickname
             uid_push_qs = UidPushModel.objects.filter(uid_set__id=uid_set_id). \
-                values('token_val', 'app_type', 'appBundleId', 'push_type', 'userID_id', 'userID__NickName','lang')
+                values('token_val', 'app_type', 'appBundleId', 'push_type', 'userID_id', 'userID__NickName', 'lang')
             if uid_set_qs.exists():
                 redisObj = RedisObject(db=6)
                 pkey = '{uid}_{channel}_ptl'.format(uid=uid, channel=channel)
@@ -281,13 +283,14 @@ class NotificationView(View):
                     push_type = up['push_type']
                     # ios apns
                     if push_type == 0:
-                        self.do_apns(request_dict, up, response, uid,channel)
+                        self.do_apns(request_dict, up, response, uid, channel, nickname)
                     # android gcm
                     elif push_type == 1:
-                        self.do_gmc(request_dict, up, response, uid,channel)
+                        self.do_fcm(request_dict, up, response, uid, channel, nickname)
+                        # self.do_gmc(request_dict, up, response, uid, channel,nickname)
                     # android jpush
                     elif push_type == 2:
-                        self.do_jpush(request_dict, up, response, uid,channel)
+                        self.do_jpush(request_dict, up, response, uid, channel, nickname)
                     # self.do_save_equipment_info(ua, n_time, channel, event_type, is_st)
                 # 需求不一样,所以这么做的
                 self.do_bulk_create_info(uid_push_qs, n_time, channel, event_type, is_st, uid)
@@ -305,7 +308,7 @@ class NotificationView(View):
         else:
             return JsonResponse(status=200, data={'code': 404, 'msg': 'data is not exist'})
 
-    def do_jpush(self, request_dict, uaql, response, uid,channel):
+    def do_jpush(self, request_dict, uaql, response, uid, channel, nickname):
         event_type = request_dict.get('event_type', None)
         jpush_config = {
             'com.ansjer.accloud_ab': {
@@ -358,15 +361,22 @@ class NotificationView(View):
         # push.audience = jpush.all_
         push.audience = jpush.registration_id(token_val)
         push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
-                     "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1"}
-        # push.message = jpush.message('Motion', extras=push_data, title='KPNS', content_type='text')
-        n_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
+                     "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
+        n_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(n_time)))
         if lang == 'cn':
-            send_text = '{uid} 通道:{channel} 日期:{date}'.format(uid=uid,channel=channel,date=n_date)
+            if nickname:
+                message_title = "周视({nickname})".format(nickname=nickname)
+            else:
+                message_title = "周视({uid})".format(uid=uid)
+            send_text = '通道:{channel} 日期:{date}'.format(channel=channel, date=n_date)
         else:
-            send_text = '{uid} channel:{channel} date:{date}'.format(uid=uid,channel=channel,date=n_date)
+            if nickname:
+                message_title = "zosi({nickname})".format(nickname=nickname)
+            else:
+                message_title = "zosi({uid})".format(uid=uid)
+            send_text = 'channel:{channel} date:{date}'.format(channel=channel, date=n_date)
         android = jpush.android(alert="Hello, Android msg", priority=1, style=1, alert_type=1,
-                                big_text=send_text,
+                                big_text=send_text, title=message_title,
                                 extras=push_data)
 
         push.notification = jpush.notification(android=android)
@@ -382,7 +392,8 @@ class NotificationView(View):
         else:
             return response.json(0)
 
-    def do_gmc(self, request_dict, uaql, response, uid,channel):
+    '''
+    def do_gmc(self, request_dict, uaql, response, uid, channel):
 
         n_time = request_dict.get('n_time')
         appBundleId = uaql['appBundleId']
@@ -413,10 +424,48 @@ class NotificationView(View):
         headers = {'Content-Type': 'application/json', 'Authorization': 'key=%s' % serverKey}
         req = requests.post(url, data, headers=headers)
         return response.json(0)
+    '''
 
-    def do_apns(self, request_dict, uaql, response, uid,channel):
+    def do_fcm(self, request_dict, uaql, response, uid, channel, nickname):
+        n_time = request_dict.get('n_time')
+        appBundleId = uaql['appBundleId']
+        token_val = uaql['token_val']
+        lang = uaql['lang']
+        fcm_config = {
+            'com.ansjer.zccloud_a': 'AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I',
+            'com.ansjer.loocamccloud_a': 'AAAAb9YP3rk:APA91bFCgd-kbVmpK4EVpfdHH_PJZQCYTkOGnTZdIuBWEz2r7aMRsJYHOH3sB-rwcbaRWgnufTyjX9nGQxb6KxQbWVk4ah_H-M3IqGh6Mb60WQQAuR33V6g_Jes5pGL6ViuIxGHqVMaR',
+            'com.ansjer.loocamdcloud_a': 'AAAAb9YP3rk:APA91bGw2I2KMD4i-5T7nZO_wB8kuAOuqgyqe5rxmY-W5qkpYEx9IL2IfmC_qf6B_xOyjIDDSjckvMo-RauN__SEoxvAkis7042GRkoKpw7cjZ_H8lC-d50PC0GclPzccrOGFusyKbFY',
+            'com.ansjer.customizedb_a': 'AAAAb9YP3rk:APA91bE7kI4vcm-9h_CJNFlOZfc-xwP4Btn6AnjOrwoKV6fgYN7fdarkO76sYxVZiAbDnxsFfOJyP7vQfwyan6mdjuyD5iHdt_XgO22VqniC0vA1V4GJiCS8Tp7LxIX8JVKZl9I_Powt',
+            'com.ansjer.customizeda_a': 'AAAAb9YP3rk:APA91bF0HzizVWDc6dKzobY9fsaKDK4veqkOZehDXshVXs8pEEvNWjR_YWbhP60wsRYCHCal8fWN5cECVOWNMMzDsfU88Ty2AUl8S5FtZsmeDTkoGntQOswBr8Ln7Fm_LAp1VqTf9CpM',
+        }
+        serverKey = fcm_config[appBundleId]
+        event_type = request_dict.get('event_type', None)
+        push_service = FCMNotification(api_key=serverKey)
+        registration_id = token_val
+        n_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(n_time)))
+        if lang == 'cn':
+            if nickname:
+                message_title = "周视({nickname})".format(nickname=nickname)
+            else:
+                message_title = "周视({uid})".format(uid=uid)
+            send_text = '通道:{channel} 日期:{date}'.format(channel=channel, date=n_date)
+        else:
+            if nickname:
+                message_title = "zosi({nickname})".format(nickname=nickname)
+            else:
+                message_title = "zosi({uid})".format(uid=uid)
+            send_text = 'channel:{channel} date:{date}'.format(channel=channel, date=n_date)
+        data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
+                "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
+        result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title,
+                                                   message_body=send_text, data_message=data)
+        response = ResponseObject()
+        return response.json(0, result)
+
+    def do_apns(self, request_dict, uaql, response, uid, channel, nickname):
         event_type = request_dict.get('event_type', None)
         token_val = uaql['token_val']
+        lang = uaql['lang']
         n_time = request_dict.get('n_time')
         appBundleId = uaql['appBundleId']
         apns_config = {
@@ -425,13 +474,26 @@ class NotificationView(View):
                 'password': '111111'
             }
         }
+        n_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(n_time)))
+        if lang == 'cn':
+            if nickname:
+                message_title = "周视({nickname})".format(nickname=nickname)
+            else:
+                message_title = "周视({uid})".format(uid=uid)
+            send_text = '通道:{channel} 日期:{date}'.format(channel=channel, date=n_date)
+        else:
+            if nickname:
+                message_title = "zosi({nickname})".format(nickname=nickname)
+            else:
+                message_title = "zosi({uid})".format(uid=uid)
+            send_text = 'channel:{channel} date:{date}'.format(channel=channel, date=n_date)
         try:
             cli = apns2.APNSClient(mode="dev", client_cert=apns_config[appBundleId]['pem_path'],
                                    password=apns_config[appBundleId]['password'])
             push_data = {"alert": "Motion ", "event_time": n_time, "event_type": event_type, "msg": "",
-                         "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1"}
+                         "received_at": n_time, "sound": "sound.aif", "uid": uid, "zpush": "1", "channel": channel}
             body = json.dumps(push_data)
-            alert = apns2.PayloadAlert(body=body, title="title!")
+            alert = apns2.PayloadAlert(body=body, title=message_title)
             payload = apns2.Payload(alert=alert)
             n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
             res = cli.push(n=n, device_token=token_val, topic=appBundleId)

+ 4 - 4
Controller/Test.py

@@ -91,10 +91,10 @@ class Test(View):
         # Your api-key can be gotten from:  https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging
 
         registration_id = rg_id
-        message_title = "Zosi Smart(Camera 007)"
+        message_title = "Zosi Smart(xxxx 007)"
         now_time = int(time.time())
         data = {"alert": "Motion ", "event_time": now_time, "event_type": "51", "msg": "",
-                "received_at": now_time, "sound": "sound.aif", "uid": "XFDJUHUIOKJHYTGSFFDR", "zpush": "1"}
+                "received_at": now_time, "sound": "sound.aif", "uid": "98UXAA8BRPA35VAL111A", "zpush": "1"}
         # message_body = json.dumps(data)
 
         message_body = '警告:Motion Channel:1 日期:{tt}'.format(
@@ -108,7 +108,7 @@ class Test(View):
             var_dump('22222222')
 
             result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title,
-                                                       message_body=message_body, data_message=data,time_to_live=3600)
+                                                       message_body=message_body, data_message=data)
         else:
             var_dump('333333')
 
@@ -214,7 +214,7 @@ class Test(View):
                      "received_at": n_time, "sound": "sound.aif", "uid": 'xoxoxoxoxoxoxoxoxoxo', "zpush": "1"}
 
         android = jpush.android(alert="Hello, Android msg", priority=1, style=1, alert_type=1, big_text='ssssssssssssssssss',
-                                extras=push_data)
+                                extras=push_data,title='fffff')
 
         push.notification = jpush.notification(android=android)
         # push.notification = jpush.notification(alert="uid:xoxoxoxoxoxoxoxoxoxo", android=android)

+ 2 - 1
requirements.txt

@@ -26,4 +26,5 @@ boto
 django-ratelimit
 paypalrestsdk
 pymongo
-apns2-client
+apns2-client
+pyfcm