PushService.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time : 2022/5/19 11:43
  4. @Auth : Locky
  5. @File :PushService.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. from Service.CommonService import CommonService
  15. # 推送对象
  16. class PushObject:
  17. @staticmethod
  18. def get_msg_title(app_bundle_id, nickname):
  19. """
  20. 获取推送消息标题
  21. @param app_bundle_id: app包id
  22. @param nickname: 设备名
  23. @return: msg_title
  24. """
  25. if app_bundle_id in APP_BUNDLE_DICT.keys():
  26. msg_title = APP_BUNDLE_DICT[app_bundle_id] + '(' + nickname + ')'
  27. else:
  28. msg_title = nickname
  29. return msg_title
  30. @staticmethod
  31. def get_gateway_msg_text(n_time, tz, lang, alarm):
  32. """
  33. 获取网关推送消息内容
  34. @param n_time: 当前时间
  35. @param tz: 时区
  36. @param lang: 语言
  37. @param alarm: 警报
  38. @return: msg_text
  39. """
  40. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  41. if lang == 'cn':
  42. msg_text = '{} 日期:{}'.format(alarm, n_date)
  43. else:
  44. msg_text = '{} date:{}'.format(alarm, n_date)
  45. return msg_text
  46. @staticmethod
  47. def get_ai_msg_text(channel, n_time, lang, tz, label):
  48. """
  49. 获取AI推送内容
  50. @param channel: 通道
  51. @param n_time: 当前时间
  52. @param lang: 语言
  53. @param tz: 时区
  54. @param label: 识别到的标签
  55. @return: ai_msg_text
  56. """
  57. n_date = CommonService.get_now_time_str(n_time=n_time, tz=tz, lang=lang)
  58. if lang == 'cn':
  59. msg = '摄像头AI识别到了{}'.format(label)
  60. ai_msg_text = '{msg} 通道:{channel} 日期:{date}'.format(msg=msg, channel=channel, date=n_date)
  61. else:
  62. msg = 'Camera AI recognizes {}'.format(label)
  63. ai_msg_text = '{msg} channel:{channel} date:{date}'.format(msg=msg, channel=channel, date=n_date)
  64. return ai_msg_text
  65. # ios apns 推送
  66. @staticmethod
  67. def ios_apns_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  68. uid='', channel='1', launch_image=None):
  69. logger = logging.getLogger('info')
  70. try:
  71. pem_path = os.path.join(BASE_DIR, APNS_CONFIG[app_bundle_id]['pem_path'])
  72. logger.info('apns推送app_bundle_id:{}, pem_path:{}'.format(app_bundle_id, pem_path))
  73. cli = apns2.APNSClient(mode=APNS_MODE, client_cert=pem_path)
  74. alert = apns2.PayloadAlert(title=msg_title, body=msg_text, launch_image=launch_image)
  75. push_data = {'alert': 'Motion', 'msg': '', 'sound': '', 'zpush': '1', 'uid': uid, 'channel': channel,
  76. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  77. 'image_url': launch_image
  78. }
  79. payload = apns2.Payload(alert=alert, custom=push_data, sound='default', category='myCategory',
  80. mutable_content=True)
  81. n = apns2.Notification(payload=payload, priority=apns2.PRIORITY_LOW)
  82. res = cli.push(n=n, device_token=token_val, topic=app_bundle_id)
  83. assert res.status_code == 200
  84. except Exception as e:
  85. logger.info('--->IOS推送异常{}'.format(repr(e)))
  86. return repr(e)
  87. # android fcm 推送
  88. @staticmethod
  89. def android_fcm_push(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text,
  90. uid='', channel='1', image=''):
  91. logger = logging.getLogger('info')
  92. try:
  93. serverKey = FCM_CONFIG[app_bundle_id]
  94. push_service = FCMNotification(api_key=serverKey)
  95. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1', 'image': image,
  96. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname,
  97. 'uid': uid, 'channel': channel
  98. }
  99. result = push_service.notify_single_device(registration_id=token_val, message_title=msg_title,
  100. message_body=msg_text, data_message=push_data,
  101. extra_kwargs={'default_sound': True,
  102. 'default_vibrate_timings': True,
  103. 'default_light_settings': True,
  104. }
  105. )
  106. logger.info('fcm推送结果:{}'.format(result))
  107. return result
  108. except Exception as e:
  109. return repr(e)
  110. # android 极光 推送
  111. @staticmethod
  112. def android_jpush(nickname, app_bundle_id, token_val, n_time, event_type, msg_title, msg_text):
  113. try:
  114. app_key = JPUSH_CONFIG[app_bundle_id]['Key']
  115. master_secret = JPUSH_CONFIG[app_bundle_id]['Secret']
  116. # 换成各自的app_key和master_secret
  117. _jpush = jpush.JPush(app_key, master_secret)
  118. push = _jpush.create_push()
  119. push.audience = jpush.registration_id(token_val)
  120. push_data = {'alert': 'Motion', 'msg': '', 'sound': 'sound.aif', 'zpush': '1',
  121. 'received_at': n_time, 'event_time': n_time, 'event_type': event_type, 'nickname': nickname
  122. }
  123. android = jpush.android(title=msg_title, big_text=msg_text, alert=msg_text, extras=push_data,
  124. priority=1, style=1, alert_type=7
  125. )
  126. push.notification = jpush.notification(android=android)
  127. push.platform = jpush.all_
  128. res = push.send()
  129. assert res.status_code == 200
  130. except Exception as e:
  131. return repr(e)