Reputation: 525
I am doing my first steps in Django-Android interaction, and I am bit confused about the logic how FCM technology works. I have installed django-fcm via pip, and now my goal is to send a notification to Android device via FCM token that has been sent to server by Android device via REST resource.
The third-party Android developers tell that they would give me only the FCM token and I should be able to send a notification. And I'm a bit confused by the following code snippet from the doc
devices = FCMDevice.objects.all()
What is FCM device ? And how does JSON code in the example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
...related to this:
device = FCMDevice.objects.all().first()
device.send_message("Title", "Message")
device.send_message(data={"test": "test"})
device.send_message(title="Title", body="Message", icon=..., data={"test": "test"})
THE QUESTION IS what is the minimum code snippet to send the simplest notification to an Android device identified by its FCM token
Upvotes: 6
Views: 7961
Reputation: 116
To send a notification via your django app server, you need to:
'fcm_django'
to your INSTALLED_APPS
.FCM_APIKEY = <your_api_key>
device_instance.send_message
(notification = { "body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"})
To fully understand how django-fcm works, I would advice that you actually go through the source code. If an open source package is disturbing me, actually viewing the source code and reading the comments is sometimes enlightening. django-fcm is a small and simple package. The utils.py file is the most important. That is where the message is composed and sent to Firebase using the python's requests package. To understand this file. Please also read the docs for firebase at: https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
Upvotes: 7
Reputation: 2281
First of all, FCMDevice is a django model object which contains the data about a registered device, to which notifications can be sent.
It is expected, that you provide that data. When you mobile device/browser gets that data from its FCM token from fcm library, it is expected that you register that token within your backend that uses the fcm-django library.
Meaning, you create an object within your database with using the model FCMDevice from the library.
Minimal code required to send the notification is exactly that, which you have posted in this post.
I believe you should perhaps read some django tutorials, to better understand what models actually are.
Upvotes: 2