Kaynağa Gözat

新增魅族推送

guanhailong 2 yıl önce
ebeveyn
işleme
f0ff794872

+ 7 - 0
AnsjerPush/cn_config/cn_formal_config.py

@@ -187,4 +187,11 @@ OPPOPUSH_CONFIG = {
         'Key': '882266a2000e4407990932be0b7043f5',
         'Secret': '1037f29469c8416e87ab9cb8537c68ce'
     }
+}
+
+MEIZUPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'ID': 151022,
+        'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
+    }
 }

+ 6 - 0
AnsjerPush/dev_config/local_config.py

@@ -170,5 +170,11 @@ OPPOPUSH_CONFIG = {
     }
 }
 
+MEIZUPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'ID': 151022,
+        'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
+    }
+}
 APNS_MODE = 'dev'
 REDIS_ADDRESS = '127.0.0.1'

+ 7 - 0
AnsjerPush/eur_config/eur_formal_config.py

@@ -182,6 +182,13 @@ VIVOPUSH_CONFIG = {
     }
 }
 
+MEIZUPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'ID': 151022,
+        'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
+    }
+}
+
 OPPOPUSH_CONFIG = {
     'com.ansjer.zccloud_ab': {
         'Key': '882266a2000e4407990932be0b7043f5',

+ 7 - 0
AnsjerPush/test_config/test_config.py

@@ -164,4 +164,11 @@ OPPOPUSH_CONFIG = {
         'Key': '882266a2000e4407990932be0b7043f5',
         'Secret': '1037f29469c8416e87ab9cb8537c68ce'
     }
+}
+
+MEIZUPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'ID': 151022,
+        'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
+    }
 }

+ 7 - 0
AnsjerPush/us_config/formal_config.py

@@ -187,4 +187,11 @@ OPPOPUSH_CONFIG = {
         'Key': '882266a2000e4407990932be0b7043f5',
         'Secret': '1037f29469c8416e87ab9cb8537c68ce'
     }
+}
+
+MEIZUPUSH_CONFIG = {
+    'com.ansjer.zccloud_ab':{
+        'ID': 151022,
+        'AppSecret': '890e94e09b7b4aa18278acce82e35c46',
+    }
 }

+ 2 - 0
Controller/AiController.py

@@ -187,6 +187,8 @@ class AiView(View):
                         PushObject.android_xmpush(**kwargs)
                     elif push_type == 5:  # android vivo推送
                         PushObject.android_vivopush(**kwargs)
