Integrate Firebase Push Notifications in Android Application

Firebase push Notifications in Android

1) Introduction of the tools (here Android N and IDE you are going to use)

Android “Nougat” (codenamed Android N during development) is the seventh major version of the Android operating system.
Android studio is used for developing android applications.

2) Setting up the environment.

In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

3) Tutorial.

In this tutorial you can learn how to integrate Firebase Push Notifications in android application.

ANDROID FIREBASE

The Google IO 2017 announced the use of FIREBASE in ANDROID. Google has moved from GCM that is Google Cloud Messaging to FCM that is FIREBASE CLOUD MESSAGING.

Firebase helps to build android apps better. Firebase allows to create a wide range to apps.

Let’s check out what FIREBASE provides for the development of an application.

Here is the list :

  1. Realtime Database
  2. Crash Reporting
  3. Authentication
  4. Cloud Functions
  5. Cloud Storage
  6. Hosting
  7. Test Lab for Android
  8. Performance Monitoring
  9. Google Analytics
  10. Cloud Messaging
  11. Dynamic Links
  12. Remote Config
  13. Invites
  14. App Indexing
  15. AdMob
  16. AdWords

We will check with a simple demo on REGISTRATION AND LOGIN by using ANDROID FIREBASE.

Firstly, let’s have a look at the steps:

1

Android Studio: Create a project in Android studio

2

Firebase Console: First step is to register the project on firebase console.

3

Register firebase: On your project in the android studio.

As simple as that.

Let’s proceed with the practical. Here, step by step we will create the project and run on a real android device.

STEPS FOR FIREBASE AUTHENTICATION IN ANDROID APP DEVELOPMENT:

1. Create a Project in Android Studio

2. Select your Platform and proceed.

3. Now You have one Activity. We are going to setup FIREBASE now.

4. For setting up the FIREBASE, go to TOOLS=>FIREBASE

5. Here, the window of firebase opens up and select the tool that you want to use. So, lets select the notification of firebase.

6. Once you click on the “receive the notification”, the window will be opened with the suggestions. Now click on “connect to firebase”

Now it will ask to create a project on firebase

Click on “CONNECT TO FIREBASE”.

As soon as the firebase is connected, it will show “CONNECTED” as above.

7. Now click on “ADD NOTIFICATIONS TO THE APP”. It will open a dialog asking for accept changes.

So click on “ACCEPT CHANGES”

8. By including this dependency, the app is ready to get the notifications as the set us is done.

9. Now create a new class MyFirebaseInstanceIDService

10. After creating this class, extend the class to FirebaseInstanceIdService.

11. Import com.google.firebase.iid.FirebaseInstanceIdService

12. Now in manifest file add the snippet : By adding this, we are registering the service in manifest file.

<service android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>

13. Now add the methods as below:

/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the
InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
[START refresh_token]
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.i("PRU TOKEN", "Refreshed token: " + refreshedToken);
// TODO: Implement this method to send any registration to your app's servers.
sendRegistrationToServer(refreshedToken);
}
// [END refresh_token]
private void sendRegistrationToServer(String token) {
 // Add custom implementation, as needed.
 }

These methods are used for getting the token from device.

14. Now add the service for getting the notifications from the server.

15. Create a new java class MyFirebaseMessagingService and extend it to FirebaseMessagingService.

In this service, we add two methods:
a) onMessageReceived(RemoteMessage remoteMessage) and
b) sendNotification(String messageBody)

16. Now add the following snippet in manifest file:

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

17. Now we implement the methods:

/**
* Called when message is received.
@param remoteMessage Object representing the message received from Firebase
Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.i("PRUSHNI", "From: " + remoteMessage.getFrom());
Log.i("PRUSHNI", "Notification Message Body: " +
remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody());
}
// [END receive_message]

18. Now we create notification for getting the message from server

private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri=
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("FCM DEMO")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

19. Now open the firebase console: https://console.firebase.google.com/

firebase console

Either you can create new project or you can use the project created from the android studio itself.

20. Once you click on the project, a new window opens:

click on the project

On the left sided panel, there is a tab of notification, Click on the “NOTIFICATIONS”

Click on “Send your first message” and a new window will be opened where you can write the message.

Add the message and click on send message

And then you are done with the coding. This is how we can implement the android firebase push notifications.

Conclusion

I hope this blog post will help you to how to Integrate Firebase Push Notification in Android. Let me know in a comment if you have any questions regarding Android. I will reply you ASAP.

Experienced Android Developer with a demonstrated history of working in the computer software industry. Expert Android Trainer at Code Kaksha. Skilled in Android, Eclipse, XML, Android Development, and Android Studio. Strong engineering professional with a Bachelor’s Degree focused in Computer Engineering from C.U.Shah College of Engineering and Technology.