Nimblechapps Blog

This is the section where we get a chance to rant about latest happenings in the tech world. Keen to know our thoughts? Refer our blogs

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.

[pastacode lang=”java” manual=”%3Cservice%20android%3Aname%3D%22.MyFirebaseInstanceIDService%22%3E%0A%3Cintent-filter%3E%0A%3Caction%20android%3Aname%3D%22com.google.firebase.INSTANCE_ID_EVENT%22%20%2F%3E%0A%3C%2Fintent-filter%3E%0A%3C%2Fservice%3E” message=”” highlight=”” provider=”manual”/]

13. Now add the methods as below:

[pastacode lang=”java” manual=”%2F**%0A*%20Called%20if%20InstanceID%20token%20is%20updated.%20This%20may%20occur%20if%20the%20security%20of%0A*%20the%20previous%20token%20had%20been%20compromised.%20Note%20that%20this%20is%20called%20when%20the%0AInstanceID%20token%0A*%20is%20initially%20generated%20so%20this%20is%20where%20you%20would%20retrieve%20the%20token.%0A*%2F%0A%5BSTART%20refresh_token%5D%0A%40Override%0Apublic%20void%20onTokenRefresh()%20%7B%0A%2F%2F%20Get%20updated%20InstanceID%20token.%0AString%20refreshedToken%20%3D%20FirebaseInstanceId.getInstance().getToken()%3B%0ALog.i(%22PRU%20TOKEN%22%2C%20%22Refreshed%20token%3A%20%22%20%2B%20refreshedToken)%3B%0A%2F%2F%20TODO%3A%20Implement%20this%20method%20to%20send%20any%20registration%20to%20your%20app’s%20servers.%0AsendRegistrationToServer(refreshedToken)%3B%0A%7D%0A%2F%2F%20%5BEND%20refresh_token%5D%0Aprivate%20void%20sendRegistrationToServer(String%20token)%20%7B%0A%20%2F%2F%20Add%20custom%20implementation%2C%20as%20needed.%0A%20%7D” message=”” highlight=”” provider=”manual”/]

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:

[pastacode lang=”java” manual=”%3Cservice%20android%3Aname%3D%22.MyFirebaseMessagingService%22%3E%0A%3Cintent-filter%3E%0A%3Caction%20android%3Aname%3D%22com.google.firebase.MESSAGING_EVENT%22%20%2F%3E%0A%3C%2Fintent-filter%3E%0A%3C%2Fservice%3E” message=”” highlight=”” provider=”manual”/]

17. Now we implement the methods:

[pastacode lang=”markup” manual=”%2F**%0A*%20Called%20when%20message%20is%20received.%0A%40param%20remoteMessage%20Object%20representing%20the%20message%20received%20from%20Firebase%0ACloud%20Messaging.%0A*%2F%0A%2F%2F%20%5BSTART%20receive_message%5D%0A%40Override%0Apublic%20void%20onMessageReceived(RemoteMessage%20remoteMessage)%20%7B%0A%2F%2FDisplaying%20data%20in%20log%0A%2F%2FIt%20is%20optional%0ALog.i(%22PRUSHNI%22%2C%20%22From%3A%20%22%20%2B%20remoteMessage.getFrom())%3B%0ALog.i(%22PRUSHNI%22%2C%20%22Notification%20Message%20Body%3A%20%22%20%2B%0AremoteMessage.getNotification().getBody())%3B%0A%2F%2FCalling%20method%20to%20generate%20notification%0AsendNotification(remoteMessage.getNotification().getBody())%3B%0A%7D%0A%2F%2F%20%5BEND%20receive_message%5D” message=”” highlight=”” provider=”manual”/]

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

[pastacode lang=”markup” manual=”private%20void%20sendNotification(String%20messageBody)%20%7B%0AIntent%20intent%20%3D%20new%20Intent(this%2C%20MainActivity.class)%3B%0Aintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)%3B%0APendingIntent%20pendingIntent%20%3D%20PendingIntent.getActivity(this%2C%200%2C%20intent%2C%0APendingIntent.FLAG_ONE_SHOT)%3B%0AUri%20defaultSoundUri%3D%0ARingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)%3B%0ANotificationCompat.Builder%20notificationBuilder%20%3D%20new%0ANotificationCompat.Builder(this)%0A.setSmallIcon(R.mipmap.ic_launcher)%0A.setContentTitle(%22FCM%20DEMO%22)%0A.setContentText(messageBody)%0A.setAutoCancel(true)%0A.setSound(defaultSoundUri)%0A.setContentIntent(pendingIntent)%3B%0ANotificationManager%20notificationManager%20%3D%0A(NotificationManager)%0AgetSystemService(Context.NOTIFICATION_SERVICE)%3B%0AnotificationManager.notify(0%2C%20notificationBuilder.build())%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

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.