+                    elif push_type == 7:  # android 魅族推送
+                        PushObject.android_meizupush(**kwargs)
                 except Exception as e:
                     LOGGING.info('ai推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
                     continue

+ 3 - 0
Controller/ComboCron/ComboCronPushController.py

@@ -235,6 +235,9 @@ class ComboCronPushView(View):
             # android vivo推送
             elif push_type == 5:
                 PushObject.android_vivopush(**kwargs)
+            # android 魅族推送
+            elif push_type == 7:
+                PushObject.android_meizupush(**kwargs)
             return True
         except Exception as e:
             logger.info('流量预警推送异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

+ 2 - 0
Controller/PowerWarningController.py

@@ -95,6 +95,8 @@ class PowerWarningView(View):
                         PushObject.android_xmpush(**kwargs)
                     elif push_type == 5:  # android vivo推送
                         PushObject.android_vivopush(**kwargs)
+                    elif push_type == 7:  # android 魅族推送
+                        PushObject.android_meizupush(**kwargs)
                 except Exception as e:
                     logger.info('低电量推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
                     continue

+ 4 - 0
Controller/gatewayController.py

@@ -177,6 +177,8 @@ class GatewayView(View):
                             PushObject.android_xmpush(**kwargs)
                         elif push_type == 5:  # android vivo推送
                             PushObject.android_vivopush(**kwargs)
+                        elif push_type == 7:  # android 魅族推送
+                            PushObject.android_meizupush(**kwargs)
                     except Exception as e:
                         logger.info('网关推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
                         continue
@@ -266,6 +268,8 @@ class GatewayView(View):
                                 PushObject.android_xmpush(**kwargs)
                             elif push_type == 5:  # android vivo推送
                                 PushObject.android_vivopush(**kwargs)
+                            elif push_type == 7:  # android 魅族推送
+                                PushObject.android_meizupush(**kwargs)
                         except Exception as e:
                             logger.info('场景日志推送消息异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
                             continue

+ 14 - 0
Service/CommonService.py

@@ -2,6 +2,7 @@
 import datetime
 import os
 import time
+import hashlib
 from pathlib import Path
 from random import Random
 import ipdb
@@ -268,3 +269,16 @@ class CommonService:
                 else:
                     os.remove(c_path)
             os.rmdir(path)
+
+    @staticmethod
+    def getMD5Sign(data, key):
+        '''
+        魅族MD5签名
+        '''
+        dataList = []
+        for k in sorted(data):
+            dataList.append("%s=%s" % (k, data[k]))
+        data = (''.join(dataList))
+        data = data + key
+        sign = hashlib.md5(data.encode(encoding="utf-8")).hexdigest()
+        return sign

+ 14 - 51
Service/DevicePushService.py

@@ -19,7 +19,7 @@ import requests
 from pyfcm import FCMNotification
 
 from AnsjerPush.config import JPUSH_CONFIG, FCM_CONFIG, APNS_CONFIG, BASE_DIR, APNS_MODE, APP_BUNDLE_DICT, \
-    XMPUSH_CONFIG, VIVOPUSH_CONFIG, OPPOPUSH_CONFIG
+    XMPUSH_CONFIG, VIVOPUSH_CONFIG, OPPOPUSH_CONFIG, MEIZUPUSH_CONFIG
 from AnsjerPush.config import SERVER_TYPE
 from Model.models import UidPushModel, SysMsgModel, DeviceSharePermission, DeviceChannelUserSet, \
     DeviceChannelUserPermission
@@ -167,7 +167,7 @@ class DevicePushService:
         try:
             kwag_args = param['kwag_args']
             result = {'do_apns_code': '', 'do_fcm_code': '', 'do_jpush_code': '', 'do_xmpush_code': '',
-                      'do_vivopush_code': ''}
+                      'do_vivopush_code': '', 'do_meizupush_code': ''}
             # 判断是否进行APP消息推送,如app_push不为空,则不进行推送
             if not param['app_push']:
                 LOGGING.info('APP准备推送:{}, {}'.format(param['uid'], param))
@@ -193,9 +193,11 @@ class DevicePushService:
                     elif push_type == 4:  # android xmpush
                         result['do_xmpush_code'] = cls.do_xmpush(**kwag_args)
                     elif push_type == 5:  # android vivopush
-                        result['do_vivopush_code'] = cls.do_vivopush(**kwag_args)
+                        result['do_vivopush_code'] = PushObject.android_meizupush(**kwag_args)
                     elif push_type == 6:  # android oppopush
                         result['do_oppopush_code'] = cls.do_oppopush(**kwag_args)
+                    elif push_type == 7:  # android meizupush
+                        result['do_meizupush_code'] = PushObject.android_meizupush(**kwag_args)
             return result
         except Exception as e:
             LOGGING.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
@@ -470,9 +472,15 @@ class DevicePushService:
             elif push_type == 4:
                 PushObject.android_xmpush(uid, appBundleId, token_val, n_time, event_type, msg_title,
                                           msg_text, uid, channel, image_url)
+            elif push_type == 5:
+                PushObject.android_meizupush(uid, appBundleId, token_val, n_time, event_type, msg_title,
+                                            msg_text, uid, channel, image_url)
             elif push_type == 6:
                 PushObject.android_oppopush(uid, appBundleId, token_val, n_time, event_type, msg_title,
                                             msg_text, uid, channel, image_url)
+            elif push_type == 7:
+                PushObject.android_meizupush(uid, appBundleId, token_val, n_time, event_type, msg_title,
+                                            msg_text, uid, channel, image_url)
         except Exception as e:
             LOGGING.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
@@ -495,7 +503,9 @@ class DevicePushService:
                 elif up['push_type'] == 4:  # android jpush
                     up['do_xmpush_code'] = params['code_dict']['code_date']['do_xmpush_code']
                 elif up['push_type'] == 5:  # android jpush
-                    up['do_vivopush_code'] = params['code_dict']['code_date']['do_xmpush_code']
+                    up['do_vivopush_code'] = params['code_dict']['code_date']['do_vivopush_code']
+                elif up['push_type'] == 7:  # android jpush
+                    up['do_meizupush_code'] = params['code_dict']['code_date']['do_meizupush_code']
                 del up['push_type']
                 del up['userID_id']
                 del up['userID__NickName']
@@ -570,50 +580,3 @@ class DevicePushService:
         if not channel_permission_qs.exists():
             return False
         return True
-
-    @classmethod
-    def do_vivopush(cls, uid, channel, appBundleId, token_val, event_type, n_time,
-                    msg_title, msg_text):
-        logger = logging.getLogger('info')
-        try:
-            authToken = 'authToken_' + appBundleId
-            redisObj = RedisObject()
-            # 获取redis里面的authToken
-            redis_authToken = redisObj.get_data(key=authToken)
-            if msg_title == '':
-                msg_title = '周视'
-            if redis_authToken is not False:
-                app_secret = VIVOPUSH_CONFIG[appBundleId]['Secret']
-                sender_send = APISender(app_secret)
-                sender_send.set_token(redis_authToken)
-            else:
-                app_id = VIVOPUSH_CONFIG[appBundleId]['ID']
-                app_key = VIVOPUSH_CONFIG[appBundleId]['Key']
-                app_secret = VIVOPUSH_CONFIG[appBundleId]['Secret']
-                sender = APISender(app_secret)
-                rec = sender.get_token(app_id, app_key)
-                # 存放authToken,有效期3个小时
-                redisObj = RedisObject()
-                redisObj.set_data(key=authToken, val=rec['authToken'], expire=10800)
-                sender_send = APISender(app_secret)
-                sender_send.set_token(rec['authToken'])
-            push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
-                         'received_at': n_time, 'event_time': n_time, 'event_type': event_type,
-                         'uid': uid, 'channel': channel
-                         }
-            # 推送
-            message = PushMessage() \
-                .reg_id(token_val) \
-                .title(msg_title) \
-                .content(msg_text) \
-                .push_mode(1) \
-                .notify_type(1) \
-                .skip_type('1') \
-                .request_id('123456') \
-                .classification(1) \
-                .client_custom_map(**push_data) \
-                .message_dict()
-            rec2 = sender_send.send(message)
-            logger.info('vivo推送结果:{}'.format(rec2))
-        except Exception as e:
-            logger.info('vivo推送异常:{}'.format(e))

+ 64 - 5
Service/PushService.py

@@ -17,7 +17,7 @@ import requests
 from pyfcm import FCMNotification
 
 from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG, XMPUSH_CONFIG \
-    , VIVOPUSH_CONFIG, OPPOPUSH_CONFIG
+    , VIVOPUSH_CONFIG, OPPOPUSH_CONFIG, MEIZUPUSH_CONFIG
 from Object.RedisObject import RedisObject
 from Service.CommonService import CommonService
 from Service.VivoPushService.push_admin.APIMessage import PushMessage
@@ -255,9 +255,9 @@ class PushObject:
 
     @staticmethod
     def android_vivopush(app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
-                         uid='', channel='1', image='', skip_type='1', nickname=''):
+                         uid='', channel='1', image='', skip_type='1', nickname='', appBundleId=''):
         """
-        vivo 推送
+        vivo 推送(不支持图片)
         @param app_bundle_id: app包id
         @param token_val: 推送token
         @param event_type: 消息类型 (0:运营类消息,1:系统类消息。默认为 0)
@@ -279,6 +279,7 @@ class PushObject:
             redisObj = RedisObject()
             # 获取redis里面的authToken
             redis_authToken = redisObj.get_data(key=authToken)
+            app_bundle_id = app_bundle_id if app_bundle_id == '' else appBundleId
             if msg_title == '':
                 msg_title = '周视'
             if redis_authToken is not False:
@@ -313,8 +314,8 @@ class PushObject:
                 .classification(1) \
                 .client_custom_map(**push_data) \
                 .message_dict()
-            rec2 = sender_send.send(message)
-            logger.info('vivo推送结果:{}'.format(rec2))
+            rec = sender_send.send(message)
+            logger.info('vivo推送结果:{}'.format(rec.json()))
         except Exception as e:
             logger.info('vivo推送异常:{}'.format(e))
 
@@ -384,3 +385,61 @@ class PushObject:
             assert response.status_code == 200
         except Exception as e:
             return repr(e)
+
+    @staticmethod
+    def android_meizupush(app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, nickname,
+                          uid='', channel='1', appBundleId='', image=''):
+        """
+        android 魅族推送(不支持图片)
+        @param app_bundle_id: app包id
+        @param token_val: 推送token
+        @param event_type: 消息类型 (0:运营类消息,1:系统类消息。默认为 0)
+        @param msg_title: 推送标题
+        @param msg_text: 推送内容
+        @param push_mode: 推送模式 (0:正式推送;1:测试推送,默认为0)
+        @param m_code: 用户请求唯一标识 最大 64 字符
+        @param skip_type: 跳转类型(1:打开 APP 首页 2:打开链接 3:自定义 4:打开 app 内指定页面)
+        @param n_time: 当前时间
+        @param nickname: 设备昵称
+        @param uid: uid
+        @param image: 推送图片链接
+        @param channel: 通道
+        @return: None
+        """
+        logger = logging.getLogger('info')
+        try:
+            #  获取包和AppSecret
+            app_bundle_id = app_bundle_id if app_bundle_id == '' else appBundleId
+            appId = MEIZUPUSH_CONFIG[app_bundle_id]['ID']
+            appSecret = MEIZUPUSH_CONFIG[app_bundle_id]['AppSecret']
+            url = 'https://server-api-push.meizu.com/garcia/api/server/push/varnished/pushByPushId'
+            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 msg_title == '':
+                msg_title = '周视'
+            # 拼接发送内容
+            messageJson = '{"clickTypeInfo":{"activity":"","clickType":0,"customAttribute":""},"extra":{},'
+            noticeBarInfo = ('"noticeBarInfo": {"title": "%s", "content":  "%s"},' % (msg_title, msg_text))
+            noticeExpandInfo = '"noticeExpandInfo":{"noticeExpandType":0},"pushTimeInfo":{"validTime":24}}'
+            messageJson += noticeBarInfo
+            messageJson += noticeExpandInfo
+            data_meizu = {
+                'appId': appId,
+                'pushIds': token_val,
+                'messageJson': messageJson
+            }
+            # 魅族MD5加密
+            sign = CommonService.getMD5Sign(data=data_meizu, key=appSecret)
+            data = {
+                'appId': appId,
+                'messageJson': messageJson,
+                'sign': sign,
+                'pushIds': token_val,
+            }
+            # 进行推送
+            response = requests.post(url, data=data)
+            logger.info("魅族推送结果:{}".format(response.json()))
+        except Exception as e:
+            return repr(e)