Django 和 FCM 发送推送通知

Django and FCM to send push notifications

使用自定义设备信息模型使用 fcm-django 的正确方法是什么,或者使用更多属性扩展 FCMDevice 的可能性是什么。

例如,我需要在设备信息中存储一些额外的字段,如语言、位置 (lat/lng)、应用程序版本、供应商等。

我是 FCM 的新手,但目前对我来说,设备注册流程不是很清楚。 我当前的项目是移动应用程序的后端服务。所以,我有一些使用 djnago-rest-framework 的 REST 服务。

我已经有一个 API 到 register/update 的移动设备。我可以添加 FCM 注册 ID 来重复使用它吗?

您可以通过扩展 AbstactFCMDevice class 来定义自定义 FCMDevice class,如下所示:

from fcm_django.models import AbstractFCMDevice
from django.db import models

class CustomFCMDevice(AbstractFCMDevice):
    language = models.CharField(max_length=35, blank=False)
    position = models.CharField(max_length=35, blank=False)
    app_version = models.CharField(max_length=35, blank=False)
    ...
    ..

然后您可以使用您的自定义 class 来获取您的查询集:

from custom_fcm_django.models import CustomFCMDevice

device = CustomFCMDevice.objects.all().first()

device.send_message(title="Title", body="Message", icon=..., data={"test": "test"})

您可以使用 django rest framework APIView 来创建您的 API 端点,例如:

from rest_framework.views import APIView

class FCMDevicesListAPIView(APIView):

permission_classes = (IsAuthenticated, )

def get(self, request):
    queryset = CustomFCMDevice.objects.all()
    serializer = FCMDeviceSerializer(queryset, many=True)
    return Response(serializer.data)