Friday, May 22, 2020

Android push notifications using Firebase Cloud Messaging FCM

Here, I’ll show you how to quickly and easily send notifications from an external server, using Firebase Cloud Messaging (FCM). Once we’ve added FCM support to a project and sent a few test notifications, I’ll show you how to create more engaging notifications, by using the Firebase Console to target specific sections of your audience, including sending a notification to a single device, using their unique token ID.

What is Firebase Cloud Messaging?

FCM is a free, cross-platform messaging solution that lets you send push notifications to your audience, without having to worry about the server code. By using FCM alongside Firebase’s Notifications Composer (as seen in the following screenshot), you can create notifications that target very specific sections of your user base, often without having to write any special code.

FCM supports two types of messages:


Notification messages. The client application will behave differently depending on whether it’s in the background or the foreground when it receives the FCM message. If your app is in the background, then the Firebase SDK will automatically process the message and display it as a notification in the device’s system tray. Since the Android system builds the notification for you, this is one of the easiest ways to send push notifications to your users. If your app receives an FCM message while it’s in the foreground, then the system won’t handle this notification automatically, leaving you to process the message in your app’s onMessageReceived() callback. We’ll be exploring onMessageReceived() later in this tutorial, but for now just be aware that if your app receives a message while it’s in the foreground, then by default this message won’t be displayed to the user.
Data messages. Unlike notification messages, you can use data messages to send custom data elements to the client application. However, FCM does place a 4KB limit on these data messages, so if your payload exceeds 4KB then you’ll need to fetch the additional data using WorkManager or the JobScheduler API.


classpath 'com.google.gms:google-services:4.0.1'

//Open your app-level build.gradle file, and add the Google services plugin, plus the dependencies for //Firebase Core and FCM:

//Add the Google services plugin//

apply plugin: 'com.google.gms.google-services'

dependencies {
   implementation fileTree(dir: 'libs', include: ['*.jar'])

//Add Firebase Core//

   implementation 'com.google.firebase:firebase-core:16.0.1'

//Add FCM//

   implementation 'com.google.firebase:firebase-messaging:17.3.4'

When prompted, sync your changes.

Next, you need to let the Firebase Console know that you’ve successfully added Firebase to your project. Install your app on either a physical Android smartphone or tablet, or an Android Virtual Device (AVD).
Back in the Firebase Console, select “Run app to verify installation.”
Once Firebase has detected your app, you’ll see a “Congratulations” message. Select “Continue to the console.”
Sending your first push notification with Firebase
And that’s it! You can now send a push notification to your users, and that notification will appear in the device’s system tray (for now, let’s assume your app isn’t in the foreground when the message is delivered).

You create FCM notifications using the Notifications Composer, which is available via the Firebase Console:

Make sure your app is installed and running in the background, and that your device has an active Internet connection.
In the Firebase Console, select “Cloud Messaging” from the left-hand menu under GROW section.



Select “Send your first message.”
Give your message a title and some body text, and then click “Next.”



Open the “Select app” dropdown, and choose your application from the list. This section also includes some advanced options that you can use to create targeted notifications, based on factors such as app version, the device’s locale, and the last time the user engaged with your app. We won’t be using any of these options in our test notification, but if you want to see what’s available, then select “and…” and explore the subsequent dropdown.



Once you’ve finished editing this section, click “Next.”
Assuming you want to send this message immediately, open the “Send to eligible users” dropdown and select “Now.”
In the bottom-right of the screen, click “Publish.”
Check all the information in the subsequent popup, and if you’re happy to proceed then select “Publish.”

After a few moments, all the client devices that you targeted should receive this notification in their system tray.

Most of the time, FCM notifications will be delivered immediately, but occasionally it may take a few minutes for a message to arrive, so don’t panic if your notification is delayed.

By default, your app won’t display any FCM messages it receives while it’s in the foreground, so when you send a message there’s no guarantee your users will actually see that message.

To act on the messages your app receives while it’s in the foreground, you’ll need to extend the FirebaseMessagingService, override the onMessageReceived method, and then retrieve the message’s content using either getNotification or getData, depending on whether you’re working with data or notification messages, or both.

If you need step by step procedure to implement the functionality for recieving notification while app is in foreground, Please comment based on request we will post the code snippets.

No comments:

Post a Comment