Source code download:
Source code download:
How it works
There are 2 ways that you can send notifications to an individual or a group of users using Firebase Cloud Messaging programmatically.
The first thing we need to have is a Firebase project that is linked with your Android application. Go to console.firebase.google.com, sign in with your Google Account and create a project.
Once you have your Firebase project, click on the Android Icon in the Project Overview page.
For the package name, use the package name from the MainActivity file of your application.
Then generate a SHA-1 Certificate by going back to Android Studio.
Copy the SHA-1 certificate in the terminal and paste it inside the textbox in Firebase.
On Step 2, download the google-services.json file and find it.
Go to the Firebase console and continue to Step 3.
Copy the code in the dependencies section and add it to the dependencies section of your project’s build gradle file.
Go back to the Firebase console and scroll down until you see the code to add more dependencies. Copy the apply plugin code and add it to the app’s build grade file in Android Studio
apply plugin: 'com.google.gms.google-services'
Continue by copying the dependencies code and add them to the app.
Click on Sync Now and wait until the process is finished.
Once the project is linked with the Android application, we can begin building the service to handle our notifications.
Building the Notification Service
Go to the app build gradle file, in the dependencies section, add the Firebase Cloud Messaging dependency and Sync the project.
Once the process is finished create a new class call PushNotification Service and have it extend the FirebaseMessagingService class.
Override the onMessageReceived() method from the FirebaseMessagingService Class.
By default, a notification will only appear when the app is closed or in the background. To change this The RemoteMessage object represents the notification message that was received.
Now let’s add the code to display the notification.
First we need to have a notification channel. The purpose of the channel is to establish a link between our app and the Android device.
NotificationChannel channel = new NotificationChannel();
It takes 3 parameters.
If we look at the Android Developer documentation, there are 4 importance levels: High, Default, Low, and Min.
Once we have our channel, we need to add the channel to the Android device. Call getSystemService() and pass in the NotificationManager class, then call createNotificationChannel() and pass in the channel.
Now that the app is linked with the device, we can create the notification object and display it.
Use the Notificaton Builder to build the notification.
The last step we need to do is to send the notification through the channel so it’ll get display.
Now that we have our notification service code set up to handle our incoming notifications, we need to add it inside the manifest file to activate it.
This is all we need to do to start receiving notifications from Firebase. Let’s see how we can add triggers in Cloud Functions to automatically fire and send notifications to users.
Installing NodeJS & Firebase Tools
We’ll begin by creating a Cloud Functions project. Make sure you have NodeJS and firebase-tools installed on your computer.
Run firebase - -version for firebase-tools
firebase - -version
If you do not have them installed, go to nodejs.org and follow the steps to install node.
npm install -g firebase-tools
Creating a Firebase Cloud Functions Project
When you have verified that you have NodeJS and firebase-tools installed, create a folder where it is easy to locate.
Select the Firebase project you have for the app and continue.
Once Cloud Functions is set, open the project in a code editor. Expand the functions folder and go to the index.js file. In here, we can define our trigger functions.
To define a function for Cloud Functions, grab the exports object and use the dot operator to add a name for the function.
Inside the parenthesis of document(), pass in the location of where you want to detect.
However, instead of using a static value to represent a single document, wrap the document id with the curly brackets to represent any document.
The onCreate() function represents the action we want to detect. Whenever a document is created on this path, we’ll run this block of code. This will be our trigger to send notifications to users.
Inside trigger function, grab the admin object, get the messaging() module, and call the sendToTopic() function.
The next thing we need to do is to deploy this function to cloud functions to activate the trigger. Save the project and run the deploy command in the terminal.
firebase deploy --only functions
Make sure your Firebase project is upgraded to the Blaze plan before executing the command or you will not be able to deploy the function.
The Firebase Blaze plan is free until you reach a certain quota. For this tutorial, it will cost nothing.
Once the deployment is completed, go to the Firebase console. In the Functions tab, you should see the function in the system.
Subscribing to a Topic
Now that we have our trigger set up, we need to let the user subscribe to the topic we are broadcasting to.
To subscribe to a topic, take the FirebaseMessaging object, call the subscribeToTopic() method, and pass in the topic you want to subscribe to.
If we run the application and add a document to the Notifications collection, a notification will appear.
To reiterate what is happening in the background, when we launch our Android application, the user will subscribe to the new_user_forum topic.
©2019 - 2023 Codeible. All rights reserved.