Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending notification to GCM from Django via FCM token

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

like image 336
Crazy Frog Avatar asked Feb 24 '17 18:02

Crazy Frog


People also ask

How do I send FCM push notifications?

FCM also has a Console where you can send the Message/Notification from, without having your own app server. NOTE: Creating your own app server is up to you. Just stating that you can send a message/notification via the console. The URL used is "https://android.googleapis.com/gcm/send".

Can Django send push notifications?

Django-Webpush is a package that enables developers to integrate and send web push notifications in Django applications.

Is GCM and FCM same?

Firebase Cloud Messaging(FCM), was formerly known as Google Cloud Messaging(GCM). FCM is a cloud platform that provides messages and push notifications for operating systems- ios and Android, and websites as well.


1 Answers

To send a notification via your django app server, you need to:

  1. Install django-fcm and add 'fcm_django' to your INSTALLED_APPS.
  2. Run migrations so as to have a device relation ready in your database.
  3. Assuming you already can send notifications from the Firebase console, go to your Firebase console settings. On the Cloud Messaging tab, copy the Server Key. Save this key in your django configuration settings as FCM_APIKEY = <your_api_key>
  4. Go to your android device and retrieve the Token, the docs for this are at https://firebase.google.com/docs/cloud-messaging/android/client
  5. Now create a Device instance in django, use the retrieved token as the reg_id.
  6. To send a notification, you can send a notification using 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

like image 110
Njenga Saruni Avatar answered Nov 15 '22 06:11

Njenga Saruni