Enabling Mobile Push Notifications: Android
What are Push Notifications?
A push notification is a message typically sent from a server to a device that instructs mobile applications to perform some action. Push notifications can deliver information to users on their devices when the application is in the foreground, background, or even closed. You can draw users back to your app by reminding and informing them of important notices, messages, timers, and events.
PubNub's Mobile Push Gateway bridges Android native message publishing with Google's Firebase Cloud Messaging (FCM), a third-party push notification service, in which sends your push message in a PubNub payload and to have FCM deliver push notifications to devices associated with your application.
Push notifications are delivered in the following manner on the PubNub network:
The application publishes to PubNub via a channel that has a device registered to receive push notifications with the push payload.
PubNub inspects the payload. If a specific mobile payload is found, it checks for device registrations on the channel. If found, PubNub forwards the push to FCM.
FCM then delivers the push notification to one or more devices.
Watch the video below or follow the step-by-step instructions to learn how to enable Android push notifications for your PubNub keyset, create a Firebase project to set up an Android application to receive push notifications from FCM, and obtain an FCM API Key necessary to connect PubNub to FCM. If you would like to follow a more detailed example, walkthrough our detailed tutorial to set up and add push notifications to an existing chat application built with PubNub.
Need extra information? Check out our in-depth how-to to learn more about push notifications in the PubNub network and how they can enhance your application and user experience. Be sure to also checkout PubNub’s official documentation on Mobile Push Gateway to learn more about push notifications.
Login to the Admin Portal.
Click on the apps tab on the left hand side of the portal.
Click on an application. You will be brought to the keysets page for this application.
Click on a keyset. You will be brought to the configuration page for this keyset.
Enable Mobile Push Notifications by clicking on the slider to turn it on. This enables the Mobile Push Gateway, the bridge between your PubNub application and FCM.
Mobile Push Notifications Webhook Settings. These are not required for push functionality to work in your application, but are necessary to receive events on your server.
Push Device Removed Webhook: Triggers the http(s) endpoint when a device token is removed from a channel. You learn more on how to implement this in our Android documentation.
Push Error Webhook: Triggers the http(s) endpoint in case of push notification errors. You can learn more about troubleshooting push notifications in our Troubleshooting docs.
Apple Push Credentials. This setting needs to be configured to receive push notifications on iOS devices. Learn how to receive push notifications for iOS devices in our documentation.
Firebase Cloud Messaging. The setting to configure to receive push notifications on Android devices. To send push notifications to FCM, PubNub requires an FCM API Key. To obtain this key, you need to create a Firebase project, register your Android app with this project, and enable the Legacy Cloud Messaging API to receive the FCM API Key.
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.
Choose to enable/disable Google Analytics for your application by clicking on the blue slider. 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 (i.e.
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.
Add the Firebase SDK to your project-level build.gradle (<project>/build.gradle) and app-level build.gradle (<project>/app/build.gradle) files. For the app-level build.gradle file, we recommend you use the latest version of the
'com.google.firebase:firebase-messaging-ktx'library. You can learn more in our Android Kotlin Push Notifications tutorial in the Project Level Dependencies step.
Click next, and then click Continue to console.
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 Server Key Token value to the FCM API Key setting field in your PubNub Keyset.
Microsoft Push Notifications. The setting to enable push notifications to be received on Microsoft devices. Please note that this setting is deprecated and is planned to be removed in the near future.
Click on Save Changes to save these updated changes.