This tutorial will guide you through how to add push notifications to a chat application using PubNub. The tutorial will focus on how to integrate push notifications in a chat application that is already running on the PubNub network. If you would like to learn more about how the chat application was constructed, follow our Android Kotlin Simple Chat Application Tutorial.
Download the JetNews chat app from the GitHub Repository. You will be selecting the push-notifications branch which contains the completed chat application with push notifications.
In a terminal, navigate to an empty folder of your choosing. Enter the following command to clone the Repository.
Checkout the push-notifications branch.
Watch the video or follow the steps below to set up your environment.
Download the latest, stable version of Android Studio for your operating system if you don't already have it. This download includes the IDE, Android SDK, and Android Emulator.
You will be using Google's Firebase Cloud Messaging (FCM) as the Service Provider to deliver push notifications to the application. You will set up and configure a Firebase project that will be directly associated with your application.
Navigate to the Firebase console.
Press the Create a project button and enter in the name of your project. The Firebase console will automatically replace hyphens with spaces. Press continue.
Disable Google Analytics for your application by clicking on the blue slider as you will not need it for this project. Press create project.
After some time, the project will be ready. Press continue. You will be redirected to the Project Overview page.
In the center of the page, click on the Android App Icon in the "Get started by adding Firebase to your app" section to add an Android application to your Firebase project.
Register the Android app by providing the Android package name (in this case
com.example.jetnews). You can find this package name by navigating to the app level build.gradle file (usually app/build.gradle), and looking at android -> defaultConfig -> applicationId. Click on Register App.
Download the google-services.json config file. This file is the bridge between your FCM project and your Android application. You will need to add this file to the application module root directory (app). This will be detailed shortly. Click next.
Do not add the Firebase SDK. You will be using the PubNub SDK and a different Firebase library. These dependencies have already been added to the application and will be discussed shortly.
Click next, and then click Continue to console.
PubNub interacts with FCM by using the Legacy Cloud Messaging API. Although the FCM HTTP v1 API has better security and more feature customizations than the Legacy Cloud Messaging API, all push notifications are sent through PubNub messages. Each PubNub message is highly secured with TLS and AES256 encryption, and supports compliance for HIPAA, GDPR, SOC2 type 2, and CCPA, so you don’t have to worry about your push notifications being insecure.
You will need to now obtain the server key token necessary to have PubNub send push payloads to FCM to deliver push notifications to devices.
In the Firebase console, select the project previously created if you are not already in the Project Overview page for that project.
In the top left corner, click on the gear icon next to the Project Overview label and select Project settings. The Project settings page contains information about your application, and allows you to edit, configure, and obtain necessary information for your application. You can also re-download the google-services.json file in case you have lost it previously.
Click on the Cloud Messaging tab. At the top of the page, there are two APIs available: Firebase Cloud Messaging API (V1), which is by default enabled, and Cloud Messaging API (Legacy) which is by default disabled.
Enable the Cloud Messaging API (Legacy) by clicking on the three dots, and click on "Manage API in Google Cloud Console". The Google Cloud Console for your app opens in a new tab.
Click on Enable to enable Cloud Messaging. After a few seconds, you will have successfully enabled the legacy Cloud Messaging. Close this tab.
Refresh the Project settings page. The Cloud Messaging API (Legacy) will now be enabled. In the Key/Token section, copy the entire Token value to a text editor or leave this page open, as you will need to add this token to your PubNub Keyset in the Admin Portal later on.
Open the application in Android Studio. Switch to the Project View instead of Android View in the top left corner, as this tutorial describes locations of files in this Project View.
The tutorial will be referencing this view frequently when adding files, and will refer to this window as the project file viewer. Please keep this side toolbar open when developing your project.
Move the google-services.json file that was downloaded earlier to the app-level module folder.
Create an android virtual device via the Device Manager to emulate the application in a virtual environment.
Open the project level build.gradle file. The Google Services plugin dependency is required to be able to have the app level build.gradle files apply the plugin to open the google-services.json file.
Open the app-level build.gradle file (app/build.gradle). The following dependencies are necessary to add push notifications to an application using PubNub:
In the plugins block, the google-services plugin is used to load the google-services file to be able to communicate with FCM.
In the dependencies block, the Kotlin SDK and Chat components for Android dependencies are used for the PubNub API libraries for communicating with the PubNub network.
In the dependencies block, the Firebase Messaging dependency is used for the Legacy Cloud Messaging API functionality.
To receive messages from FCM in your application, a service that extends FirebaseMessagingService needs to be added to your Android application. This service is represented by the MyFirebaseMessagingService.kt class in app/src/main/java/com.example.jetnews/utils.
This class extends the
onNewToken is called whenever FCM assigns the Android device a new or different token (when starting the application for the first time or when the old token expires). You would then register the device token on a channel in the PubNub network.
onMessageReceived displays push notifications once received from FCM based on the type of push message received. The functionality of the class will be detailed in the Build and run section of the tutorial.
This class has been added as a service to the app's AndroidManifest.xml to listen for and handle any FCM operations. The metadata for
default_notification_channel_id, default_notification_channel_name, and
default_notification_channel_description are also located below the service. A Notification ID is required to be set when creating a notification channel, and FCM will use this value whenever incoming messages do not explicitly set a notification channel. You can learn more about this metadata in Android's documentation. You'll learn more about creating notifications in the build and run section of this tutorial.
View the top of the application's AndroidManifest.xml file. The PubNub Network API requires internet permissions, otherwise you cannot connect to the PubNub Network. View the internet permissions in the manifest element, before the application block.