def send_to_device(msg, mobile_device): if not firebase_app: return if mobile_device.platform == 'ios': ios_push_notification(msg, mobile_device.device_token) return try: message = Message(data=msg, android=AndroidConfig(priority='high'), apns=APNSConfig(headers={ 'apns-push-type': 'background', 'apns-priority': '5' }, payload=APNSPayload(aps=Aps( content_available=True))), token=mobile_device.device_token) return send(message, app=firebase_app) except (UnregisteredError, SenderIdMismatchError, firebase_admin.exceptions.InternalError): MobileDevice.objects.filter( device_token=mobile_device.device_token).update( deactivated_at=now()) except: import traceback traceback.print_exc() sentryClient.captureException()
def call(self, title: str = '', body: str = ''): token = "asfdjkladjfkaldjfklajdfkl2j13klj132kljkl" messages = [] message = messaging.Message(notification=messaging.Notification( title=title, body=body), token=token, data=None, android=AndroidConfig(priority=10, ttl=45)) messages.append(message) if len(messages) == 0: batch_rsp = messaging.send_all(messages) i = 0 failed_ids = [] for rsp in batch_rsp.responses: if rsp.exception: if rsp.exception.http_response.status_code in [404, 400]: failed_ids.append(token) else: logger.warning(f"failed fcm batch send: {token}") i += 1 return failed_ids
def send_push_notification( participant: Participant, reference_schedule: ScheduledEvent, survey_obj_ids: List[str], fcm_token: str ): """ Contains the body of the code to send a notification """ # we include a nonce in case of notification deduplication. data_kwargs = { 'nonce': ''.join(random.choice(OBJECT_ID_ALLOWED_CHARS) for _ in range(32)), 'sent_time': reference_schedule.scheduled_time.strftime(API_TIME_FORMAT), 'type': 'survey', 'survey_ids': json.dumps(list(set(survey_obj_ids))), # Dedupe. } if participant.os_type == Participant.ANDROID_API: message = Message( android=AndroidConfig(data=data_kwargs, priority='high'), token=fcm_token, ) else: display_message = \ "You have a survey to take." if len(survey_obj_ids) == 1 else "You have surveys to take." message = Message( data=data_kwargs, token=fcm_token, notification=Notification(title="Beiwe", body=display_message), ) send_notification(message)
def send_to_device(registration_token, msg): message = Message( data=msg, android=AndroidConfig(priority="high"), apns=APNSConfig(payload=APNSPayload(aps=Aps(content_available=True))), token=registration_token) return send(message)
def broadcast(self, code: str, content: str) -> str: notification = messaging.Notification(body=content) android_notification = AndroidNotification(sound='default') android_config = AndroidConfig(notification=android_notification) # web_notification = WebpushNotification() # web_configuration = WebpushConfig(notification=web_notification) message = messaging.Message( notification=notification, android=android_config, # webpush=web_configuration, topic=code) return messaging.send(message)
def send_to_device(msg, device_token): try: message = Message(data=msg, android=AndroidConfig(priority='high'), apns=APNSConfig(headers={ 'apns-push-type': 'background', 'apns-priority': '5' }, payload=APNSPayload(aps=Aps( content_available=True))), token=device_token) return send(message) except UnregisteredError: MobileDevice.objects.filter(device_token=device_token).update( deactivated_at=now())
def push(self, request): from fcm_django.models import FCMDevice from firebase_admin.messaging import Notification, Message, \ AndroidConfig, AndroidNotification, APNSPayload, Aps, APNSConfig for device in self.recipient.fcmdevice_set.all(): data = {} for data_attr in [ 'id', 'object_id', 'target_id', 'object_content_type', 'target_content_type' ]: value = getattr(self, data_attr) if value: data[data_attr] = '.'.join( value.natural_key()) if isinstance( value, ContentType) else str(value) # from objprint import op # op(data) result = device.send_message( Message( notification=Notification( title=self.push_config['title'], body=self.push_config['body'], # image=self.push_data['image_url']" ), data=data, android=AndroidConfig( collapse_key=self.push_config['android'] ['collapse_key'], priority=self.push_config['android']['priority'], notification=AndroidNotification( click_action=self.push_config['android'] ['click_action'], sound=self.push_config['android']['sound'])), apns=APNSConfig(payload=APNSPayload(aps=Aps( badge=self.recipient.unread_notifications.count(), category=self.push_config['apns']['category'], sound=self.push_config['apns']['sound'])))))
def send_to_device(msg, mobile_device): if not firebase_app: return kwargs = dict( data=msg, android=AndroidConfig(priority='high'), apns=APNSConfig( headers={'apns-push-type': 'background', 'apns-priority': '5'}, payload=APNSPayload( aps=Aps(content_available=True, category='post')) ), token=mobile_device.device_token ) try: message = Message(**kwargs) return send(message, app=firebase_app) except (UnregisteredError, SenderIdMismatchError, firebase_admin.exceptions.InternalError): MobileDevice.objects.filter(device_token=mobile_device.device_token).update(deactivated_at=now()) except Exception: import traceback; traceback.print_exc() capture_exception()