chenjunkai 6 年之前
父节点
当前提交
7658194d77
共有 1 个文件被更改,包括 26 次插入14 次删除
  1. 26 14
      Controller/DetectController.py

+ 26 - 14
Controller/DetectController.py

@@ -108,7 +108,8 @@ class NotificationView(View):
         uidToken = request_dict.get('uidToken', None)
         utko = UidTokenObject(uidToken)
         uid = utko.UID
-        uaqs = UID_App.objects.filter(uid=uid).values('token_val', 'app_type', 'appBundleId', 'push_type', 'uid')
+        uaqs = UID_App.objects.filter(uid=uid).\
+            values('token_val', 'app_type', 'appBundleId', 'push_type', 'uid')
         if uaqs.exists():
             push_type = uaqs[0]['push_type']
             # ios apns
@@ -195,40 +196,51 @@ class NotificationView(View):
         else:
             return response.json(0)
 
-    def do_gmc(self, request_dict, response):
+    def do_gmc(self, request_dict, uaql, response):
 
         n_time = request_dict.get('n_time')
-        n_type = request_dict.get('n_type')
-        msg = {'n_time': n_time, 'n_type': n_type}
+        appBundleId = uaql['appBundleId']
+        token_val = uaql['token_val']
+        uid = uaql['uid']
+        gcm_config = {
+            'xxx': 'key'
+        }
+        serverKey = gcm_config[appBundleId]
+
+        msg = {'n_time': n_time, 'uid': uid}
         json_data = {
             "collapse_key": "WhatYouWant",
             "data": msg,
             "delay_while_idle": False,
             "time_to_live": 3600,
-            "registration_ids": [
-                'eSooD4fAARg:APA91bEPenBPnSn5aXIQk56QdLOQ1Mu3hevHsekP_0eDpg458y2ZMBP6By2rVsGYIoxZrXxvkkptPKUE9CmUygBxaZXABddUWB9FyLlznRFerC7RG9X5PsEOH58xK9_aTUdkT7p6Ocld']
+            "registration_ids": [token_val]
         }
 
         url = 'https://android.googleapis.com/gcm/send'
-        serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I"
+        # serverKey = "AAAAb9YP3rk:APA91bHu8u-CTpcd0g6lKPo0WNVqCi8jZub1cPPbSAY9AucT1HxlF65ZDUko9iG8q2ch17bwu9YWHpK1xI1gHSRXCslLvZlXEmHZC0AG3JKg15XuUvlFKACIajUFV-pOeGRT8tM6-31I"
 
         data = json.dumps(json_data).encode('utf-8')
         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, response):
-        device_token = request_dict.get('device_token')
-
+    def do_apns(self, request_dict, uaql, response):
+        token_val = uaql['token_val']
+        n_time = request_dict.get('n_time')
+        appBundleId = uaql['appBundleId']
+        uid = uaql['uid']
+        apns_config = {'appbundleId': {'pem_path': 'xxxx', 'topic': 'topic'}}
         try:
-            daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103))
-            print(daytime)
-            pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem')
+            # daytime = time.strftime("%Y%m%d%H%M", time.localtime(1547256103))
+            # print(daytime)
+            pem_path = os.path.join(BASE_DIR, apns_config[appBundleId]['topic'])
+            # pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns-dev.pem')
             cli = apns2.APNSClient(mode="dev", client_cert=pem_path, password='111111')
+            body = json.dumps({'uid': uid, 'n_time': n_time})
             alert = apns2.PayloadAlert(body="body!", title="title!")
             payload = apns2.Payload(alert=alert)
             n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
-            res = cli.push(n=n, device_token=device_token, topic='com.ansjer.zsavcloud')
+            res = cli.push(n=n, device_token=token_val, topic=apns_config[appBundleId]['pem_path'])
             # assert res.status_code == 200, res.reason
             # assert res.apns_id
             if res.status_code == 200: