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 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)
Esempio n. 3
0
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 ios_push_notification(data, device_token):
    if not firebase_app:
        return

    # TODO: Fixed notification settings that only sends events turned on by default, until we find a better solution for ios
    if data['type'] in [
            'heaterEvent',
    ]:
        return
    if data['type'] == 'printEvent' and data['eventType'] not in [
            'PrintDone', 'PrintCancelled'
    ]:
        return

    notification = Notification(title=data['title'], body=data['body'])

    if data.get('picUrl'):
        notification.image = data.get('picUrl')

    try:
        message = Message(notification=notification,
                          apns=APNSConfig(headers={
                              'apns-push-type':
                              'alert',
                              'apns-priority':
                              '5',
                              'apns-topic':
                              'com.thespaghettidetective.ios',
                              'apns-collapse-id':
                              f'collapse-{data["printerId"]}',
                          }, ),
                          token=device_token)

        if data['type'] != 'printProgress':
            message.apns.payload = APNSPayload(aps=Aps(sound="default"))

        return send(message, app=firebase_app)
    except (UnregisteredError, SenderIdMismatchError,
            firebase_admin.exceptions.InternalError):
        MobileDevice.objects.filter(device_token=device_token).update(
            deactivated_at=now())
    except:
        import traceback
        traceback.print_exc()
        sentryClient.captureException()
Esempio n. 5
0
    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()