GatewayService.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2022/5/19 11:43
  4. @Auth : Locky
  5. @File :GatewayService.py
  6. @IDE :PyCharm
  7. """
  8. import logging
  9. import os
  10. import apns2
  11. import jpush
  12. from pyfcm import FCMNotification
  13. from AnsjerPush.config import APP_BUNDLE_DICT, APNS_MODE, BASE_DIR, APNS_CONFIG, FCM_CONFIG, JPUSH_CONFIG
  14. # 网关推送类
  15. from Service.CommonService import CommonService
  16. class GatewayPushService:
  17. # 获取推送消息标题
  18. @staticmethod
  19. def get_msg_title(app_bundle_id, nickname):
  20. if app_bundle_id in APP_BUNDLE_DICT.keys():
  21. return APP_BUNDLE_DICT[app_bundle_id] + '(' + nickname + ')'
  22. else:
  23. return nickname
  24. # 获取推送消息内容
  25. @staticmethod
  26. def get_msg_text(n_time, tz, lang, alarm):
  27. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  28. if lang == 'cn':
  29. msg_text = '{} 日期:{}'.format(alarm, n_date)
  30. else:
  31. msg_text = '{} date:{}'.format(alarm, n_date)
  32. return msg_text
  33. # ios apns 推送
  34. @staticmethod
  35. def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  36. uid='', channel='1', launch_image=None):
  37. logger = logging.getLogger('info')
  38. try:
  39. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  40. logger.info('apns推送app_bundle_id:{}, pem_path:{}'.format(app_bundle_id, pem_path))
  41. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  42. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  43. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
  44. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  45. 'image_url': launch_image
  46. }
  47. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', category='myCategory',
  48. mutable_content=True)
  49. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  50. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  51. assert res.status_code == 200
  52. except Exception as e:
  53. logger.info('--->IOS推送异常{}'.format(repr(e)))
  54. return repr(e)
  55. # android fcm 推送
  56. @staticmethod
  57. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  58. uid='', channel='1', image=''):
  59. logger = logging.getLogger('info')
  60. try:
  61. serverKey = FCM_CONFIG[app_bundle_id]
  62. push_service = FCMNotification(api_key=serverKey)
  63. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  64. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  65. 'uid': uid, 'channel': channel
  66. }
  67. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  68. message_body=msg_text, data_message=push_data,
  69. extra_kwargs={'default_sound': True,
  70. 'default_vibrate_timings': True,
  71. 'default_light_settings': True,
  72. }
  73. )
  74. logger.info('fcm推送结果:{}'.format(result))
  75. return result
  76. except Exception as e:
  77. return repr(e)
  78. # android 极光 推送
  79. @staticmethod
  80. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
  81. try:
  82. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  83. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  84. # 换成各自的app_key和master_secret
  85. _jpush = jpush.JPush(app_key, master_secret)
  86. push = _jpush.create_push()
  87. push.audience = jpush.registration_id(token_val)
  88. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  89. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  90. }
  91. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  92. priority=1, style=1, alert_type=7
  93. )
  94. push.notification = jpush.notification(android=android)
  95. push.platform = jpush.all_
  96. res = push.send()
  97. assert res.status_code == 200
  98. except Exception as e:
  99. return repr(e)