GatewayService.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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, launch_image=None):
  36. logger = logging.getLogger('info')
  37. try:
  38. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  39. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  40. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  41. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1',
  42. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  43. }
  44. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', mutable_content=True)
  45. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  46. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  47. assert res.status_code == 200
  48. except Exception as e:
  49. logger.info('--->IOS推送异常{}'.format(repr(e)))
  50. return repr(e)
  51. # android fcm 推送
  52. @staticmethod
  53. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text, image=''):
  54. logger = logging.getLogger('info')
  55. try:
  56. serverKey = FCM_CONFIG[app_bundle_id]
  57. push_service = FCMNotification(api_key=serverKey)
  58. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  59. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  60. }
  61. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  62. message_body=msg_text, data_message=push_data,
  63. extra_notification_kwargs={
  64. 'image': image
  65. },
  66. extra_kwargs={'default_sound': True,
  67. 'default_vibrate_timings': True,
  68. 'default_light_settings': True,
  69. }
  70. )
  71. logger.info('fcm推送结果:{}'.format(result))
  72. return result
  73. except Exception as e:
  74. return repr(e)
  75. # android 极光 推送
  76. @staticmethod
  77. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
  78. try:
  79. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  80. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  81. # 换成各自的app_key和master_secret
  82. _jpush = jpush.JPush(app_key, master_secret)
  83. push = _jpush.create_push()
  84. push.audience = jpush.registration_id(token_val)
  85. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  86. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  87. }
  88. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  89. priority=1, style=1, alert_type=7
  90. )
  91. push.notification = jpush.notification(android=android)
  92. push.platform = jpush.all_
  93. res = push.send()
  94. assert res.status_code == 200
  95. except Exception as e:
  96. return repr(e)