chenjunkai 6 gadi atpakaļ
vecāks
revīzija
7dead7739a
1 mainītis faili ar 35 papildinājumiem un 1 dzēšanām
  1. 35 1
      Controller/Test.py

+ 35 - 1
Controller/Test.py

@@ -37,6 +37,7 @@ class Test(View):
         # return JsonResponse(status=200,data={
         #         'code': 173,
         #         'msg': 'data is not exist'})
+        return self.do_fcm_push(request)
         return self.do_gcm_push(request)
         return self.do_alipay_query_status()
 
@@ -67,6 +68,39 @@ class Test(View):
             print("not paid...")
             return response.json(404)
 
+    def do_fcm_push(self, request):
+        rg_id = request.GET.get('rg_id', '')
+        serverKey = request.GET.get('serverKey', '')
+        # Send to single device.
+        from pyfcm import FCMNotification
+
+        push_service = FCMNotification(api_key="<api-key>")
+
+        # OR initialize with proxies
+
+        # proxy_dict = {
+        #     "http": "http://127.0.0.1",
+        #     "https": "http://127.0.0.1",
+        # }
+        # push_service = FCMNotification(api_key="<api-key>", proxy_dict=proxy_dict)
+        push_service = FCMNotification(api_key=serverKey)
+
+        # Your api-key can be gotten from:  https://console.firebase.google.com/project/<project-name>/settings/cloudmessaging
+
+        registration_id = rg_id
+        message_title = "Uber update"
+        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"}
+        message_body = json.dumps(data)
+        result = push_service.notify_single_device(registration_id=registration_id, message_title=message_title,
+                                                   message_body=message_body)
+        from var_dump import var_dump
+        var_dump(result)
+
+        response = ResponseObject()
+        return response.json(0, result)
+
     def do_apns(self, request_dict):
         token_val = request_dict.get('token_val', None)
         pem_path = os.path.join(BASE_DIR, 'Ansjer/file/apns_pem/apns-dev-test.pem')
@@ -123,7 +157,7 @@ class Test(View):
             return response.json(444)
         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": "XFDJUHUIOKJHYTGSFFDR", "zpush": "1"}
         json_data = {
             "collapse_key": "WhatYouWant",
             "data": data,