Tutorial: Send push notifications to Android devices using Firebase SDK version 1.0.0-preview1
This tutorial shows how to use Azure Notification Hubs and the updated version of the Firebase Cloud Messaging (FCM) SDK (version 1.0.0-preview1) to send push notifications to an Android application. In this tutorial, you create a blank Android app that receives push notifications using Firebase Cloud Messaging (FCM).
Important
As of June 2024, FCM legacy APIs will no longer be supported and will be retired. To avoid any disruption in your push notification service, you must migrate to the FCM v1 protocol as soon as possible.
You can download the completed code for this tutorial from GitHub.
This tutorial covers the following steps:
- Create an Android Studio project.
- Create a Firebase project that supports Firebase Cloud Messaging.
- Create a notification hub.
- Connect your app to the hub.
- Test the app.
Prerequisites
To complete this tutorial, you must have an active Azure account. If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.
Note
Google/Firebase APIs are not supported in Azure China regions.
You also need the following items:
- The latest version of Android Studio is recommended.
- Minimum support is API level 19.
Create an Android Studio project
The first step is to create a project in Android Studio:
Launch Android Studio.
Select File, then select New, and then New Project.
On the Choose your project page, select Empty Activity, and then select Next.
On the Configure your project page, do the following:
- Enter a name for the application.
- Specify a location in which to save the project files.
- Select Finish.
Create a Firebase project that supports FCM
Sign in to the Firebase console. Create a new Firebase project if you don't already have one.
After you create your project, select Add Firebase to your Android app.
On the Add Firebase to your Android app page, do the following:
For Android package name, copy the value of the applicationId in your application's build.gradle file. In this example, it's
com.fabrikam.fcmtutorial1app
.Select Register app.
Select Download google-services.json, save the file into the app folder of your project, and then select Next.
In the Firebase console, select the cog for your project. Then select Project Settings.
If you haven't downloaded the google-services.json file into the app folder of your Android Studio project, you can do so on this page.
Switch to the Cloud Messaging tab.
Copy and save the Server key for later use. You use this value to configure your hub.
If you do not see a Server key on the Firebase Cloud Messaging tab, follow these steps:
- Select the three-dots menu of the Cloud Messaging API (Legacy) Disabled heading.
- Follow the link to Manage API in Google Cloud Console.
- In the Google Cloud Console, select the button to enable the Google Cloud Messaging API.
- Wait a few minutes.
- Go back to your Firebase console project Cloud Messaging tab, and refresh the page.
- See that the Cloud Messaging API header has changed to Cloud Messaging API (Legacy) Enabled and now shows a server key.
Configure a notification hub
Sign in to the Azure portal.
Select All services on the left menu, and then select Notification Hubs in the Mobile section. Select the star icon next to the service name to add the service to the FAVORITES section on the left menu. After you add Notification Hubs to FAVORITES, select it on the left menu.
On the Notification Hubs page, select Add on the toolbar.
On the Notification Hubs page, do the following:
Enter a name in Notification Hub.
Enter a name in Create a new namespace. A namespace contains one or more hubs.
Select a value from the Location drop-down. This value specifies the location in which you want to create the hub.
Select an existing resource group in Resource Group, or create a new one.
Select Create.
Select Notifications (the bell icon), and then select Go to resource. You can also refresh the list on the Notification Hubs page and select your hub.
Select Access Policies from the list. Note that two connection strings are available. You'll need them later to handle push notifications.
Important
Do not use the DefaultFullSharedAccessSignature policy in your application. This policy is to be used in the app back-end only.
Configure Firebase Cloud Messaging settings for the hub
In the left pane, under Settings, select Google (GCM/FCM).
Enter the server key for the FCM project that you saved earlier.
On the toolbar, select Save.
The Azure portal displays a message that the hub has been successfully updated. The Save button is disabled.
Your notification hub is now configured to work with Firebase Cloud Messaging. You also have the connection strings that are necessary to send notifications to a device and register an app to receive notifications.
Connect your app to the notification hub
Add Google Play services to the project
In Android Studio, select Tools on the menu, and then select SDK Manager.
Select the target version of the Android SDK that is used in your project. Then select Show Package Details.
Select Google APIs, if it's not already installed.
Switch to the SDK Tools tab. If you haven't already installed Google Play Services, select Google Play Services as shown in the following image. Then select Apply to install. Note the SDK path, for use in a later step.
If you see the Confirm Change dialog box, select OK. The component installer installs the requested components. Select Finish after the components are installed.
Select OK to close the Settings for New Projects dialog box.
Add Azure Notification Hubs libraries
In the build.gradle file for the app, add the following lines in the dependencies section:
implementation 'com.microsoft.azure:notification-hubs-android-sdk-fcm:1.1.4' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.android.volley:volley:1.2.1'
Add the following repository after the dependencies section:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url 'https://example.io' } } }
Add Google Firebase support
Add the following plug-in at the end of the file if it's not already there.
apply plugin: 'com.google.gms.google-services'
Select Sync Now on the toolbar.
Add code
Create a NotificationHubListener object, which handles intercepting the messages from Azure Notification Hubs.
public class CustomNotificationListener implements NotificationListener { @override public void onNotificationReceived(Context context, RemoteMessage message) { /* The following notification properties are available. */ Notification notification = message.getNotification(); String title = notification.getTitle(); String body = notification.getBody(); Map<String, String> data = message.getData(); if (message != null) { Log.d(TAG, "Message Notification Title: " + title); Log.d(TAG, "Message Notification Body: " + message); } if (data != null) { for (Map.Entry<String, String> entry : data.entrySet()) { Log.d(TAG, "key, " + entry.getKey() + " value " + entry.getValue()); } } } }
In the
OnCreate
method of theMainActivity
class, add the following code to start the Notification Hubs initialization process when the activity is created:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NotificationHub.setListener(new CustomNotificationListener()); NotificationHub.start(this.getApplication(), "Hub Name", "Connection-String"); }
In Android Studio, on the menu bar, select Build, then select Rebuild Project to make sure that there are no errors in your code. If you receive an error about the ic_launcher icon, remove the following statement from the AndroidManifest.xml file:
android:icon="@mipmap/ic_launcher"
Ensure that you have a virtual device for running the app. If you do not have one, add one as follows:
Run the app on your selected device, and verify that it registers successfully with the hub.
Note
Registration might fail during the initial launch, until the
onTokenRefresh()
method of the instance ID service is called. A refresh should initiate a successful registration with the notification hub.
Send a test notification
You can send push notifications to your notification hub from the Azure portal, as follows:
In the Azure portal, on the notification hub page for your hub, select Test Send in the Troubleshooting section.
In Platforms, select Android.
Select Send. You won't see a notification on the Android device yet because you haven't run the mobile app on it. After you run the mobile app, select the Send button again to see the notification message.
See the result of the operation in the list at the bottom of the portal page.
You see the notification message on your device.
Push notifications are normally sent in a back-end service such as Mobile Apps or ASP.NET using a compatible library. If a library isn't available for your back end, you can also use the REST API directly to send notification messages.
Run the mobile app on emulator
Before you test push notifications inside an emulator, make sure that your emulator image supports the Google API level that you chose for your app. If your image doesn't support native Google APIs, you might get a SERVICE_NOT_AVAILABLE exception.
Also make sure that you've added your Google account to your running emulator under Settings > Accounts. Otherwise, your attempts to register with FCM might result in an AUTHENTICATION_FAILED exception.
Next steps
In this tutorial, you used Firebase Cloud Messaging to broadcast notifications to all Android devices that were registered with the service. To learn how to push notifications to specific devices, advance to the following tutorial:
The following is a list of some other tutorials for sending notifications:
Azure Mobile Apps: For an example of how to send notifications from a Mobile Apps back end integrated with Notification Hubs, see Add Push Notifications to your iOS app.
ASP.NET: Use Notification Hubs to send push notifications to users.
Azure Notification Hubs Java SDK: See How to use Notification Hubs from Java for sending notifications from Java. This has been tested in Eclipse for Android Development.