Using Cron Jobs to deploy FCM Push Notifications

Nikhil Sachdeva
Analytics Vidhya
4 min readMar 25, 2020

--

Push notifications are the first and often most important communications channel used by apps. Nearly every major app uses push notifications for transactions and re-engagement. It is an essential tracking metric for every product. A recent study showed that mobile users receive an average of 63.5 notifications per day, with most notifications coming from messaging and email applications.

Let’s Begin!!

Following libraries will be used to implement automated Push Notification service :
Firebase Messaging Android SDK
pyfcm
python-crontab

Full implementation using a MySQL client is available here.

This tutorial can be broken down into 3 steps :

  1. Integrating Firebase and FCM services in the app.
  2. Implementing notification service using pyfcm.
  3. Automating the service using Cron Jobs by python-crontab.

Step 1

Integrate Firebase in the app using the official docs and firebase console. Then, add the dependency for the Cloud Messaging Android library to your module (app-level) Gradle file (usually app/build.gradle):

implementation 'com.google.firebase:firebase-messaging:20.1.2'

Create FirebaseMessagingService as per the tutorial on Firebase docs.

Use the onNewTokenmethod to retrieve the FCM token. This token will be used by the service to send push notifications to devices. Ideally, send the token to server at “Login” event of user for each user’s lifecycle. To do this, implement the following method.

FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceId failed", task.getException());
return;
}

// Get new Instance ID token
String token = task.getResult().getToken();
sendTokenToServer(token);

}
});

The sendTokenToServer() method will be used to send the token to the backend database to be stored against each user ID. For ex, in a REST framework, a POST query can be called here which saves Token against each user ID.

The FCM architecture

Step 2

Now we’ll use olucurious/PyFCM library to send Push Notifications to multiple devices. There are two ways to do so — > either subscribe devices to a topic or send notifications to a list of FCM tokens. Get API key using :
firebase console ->Settings -> Cloud Messaging.

Sending to a list of FCM tokens using pyfcm :

In this method, only server side code has to written as follows. Running this python script will send the message as notification to the list of devices whose FCM tokens are added to the list of registration_ids.

# Send to single device.
from pyfcm import FCMNotification
push_service = FCMNotification(api_key="<api-key>")# Send to multiple devices by passing a list of ids.registration_ids = ["<device registration_id 1>", "<device registration_id 2>", ...]
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)

print result

Sending notifications using topics subscription :

Client side code :

FirebaseMessaging.getInstance().subscribeToTopic("weather")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = getString(R.string.msg_subscribed);
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});

Server side code :

push_service = FCMNotification(SERVER_KEY)
tokens = [
<registration_id_1>,
<registration_id_2>,
]

subscribed = push_service.subscribe_registration_ids_to_topic(tokens, 'test')
result = push_service.notify_topic_subscribers(topic_name="weather", message_body=message)


result = push_service.notify_topic_subscribers(message_body=message)

Important : Write the conditions for events at which notifications are supposed to be triggered in the python scripts. For each FCM token, check user DB and create conditions as per requirement.

Step 3

Now to automate this script and run it at fixed intervals, python-crontab will be used.

pip install python-crontab

In a separate python file, implement a new Cron Job as following :

from crontab import CronTabcron = CronTab(user='UserName')job = cron.new(command='command')job.minute.every(1)cron.write()

In command write the commands that the script needs to follow. These commands will run as a sh shell script from root directory. Hence, change the directory in which the project is saved. Also, python as command cannot be directly used. Its path must be provided in the command. To do this run “whereis python” as follows.

Finding path of installed libraries.

Now change the command to the following.

job = cron.new(command='cd Documents/project-notifs && /usr/bin/python notifications.py')

Determine the intervals as per requirement and run this python script. To check cron jobs running in background, you can run crontab -l in terminal. If you wish to use libraries in the python scripts, make sure to install them in the Home directory using -H flag with pip install.

Now, you can run this cron job on your system or host this job on any hosting website.

Find full implementation in a project I did here.

--

--