Browse Source

更新fcm v1推送

linhaohong 1 year ago
parent
commit
81b64c9911

+ 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')
 
@@ -207,3 +210,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'))

+ 1 - 1
Controller/ComboCron/ComboCronPushController.py

@@ -226,7 +226,7 @@ class ComboCronPushView(View):
                 PushObject.ios_apns_push(**kwargs)
             # android gcm
             elif push_type == 1:
-                PushObject.android_fcm_push(**kwargs)
+                PushObject.android_fcm_push_v1(**kwargs)
             # android 极光推送
             elif push_type == 2:
                 PushObject.android_jpush(**kwargs)

+ 1 - 1
Controller/PowerWarningController.py

@@ -89,7 +89,7 @@ class PowerWarningView(View):
                     if push_type == 0:  # ios apns
                         PushObject.ios_apns_push(**kwargs)
                     elif push_type == 1:  # android gcm
-                        PushObject.android_fcm_push(**kwargs)
+                        PushObject.android_fcm_push_v1(**kwargs)
                     elif push_type == 2:  # android jpush
                         kwargs.pop('uid')
                         PushObject.android_jpush(**kwargs)

+ 1 - 1
Controller/gatewayController.py

@@ -414,7 +414,7 @@ class GatewayView(View):
         if push_type == 0:  # ios apns
             PushObject.ios_apns_push(**kwargs)
         elif push_type == 1:  # android gcm
-            PushObject.android_fcm_push(**kwargs)
+            PushObject.android_fcm_push_v1(**kwargs)
         elif push_type == 2:  # android 极光推送
             PushObject.android_jpush(**kwargs)
         elif push_type == 3:

+ 1 - 1
Object/SageMakerAiObject.py

@@ -251,7 +251,7 @@ class SageMakerAiObject:
             if push_type == 0:  # ios apns
                 PushObject.ios_apns_push(**kwargs)
             elif push_type == 1:  # android gcm
-                PushObject.android_fcm_push(**kwargs)
+                PushObject.android_fcm_push_v1(**kwargs)
             elif push_type == 2:  # android jpush
                 kwargs.pop('uid')
                 PushObject.android_jpush(**kwargs)

+ 1 - 1
Service/CustomizedPushService.py

@@ -173,7 +173,7 @@ class CustomizedPushObject:
                 if icon_link is None:
                     icon_link = ''
                 push_kwargs['image'] = icon_link
-                return PushObject.android_fcm_push(**push_kwargs)
+                return PushObject.android_fcm_push_v1(**push_kwargs)
             # 极光
             elif push_type == 2:
                 return PushObject.android_jpush(**push_kwargs)

+ 2 - 2
Service/DevicePushService.py

@@ -397,7 +397,7 @@ class DevicePushService:
                     if push_type == 0:  # ios apns
                         push_result = PushObject.ios_apns_push(**kwargs)
                     elif push_type == 1:  # android gcm
-                        push_result = PushObject.android_fcm_push(**kwargs)
+                        push_result = PushObject.android_fcm_push_v1(**kwargs)
                     elif push_type == 2:  # android jpush
                         kwargs.pop('uid')
                         push_result = PushObject.android_jpush(**kwargs)
@@ -701,7 +701,7 @@ class DevicePushService:
                 push_result = PushObject.ios_apns_push(
                     uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, uid, channel, image_url)
             elif push_type == 1:
