Browse Source

android fcm推送V1 修复bug

linhaohong 1 year ago
parent
commit
fd42a1b433
2 changed files with 13 additions and 8 deletions
  1. 6 0
      AnsjerPush/config.py
  2. 7 8
      Service/PushService.py

+ 6 - 0
AnsjerPush/config.py

@@ -6,6 +6,9 @@ import os
 import datetime
 import logging
 
+import firebase_admin
+from firebase_admin import credentials
+
 # 日志配置
 LOGGER = logging.getLogger('info')
 
@@ -195,3 +198,6 @@ XM_PUSH_CHANNEL_ID = {
     'service_reminder': 104552,    # 服务提醒
     'sys_notification': 104553     # 系统通知
 }
+
+firebase_admin.initialize_app(credentials.Certificate(
+    BASE_DIR + '/static/fcm/adcloud-fdf9b-firebase-adminsdk-dcvn1-d53e047cc8.json'))

+ 7 - 8
Service/PushService.py

@@ -201,11 +201,9 @@ class PushObject:
         @return: bool
         """
         try:
-            # See documentation on defining a message payload.
-            cred = credentials.Certificate(
-                BASE_DIR + '/static/fcm/adcloud-fdf9b-firebase-adminsdk-dcvn1-d53e047cc8.json')
-            firebase_admin.initialize_app(cred)
-            push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
+            event_type = str(event_type)
+            n_time = str(n_time)
+            push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
                          'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
                          'uid': uid, 'channel': channel
                          }
@@ -216,14 +214,15 @@ class PushObject:
             message = messaging.Message(
                 notification=messaging.Notification(
                     title=msg_title,
-                    body=msg_text
+                    body=msg_text,
+                    image=image
                 ),
                 data=push_data,
-                token=token_val,
+                token=token_val
             )
             # Send a message to the device corresponding to the provided
             # registration token.
-            result = messaging.send(message, dry_run=True)
+            result = messaging.send(message)
             LOGGER.info('fcm推送结果:{}'.format(result))
             return True
         except Exception as e: