コード例 #1
0
ファイル: admin.py プロジェクト: TralahM/fcm-django
    def send_messages(self, request, queryset, bulk=False):
        """
        Provides error handling for DeviceAdmin send_message and
        send_bulk_message methods.
        """
        total_failure = 0
        single_responses: List[Tuple[SendResponse, str]] = []

        for device in queryset:
            device: "FCMDevice"
            if bulk:
                response = queryset.send_message(
                    Message(notification=Notification(
                        title="Test notification",
                        body="Test bulk notification")))
                total_failure = len(response.deactivated_registration_ids)
                return self._send_deactivated_message(request, response,
                                                      total_failure, False)
            else:
                response = device.send_message(
                    Message(notification=Notification(
                        title="Test notification",
                        body="Test single notification")))
                single_responses.append((response, device.registration_id))
                if type(response) != SendResponse:
                    total_failure += 1

        self._send_deactivated_message(request, single_responses,
                                       total_failure, False)
コード例 #2
0
ファイル: api.py プロジェクト: jainhitesh9998/CrowdCount
def notify():
    payload = request.get_json()
    print(payload)
    if payload is None:
        return "error"
    notification = Notification()
    notification.title = "Order Status"
    print(type(payload))
    notification.body = json.dumps(payload)
    if payload["topic"] == "user":
        return send_to_token(tokens["users"], notification=notification)
    else:
        return send_to_token(tokens["vendors"][-1], notification=notification)
コード例 #3
0
def notify_getoff():
    fcm_messages = []
    standing_users = {}
    vacancies_created_car_ids = set()

    itinerary_keys = redis.keys("itinerary:*")
    for itinerary_key in itinerary_keys:
        user_id = itinerary_key.split(':')[-1]
        user = get_user(user_id)
        itinerary = get_itinerary(user_id)
        train = get_train(itinerary['subway_id'], itinerary['train_id'])
        car_id = f"{train.subway_id}-{train.number}-{itinerary['car_number']}"

        if train.station_id == itinerary['destination_id']:
            delete_itinerary(user_id)
            vacancies_created_car_ids.add(car_id)
            if user_id.startswith('dummy'):
                continue

            # send notification to users who need to get off the train
            if user.get('notification_itinerary_end', 'on') == 'on' and user['platform'] == 'fcm':
                message = Message(
                    notification=Notification('여정이 끝났습니다!', '하차하세요~'),
                    token=user['token'],
                )
                fcm_messages.append(message)
        else:
            # collect real users who are standing and whose itinerary is ongoing
            if user_id.startswith('dummy'):
                continue

            if itinerary['seated'] == "false":
                if car_id not in standing_users:
                    standing_users[car_id] = []
                standing_users[car_id].append(user)
    
    for car_id in vacancies_created_car_ids:
        if car_id in standing_users:
            for user in standing_users[car_id]:
                if user.get('notification_seat_vacancy', 'on') == 'on' and user['platform'] == 'fcm':
                    message = Message(
                        notification=Notification('자리가 생겼습니다!', '착석하세요~'),
                        token=user['token'],
                    )
                    fcm_messages.append(message)

    # bulk send FCM messages
    messaging.send_all(fcm_messages)
    for m in fcm_messages:
        click.echo(f"token: {m.token} title: {m.notification.title} body: {m.notification.body}")
    click.echo(f"[{datetime.now()}] {len(fcm_messages)} 개 FCM 알람 발송")
コード例 #4
0
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)
コード例 #5
0
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()
コード例 #6
0
 def make_message(self, body=None, notification=None):
     topic_or_token = self.topic if self.topic else self.token
     body = {} if not body else body
     notification = {} if not notification else notification
     return messaging.Message(
         data=body,
         notification=Notification(**notification),
         token=topic_or_token,
         apns=messaging.APNSConfig(
             headers={
                 'apns-push-type': 'background',
                 'apns-priority': '5'
             },
             payload=messaging.APNSPayload(
                 aps=messaging.Aps(content_available=True)
                 # badge=Session().query(Notification).filter((Notification.to_user_id == self.target_user_id)
                 #                                            & (Notification.read == False)).count() + 1
             )))
コード例 #7
0
ファイル: models.py プロジェクト: matejhazala/django-whistle
    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'])))))
コード例 #8
0
ファイル: admin.py プロジェクト: xtrinch/fcm-django
 def handle_send_topic_message(self, request, queryset):
     FCMDevice.send_topic_message(
         Message(notification=Notification(
             title="Test notification", body="Test single notification")),
         "test-topic",
     )