简体   繁体   English

Django 和 FCM 发送推送通知

[英]Django and FCM to send push notifications

What's the correct way to use fcm-django using a custom Device-info model or a possibility to extend FCMDevice with more attributes.使用自定义设备信息 model 或使用更多属性扩展 FCMDevice 的可能性来使用 fcm-django 的正确方法是什么。

For example, I need to store into the device info some additional fields like language, position (lat/lng), app version, vendor etc.例如,我需要在设备信息中存储一些附加字段,如语言、position(纬度/经度)、应用程序版本、供应商等。

I'm new for FCM but for me, at the moment, It's not very clear the device registration flow.我是 FCM 的新手,但对我来说,目前,设备注册流程还不是很清楚。 My current project is a backend service for mobile apps.我目前的项目是移动应用程序的后端服务。 So, I have some REST service using djnago-rest-framework.所以,我有一些使用 djnago-rest-framework 的 REST 服务。

I already have an API to register/update a mobile device.我已经有一个 API 来注册/更新移动设备。 Can I reuse it adding the FCM registration-id?我可以重复使用它添加 FCM 注册 ID 吗?

You can define your custom FCMDevice class by extending AbstactFCMDevice class as follows:您可以通过扩展 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)
    ...
    ..

you can then use your custom class to get your queryset:然后,您可以使用您的自定义 class 来获取您的查询集:

from custom_fcm_django.models import CustomFCMDevice

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

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

you can use django rest framework APIView to create your API endpoints, for example:您可以使用 django rest 框架 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM