IEMS5722 Assignment 4: Using Firebase Cloud Messaging and Notifications
Page 1/4
Submit to Blackboard by 23:59, Friday, December 10, 2021.
Notes:
1. Read carefully the instructions and requirements to understand what you have to do.
2. Follow the instructions to submit your files for marking.
3. Late submissions will receive 30% mark penalty.
4. This assignment accounts for 14% of your final grade.
1. Objectives
• To learn how to implement push messages in Android using Google Cloud
Messaging
• To learn how to use asynchronous tasks and message queues on the server side
2. Instructions
In this assignment, you will further develop your mobile app and your server application,
such that the server application will be able to push new messages to the Android app using
Firebase Cloud Messaging (FCM). Specifically, you will have to perform these tasks in this
assignment:
• Register for a Google account, create a project, and set up Firebase Cloud Messaging
API
• Extend your Android app to enable FCM, including getting a registration token from
FCM
• Extend your server application such that when a new message is submitted to a chat
room, all other users (devices) should be notified by using FCM messages
2.1 Preparations
You should register for a Google account, create a project, and enable the Google Cloud
Messaging API. You can follow what we discussed in Lecture 7. A detailed guideline can be
found at this website: https://firebase.google.com/docs/cloud-messaging/android/client
2.2 Extending the Android App
In order to receive FCM messages, you have to extend your existing app. A detailed guide
can be found here: https://firebase.google.com/docs/cloud-messaging/android/client. In
summary, you will have to complete these steps:
• Add Firebase Cloud Messaging to your Android project.
• Add code to check for Google Play Services in the main activity’s onCreate and
onResume methods.
• Create a class that extends FirebaseMessagingService that will obtain a token for
FCM.
• Add code to report the received token to your own server application.
• Use the above class that extends FirebaseMessagingService to handle FCM
messages pushed from the server.
https://firebase.google.com/docs/cloud-messaging/android/client
https://firebase.google.com/docs/cloud-messaging/android/client
Page 2/4
• Generate a notification when a push message is received, regardless whether the app
is in foreground or background. Use the chat room name as the title of the
notification, and put the message in the short description field.
You can refer to the sample app at the following URL:
https://github.com/firebase/quickstart-android/tree/master/messaging
2.3 Extending the Server Application
Next, you should extend your server such that it can send out push notifications by sending
requests to Google’s FCM server when necessary.
2.3.1 Storing the Token Submitted by the Client
First, you should create a new table in the database, which will be used to store the tokens
submitted by the app. You can create a simple table with the following schema, and insert a
new record whenever a client has submitted a token.
CREATE TABLE push_tokens (
id INT NOT NULL AUTO_INCREMENT,
user_id INT NOT NULL,
token VARCHAR(256) NOT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET = utf8
Next, you should create a new API to allow the client to report the token. Create an API as
specified below.
API POST /api/a4/submit_push_token
Descriptions For submitting a token for a specific user
Input Parameters • user_id (the unique ID of the user, use your student ID for now)
• token (the push token generated for FCM)
Example POST the following to the API:
user_id=1234567890&token=iamadummytokeniems5722
Sample Output {
“status”: “OK”
}
2.3.2 Generating Push Messages Asynchronously
We discussed about asynchronous tasks and message queues in Lecture 9. In this
assignment, you will use these to handle push notifications. This is because sending request
to Google’s FCM server may take some time, and therefore the operation should not be
carried out within the HTTP request-response cycle.
https://github.com/firebase/quickstart-android/tree/master/messaging
Page 3/4
Fig. 1. Data Flow of the Instant Messaging App
In order to extend the system, you should first install RabbitMQ and also Celery. Follow the
instructions in the section “Using rabbitmq.com APT Repository” at
https://www.rabbitmq.com/install-debian.html to install RabbitMQ server.
Next, you will need to install the Celery Python library. Install this by using the following
command:
$ sudo pip3 install celery
2.3.3 Modifying the server application
First, you should create a new file (e.g. task.py), in which you will implement a function with
the logic to send push messages to all users of the app.
Next, in your app.py file, in the function that is associated with the /api/a3/send_message
API, you should call the function implemented in task.py after receiving a message from the
client side. You should call the function using .delay().
Refer to the materials in Lecture 9 (e.g., pp. 45–47) when implementing this part.
2.3.4 Deploy the Celery work
Once you have finished extending the server application and implementing the Celery
worker. You can test the Celery worker by running it using the follow command:
$ celery -A task.celery worker
You can refer to the instruction of how to deploy your Flask app, and deploy the Celery
worker in a similar way: create a new configuration file under /etc/supervisor/conf.d, and
update supervisor.
http://www.rabbitmq.com/install-debian.html
http://www.rabbitmq.com/install-debian.html
Page 4/4
3. Requirements
You should have implemented the following features in this assignment:
• Extends your Android app and makes it able to receive FCM messages.
• Upon receiving a FCM message, the Android app should create a notification to
notify the user of the new message received.
• Create a new API /api/a4/submit_push_token to let the client to submit the FCM
push token to the server, which will save the (user_id, push_token) pair in a new
database table.
• Implement a Celery task that will send requests to FCM to push messages to the
clients.
• Extends the server application’s /api/a3/send_message API to submit a task to the
Celery worker when a user submits a new message to a chat room.
4. Submission
To facilitate marking of the assignments, you should strictly follow the instructions below:
To submit your assignment, create a folder name
should include the following items:
• All source codes of your Python server application (including app.py and task.py)
• The APK file generated using Android Studio (a debug version is enough)
• The whole project folder of the android app (delete the ‘build’ folders to reduce the
total size of the submission)
Compress this folder using ZIP, you should now have a file named
https://blackboard.cuhk.edu.hk/