-                push_result = PushObject.android_fcm_push(
+                push_result = PushObject.android_fcm_push_v1(
                     uid, appBundleId, token_val, n_time, event_type, msg_title, msg_text, uid, channel, image_url)
             elif push_type == 3:
                 huawei_push_object = HuaweiPushObject()

+ 48 - 0
Service/PushService.py

@@ -12,8 +12,10 @@ import os
 import time
 
 import apns2
+import firebase_admin
 import jpush
 import requests
+from firebase_admin import messaging
 from pyfcm import FCMNotification
 
 from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG, XMPUSH_CONFIG \
@@ -193,6 +195,52 @@ class PushObject:
             LOGGER.info('uid:{},evnet_type:{},time:{},fcm推送异常:{}'.format(uid, event_type, n_time, repr(e)))
             return False
 
+    @staticmethod
+    def android_fcm_push_v1(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
+                            uid='', channel='1', image=''):
+        """
+        android fcm 推送
+        @param nickname: 设备昵称
+        @param app_bundle_id: app包id
+        @param token_val: 推送token
+        @param n_time: 当前时间
+        @param event_type: 事件类型
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @param uid: uid
+        @param channel: 通道
+        @param image: 推送图片链接
+        @return: bool
+        """
+        try:
+            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
+                         }
+            if event_type in [606, 607]:
+                push_data['priority'] = 'high'
+                push_data['content_available'] = True
+                push_data['direct_boot_ok'] = True
+            message = messaging.Message(
+                notification=messaging.Notification(
+                    title=msg_title,
+                    body=msg_text,
+                    image=image
+                ),
+                data=push_data,
+                token=token_val,
+            )
+            # Send a message to the device corresponding to the provided
+            # registration token.
+            result = messaging.send(message)
+            LOGGER.info('fcm推送结果:{}'.format(result))
+            return True
+        except Exception as e:
+            LOGGER.info('fcm推送异常:{}'.format(repr(e)))
+            return False
+
     @staticmethod
     def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, channel=1):
         """

+ 91 - 69
requirements.txt

@@ -1,76 +1,98 @@
-aliyun-python-sdk-core==2.13.36
-aliyun-python-sdk-core-v3==2.13.33
-aliyun-python-sdk-kms==2.16.0
+aliyun-python-sdk-core==2.15.1
+aliyun-python-sdk-kms==2.16.3
 apns2-client==0.5.4
-asgiref==3.2.3
-asn1crypto==0.24.0
-blinker==1.4
-boto3==1.11.16
-botocore==1.14.17
-certifi==2022.12.7
-cffi==1.15.1
-chardet==5.1.0
-charset-normalizer==2.0.12
-configobj==5.0.6
+asgiref==3.8.1
+asttokens==2.4.1
+async-timeout==4.0.3
+backcall==0.2.0
+backports.zoneinfo==0.2.1
+boto3==1.34.103
+botocore==1.34.103
+CacheControl==0.14.0
+cachetools==5.3.3
+certifi==2024.2.2
+cffi==1.16.0
+charset-normalizer==3.3.2
+colorama==0.4.6
 crcmod==1.7
-cryptography==39.0.2
-Django==3.0.3
-django-appconf==1.0.5
-django-cors-headers==3.2.1
-django-imagekit==4.1.0
-docutils==0.15.2
-grpcio==1.52.0
+cryptography==42.0.7
+decorator==5.1.1
+Django==3.2.16
+django-appconf==1.0.6
+django-cors-headers==4.3.1
+django-imagekit==5.0.0
+executing==2.0.1
+firebase-admin==6.5.0
+google-api-core==2.19.0
+google-api-python-client==2.129.0
+google-auth==2.29.0
+google-auth-httplib2==0.2.0
+google-cloud-core==2.4.1
+google-cloud-firestore==2.16.0
+google-cloud-storage==2.16.0
+google-crc32c==1.5.0
+google-resumable-media==2.7.0
+googleapis-common-protos==1.63.0
+grpcio==1.63.0
+grpcio-status==1.62.2
 h2==2.6.2
 hpack==3.0.0
-httplib2==0.9.2
+httplib2==0.22.0
 hyper==0.7.0
 hyperframe==3.2.0
-idna==3.4
-ipip-ipdb==1.3.2
-jmespath==0.9.4
-jpush==3.3.8
-jsonpatch==1.16
-jsonpointer==1.10
-keyrings.alt==3.0
-mysqlclient==1.4.6
-numpy==1.21.6
-oauthlib==2.0.6
-oss2==2.9.1
-packaging==21.3
-paypalrestsdk==1.13.1
-pendulum==2.1.2
-pexpect==4.2.1
-pilkit==2.0
-Pillow==9.4.0
-protobuf==3.19.6
-ptyprocess==0.7.0
-pyasn1==0.4.8
-pycparser==2.21
-pycryptodome==3.17
-pyfcm==1.4.7
+idna==3.7
+image==1.5.33
+importlib_resources==6.4.0
+ipdb==0.13.13
+ipython==8.12.3
+jedi==0.19.1
+jmespath==0.10.0
+jpush==3.3.9
+matplotlib-inline==0.1.7
+msgpack==1.0.8
+mysqlclient==2.2.4
+numpy==1.24.4
+opencv-python==4.9.0.80
+oss2==2.18.5
+packaging==24.0
+parso==0.8.4
+pendulum==3.0.0
+pickleshare==0.7.5
+pilkit==3.0
+pillow==10.3.0
+prompt-toolkit==3.0.43
+proto-plus==1.23.0
+protobuf==4.25.3
+pure-eval==0.2.2
+pyasn1==0.6.0
+pyasn1_modules==0.4.0
+pycparser==2.22
+pycryptodome==3.20.0
+pyfcm==1.5.4
+Pygments==2.18.0
 pyipip==0.1.1
-PyJWT==1.5.3
-pyOpenSSL==23.0.0
-pyparsing==3.0.9
-pyserial==3.4
-python-dateutil==2.8.1
-python-debian==0.1.32
-python-rapidjson==1.9
-pytz==2019.3
-pytzdata==2020.1
-pyxdg==0.25
-redis==3.4.1
-requests==2.28.2
-requests-unixsocket==0.1.5
-s3transfer==0.3.7
-SecretStorage==2.3.1
-simplejson==3.17.0
-six==1.11.0
-sqlparse==0.3.0
-ssh-import-id==5.7
-supervisor==4.1.0
-tritonclient==2.30.0
-typing_extensions==4.5.0
-ujson==5.7.0
-urllib3==1.25.11
-var-dump==1.2
+PyJWT==2.5.0
+pyOpenSSL==24.1.0
+pyparsing==3.1.2
+python-dateutil==2.9.0.post0
+python-rapidjson==1.16
+pytz==2024.1
+redis==5.0.4
+requests==2.31.0
+rsa==4.9
+s3transfer==0.10.1
+simplejson==3.19.2
+six==1.16.0
+sqlparse==0.5.0
+stack-data==0.6.3
+time-machine==2.14.1
+tomli==2.0.1
+traitlets==5.14.3
+tritonclient==2.41.1
+typing_extensions==4.11.0
+tzdata==2024.1
+ujson==5.9.0
+uritemplate==4.1.1
+urllib3==1.26.0
+wcwidth==0.2.13
+zipp==3.18.1