Setting up Push Notifications with FCM
This document describes how developers can set up the Pivotal Cloud Foundry (PCF) Push Notification Service with the Firebase Cloud Messaging (FCM) platform so their apps can send push notifications to Android devices.
Prerequisites
The procedures in this document require the following:
- You must have access to a PCF environment with the Push Notification Service installed.
- You must have Android Studio 2.2 or later installed on your machine.
- You must have the Google Repository from the Android SDK Manager.
- You must have the Push Android SDK 1.7 or later from Github.
- The devices that you want to send push notifications to must run Android 2.3 (Gingerbread) or later.
- The devices that you want to send push notifications to must have Google Play Services 9.8.0 or later.
Prepare an FCM Project
Follow these steps to prepare an FCM project for your app.
- Navigate to the Firebase Console and create an account if you do not have one already.
- Once logged in, Create or Import a project you want to use with FCM.
- When prompted, click Add Firebase to your Android app.
- Enter a Package name that matches the ID of your app:
- For the push-sample app, the ID is
io.pivotal.android.push.sample
. - For the push-demo app, the ID is
io.pivotal.android.push.demo
.
- For the push-sample app, the ID is
- Ensure the Debug signing certificate SHA-1 matches the SHA-1 from your debug signing certificate. For instructions on how to get this fingerprint, refer to Authenticating Your Client in the Google APIs for Android documentation.
- After you finish creating or importing your project, a
google-services.json
file downloads. Keep track of this file for later use.
- Click your project.
- Click the settings icon next to your project name and select Project Settings.
- Select the Cloud Messaging tab.
- Record the Server key for later use.
Configure Your Push Dashboard
Follow the steps below to navigate to the Push dashboard and configure the Push Notification service.
You can navigate to the Push dashboard using either Apps Manager or the Cloud Foundry Command Line Interface (cf CLI). Use the cf CLI instructions if you did not enable the Push Apps Manager errand when deploying Elastic Runtime.
Navigate to Push Dashboard using Apps Manager
In a browser, navigate to
apps.YOUR-SYSTEM-DOMAIN
.Select the system org and the push-notifications space.
Click the Services tab.
Select the PCF Push Notification Service row and click the Manage link.
Navigate to Push Dashboard using cf CLI
Open a terminal window and log in:
$ cf login -a https://api.YOUR-SYSTEM-DOMAIN -u USERNAME -p PASSWORD
Target the correct org and space:
$ cf target -o system -s push-notifications
Run the following command:
$ cf service push-service-instance
Copy the URL from the Dashboard field and paste it into your browser.
Configure the Push Notification Service
Follow these steps to configure the Push backend by creating a new platform for the sample app.
- In the Push dashboard, select the + icon from the left to create a new app to send push notifications to, either the push sample app or push demo app.
- Enter a Name and Description.
- Once you create an app, select the Configuration tab for that app.
- Click Add New Platform.
- Enter a Name and Description, and choose a Mode.
- For Type, select
Android-FCM
. - Once created, click the pencil icon to edit the platform.
- In the Google Key field, paste the server key that you recorded earlier.
Note: You can add multiple FCM Platforms with server keys from different FCM projects, depending on how your FCM applications and projects are organized. There is no requirement that all FCM Platforms use the same server key in the Push backend.
Run the App on Your Device
Follow these steps to compile and deploy the app on your Android device.
- Navigate to the Push Android Samples repository.
- Clone the repository to your workspace.
- Checkout the
release-v1.7.0
branch, or the branch of a later version. - Copy the
google-services.json
file from earlier into your app project:- If you want to compile the sample app, copy the
json
file to thepush-sample
subdirectory of the app project. - If you want to compile the demo app, copy the
json
file to thepush-demo
subdirectory.
- If you want to compile the sample app, copy the
- Open a project in Android Studio using the repo you cloned.
- Open the
pivotal.properties
file.- For the sample app, you can find this file in
push-sample/src/main/res/raw/
. - For the demo app, you can find this file in
push-demo/src/main/assets/
.
- For the sample app, you can find this file in
- Update the file as follows:
pivotal.push.platformUuid
: This value must match the platform UUID of the FCM Platform you created in the previous section.pivotal.push.platformSecret
: This value must match the platform SECRET of the FCM Platform you created in the previous step.pivotal.push.serviceUrl
: Enter the server address to your push backend API in the form ofhttps://push-api.YOUR-SYSTEM-DOMAIN
. For more information, see API URL.
- Compile and deploy the application to your Android device.
Once the application registers with the Push backend, it can receive push notifications. To verify that your device registered, see the Devices tab in the Push dashboard. The device Type field displays a Firebase logo.
You can also send test pushes to the device from the Push dashboard.
Note: If you send a test push to your device from the Push dashboard, ensure the app is not open on your device. You cannot see the test push while the app is open.