【问题标题】:Django and FCM to send push notificationsDjango 和 FCM 发送推送通知
【发布时间】:2020-09-17 23:40:46
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: django push-notification firebase-cloud-messaging


    【解决方案1】:

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

    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)
        ...
        ..
    

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2017-12-09
      • 1970-01-01
      • 2020-04-21
      • 2015-05-26
      相关资源
      最近更新 更多