Django-Fcm Documentation: Release 0.0.2
Django-Fcm Documentation: Release 0.0.2
Release 0.0.2
Chitrank Dixit
1 Quickstart 3
2 Sending messages 5
2.1 Multicast message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Topic messaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Signals 7
i
ii
django-fcm Documentation, Release 0.0.2
Django-fcm is a reusable application. It serves as a server for the FCM (Firebase Cloud Messaging) service and allows
you to register devices and send messages to them.
Contents:
Contents 1
django-fcm Documentation, Release 0.0.2
2 Contents
CHAPTER 1
Quickstart
urlpatterns = [
url(r'fcm/', include('fcm.urls')),
]
FCM urls:
* Register device
/fcm/v1/devices/
* Unregister device
/fcm/v1/devices/{id}/
FCM_APIKEY = "<api_key>"
Note: To obtain api key please go to https://console.firebase.google.com/ and grab the key for the server app.
3
django-fcm Documentation, Release 0.0.2
4 Chapter 1. Quickstart
CHAPTER 2
Sending messages
Using console:
# Get the list of devices
$ python manage.py fcm_messenger --devices
> Devices list:
> (#1) My phone
Note: For more information, see Lifetime of a Message and Sending a downstream message docs.
django-fcm supports sending messages to multiple devices that have opted in to a particular fcm topic:
5
django-fcm Documentation, Release 0.0.2
Signals
fcm.signals.device_registered
Sent when a device is registered. Provides the following arguments:
sender The resource class used to register the device.
device An instance of fcm.models.Device (see Extending device model) represents the registered de-
vice.
request The HttpRequest in which the device was registered.
fcm.signals.device_unregistered
Sent when a device is unregistered. Provides the following arguments:
sender The resource class used to unregister the device.
device An instance of fcm.models.Device (see Extending device model) represents the unregistered
device.
request The HttpRequest in which the device was unregistered.
7
django-fcm Documentation, Release 0.0.2
8 Chapter 3. Signals
CHAPTER 4
Allows you to store additional data in the device model (e.g. foreign key to the user)
In your application, you need to create your own Device model. This model has to inherit from
fcm.models.AbstractDevice.
# import the AbstractDevice class to inherit from
from fcm.models import AbstractDevice
class MyDevice(AbstractDevice):
pass
In the end, you have to inform django-fcm where it can find your model.
Add appropriate path to the settings.py file:
FCM_DEVICE_MODEL = 'your_app.MyDevice'
9
django-fcm Documentation, Release 0.0.2
Allows you to manage access to the FCM api using one of the available tastypie authentication methods -
ApiKeyAuthentication.
Adding authentication requires djangorestframework added to your INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
...
'fcm',
'rest_framework',
]
You need to extend Device model and add user field. (See Extending device model)
# your_app/models.py
from django.conf import settings
from django.db import models
from fcm.models import AbstractDevice
class MyDevice(AbstractDevice):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
In your application , you can create the serializer, or customize it according the extra field you have included in your
Device model.
11
django-fcm Documentation, Release 0.0.2
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Device
fields = ('dev_id','reg_id','name','is_active')
In your application, you need to create your view either through ModelViewSet or can user or override methods as
specified in django-rest-framework documentation.
from rest_framework import viewsets
from fcm.models import Device
from fcm.serializers import DeviceSerializer
class DeviceViewSet(viewsets.ModelViewSet):
queryset = Device.objects.all()
serializer_class = DeviceSerializer
router = routers.DefaultRouter()
router.register(r'devices', DeviceViewSet)
urlpatterns = [
url(r'^v1/', include(router.urls))
urlpatterns = [
url(r'', include('your_app.urls')),
]
6.1 Register
POST parameters:
dev_id Unique device identifier
reg_id Registration token
name Optional device name
curl -X POST -H "Content-Type: application/json" -d '{"dev_id": "test", "reg_id":"abcd", "name":"test
http://localhost:8000/fcm/v1/devices/
6.2 Unregister
POST parameters:
dev_id Unique device identifier
curl -X POST -H "Content-Type: application/json" -d '{"dev_id": "test"}' http://localhost:8000/fcm/v1
13
django-fcm Documentation, Release 0.0.2
f
fcm.signals, 7
15
django-fcm Documentation, Release 0.0.2
D
device_registered (in module fcm.signals), 7
device_unregistered (in module fcm.signals), 7
F
fcm.signals (module), 7
17