Firebase Cloud Messaging Server in Django Rest FrameWork
pip install drf-fcm
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'...',
'drf_fcm',
]
DRF_FCM = {
'API_KEY': <FCM_SERVER_KEY>,
}
urls.py
url(r'^fcm/', include('drf_fcm.urls')),
Available Endpoints:
-
http://localhost:8000/fcm/devices/{pk}/ (GET | PUT)
Register device:
Endpoint : http://localhost:8000/fcm/devices/ (POST)
Request Header: Authorization : Token "drf auth token"
Request Body:
{
"name": "Lenovo Vibe",
"device_id": "<IMEI>",
"reg_id": "<gcm reg id>",
"is_active": true
}
List all device for given user:
Endpoint : http://localhost:8000/fcm/devices/ (GET)
Request Header: Authorization : Token "drf auth token"
Request Body:```
{
count: 1,
next: null,
previous: null,
results: [
{
id: 2,
name: "My LG Optimus Black",
device_id: "XXXXXXXXXXXXXX",
reg_id: "12346767777",
is_active: true,
created_at: "2017-03-16T14:36:42.404759Z",
updated_at: "2017-03-16T14:36:42.404800Z"
}
]
}
Send Notifications via REST:
Endpoint : http://localhost:8000/fcm/send/ (POST)
Request Header: Authorization : Token "drf auth token"
Request Body:
{
"sender": ["reg_id_1", "reg_id_2"],
"data": {"notification":{"title": "hi", "body": "hello world"}}
}
Send Notification messages to 1 device
from drf_fcm.models import *
d = Device.objects.all()[0]
d.send_message({"notification":{"title": "hi", "body": "Hello DRF"}})
Send Notification messages to all device
from drf_fcm.models import *
d = Device.objects.all()
d.send_message({"notification":{"title": "hi", "body": "Hello DRF"}})
Send Notification messages to all device of particular user
from drf_fcm.models import *
d = Device.objects.filter(user=1)
d.send_message({"notification":{"title": "hi", "body": "Hello DRF"}})