diff --git a/push_notifications/admin.py b/push_notifications/admin.py index 53a497c0..04579d5d 100644 --- a/push_notifications/admin.py +++ b/push_notifications/admin.py @@ -5,11 +5,10 @@ from .apns import APNSServerError from .gcm import GCMError -from .models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice +from .models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice, FCMDevice from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS from .webpush import WebPushError - User = apps.get_model(*SETTINGS["USER_MODEL"].split(".")) @@ -135,7 +134,13 @@ class GCMDeviceAdmin(DeviceAdmin): list_filter = ("active", "cloud_message_type") +class FCMDeviceAdmin(GCMDeviceAdmin): + list_display = GCMDeviceAdmin.list_display + ('platform',) + list_filter = GCMDeviceAdmin.list_filter + ('platform',) + + admin.site.register(APNSDevice, DeviceAdmin) admin.site.register(GCMDevice, GCMDeviceAdmin) +admin.site.register(FCMDevice, FCMDeviceAdmin) admin.site.register(WNSDevice, DeviceAdmin) admin.site.register(WebPushDevice, DeviceAdmin) diff --git a/push_notifications/api/rest_framework.py b/push_notifications/api/rest_framework.py index 6efff4ae..52d5c1c6 100644 --- a/push_notifications/api/rest_framework.py +++ b/push_notifications/api/rest_framework.py @@ -5,7 +5,7 @@ from rest_framework.viewsets import ModelViewSet from ..fields import UNSIGNED_64BIT_INT_MAX_VALUE, hex_re -from ..models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice +from ..models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice, FCMDevice from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS @@ -110,6 +110,12 @@ def validate_device_id(self, value): return value +class FCMDeviceSerializer(GCMDeviceSerializer, ModelSerializer): + class Meta(GCMDeviceSerializer.Meta): + model = FCMDevice + fields = GCMDeviceSerializer.Meta.fields + ('platform',) + + class WNSDeviceSerializer(UniqueRegistrationSerializerMixin, ModelSerializer): class Meta(DeviceSerializerMixin.Meta): model = WNSDevice @@ -211,3 +217,12 @@ class WebPushDeviceViewSet(DeviceViewSetMixin, ModelViewSet): class WebPushDeviceAuthorizedViewSet(AuthorizedMixin, WebPushDeviceViewSet): pass + + +class FCMDeviceViewSet(DeviceViewSetMixin, ModelViewSet): + queryset = FCMDevice.objects.all() + serializer_class = FCMDeviceSerializer + + +class FCMDeviceAuthorizedViewSet(AuthorizedMixin, FCMDeviceViewSet): + pass diff --git a/push_notifications/migrations/0008_fcmdevice.py b/push_notifications/migrations/0008_fcmdevice.py new file mode 100644 index 00000000..be5fcd2a --- /dev/null +++ b/push_notifications/migrations/0008_fcmdevice.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.12 on 2021-07-08 05:32 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('push_notifications', '0007_uniquesetting'), + ] + + operations = [ + migrations.CreateModel( + name='FCMDevice', + fields=[ + ('gcmdevice_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='push_notifications.gcmdevice')), + ('platform', models.CharField(blank=True, choices=[('i', 'ios'), ('a', 'android'), ('w', 'web')], help_text='Optional device platform: i - ios, a - android, w - web', max_length=1, null=True)), + ], + options={ + 'verbose_name': 'FCM device', + }, + bases=('push_notifications.gcmdevice',), + ), + ] diff --git a/push_notifications/models.py b/push_notifications/models.py index 3beefa02..f67f540e 100644 --- a/push_notifications/models.py +++ b/push_notifications/models.py @@ -4,7 +4,6 @@ from .fields import HexIntegerField from .settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS - CLOUD_MESSAGE_TYPES = ( ("FCM", "Firebase Cloud Message"), ("GCM", "Google Cloud Message"), @@ -124,7 +123,7 @@ def send_message(self, message, creds=None, **kwargs): if self.exists(): from .apns import apns_send_bulk_message - app_ids = self.filter(active=True).order_by("application_id")\ + app_ids = self.filter(active=True).order_by("application_id") \ .values_list("application_id", flat=True).distinct() res = [] for app_id in app_ids: @@ -257,3 +256,29 @@ def send_message(self, message, **kwargs): return webpush_send_message( uri=self.registration_id, message=message, browser=self.browser, auth=self.auth, p256dh=self.p256dh, application_id=self.application_id, **kwargs) + + +class FCMDevice(GCMDevice): + PLATFORM_IOS = 'i' + PLATFORM_ANDROID = 'a' + PLATFORM_WEB = 'w' + + PLATFORM_CHOICES = ( + (PLATFORM_IOS, 'ios'), + (PLATFORM_ANDROID, 'android'), + (PLATFORM_WEB, 'web'), + ) + platform = models.CharField( + max_length=1, + help_text=_("Optional device platform: i - ios, a - android, w - web"), + choices=PLATFORM_CHOICES, + null=True, + blank=True + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.cloud_message_type = "FCM" + + class Meta: + verbose_name = _("FCM device")