class FCMPushSender(Sender): """ FCM Push Sender uses python-FCM module: https://github.com/geeknam/python-FCM FCM documentation: https://developer.android.com/google/FCM/FCM.html """ def __init__(self, config, log): Sender.__init__(self, config, log) self.base_deeplink_url = config.get('Messenger', 'base_deeplink_url') app = generate_fcm_app( config.get('Messenger', 'google_application_credentials')) self.FCM = FCM(app) self.canonical_ids = [] self.unregistered_devices = [] def pop_canonical_ids(self): items = self.canonical_ids self.canonical_ids = [] return items def pop_unregistered_devices(self): items = self.unregistered_devices self.unregistered_devices = [] return items def create_message(self, notification): expiry_seconds = (notification['time_to_live_ts_bigint'] - int(round(time.time() * 1000))) / 1000 if expiry_seconds < 0: self.log.warning( 'FCM: expired notification with sending_id: {0}; expiry_seconds: {1}' .format(notification['sending_id'], expiry_seconds)) notification['status'] = const.NOTIFICATION_EXPIRED return utm_source = 'pushnotification' utm_campaign = str(notification['campaign_id']) utm_medium = str(notification['message_id']) data = { 'title': notification['title'], 'message': notification['content'], 'url': self.base_deeplink_url + '://' + notification['screen'] + '?utm_source=' + utm_source + '&utm_campaign=' + utm_campaign + '&utm_medium=' + utm_medium, 'notifid': notification['campaign_id'], } msg = messaging.Message( data=data, token=notification['receiver_id'], ) return msg def send(self, notification): dry_run = 'dry_run' in notification and notification['dry_run'] == True message = self.create_message(notification) response = self.FCM.send(message, dry_run) return response def send_batch(self): messages = [] while len(self.queue): messages.append(self.create_message(self.queue.pop())) return self.FCM.send_batch(messages)