Android Firebase FCM firebase cloud messaging tutorial firebase push firebase push notification android firebase push notification android example firebase push notification android sample firebase push notification with image Push notification using firebase

Firebase Push Notification With Image Tutorial on Android : Firebase Cloud Messaging

Get 20% off all New HostGator Internet hosting plans with Coupon: SNAPPY.

Android Tutorials : Firebase Push Notification Tutorial

AndroidBash Firebase Push Notification

Firebase cloud messaging (FCM) is a new version of Google Cloud Messaging (GCM). Utilizing FCM you’ll be able to send notification messages to your shopper software as a way to drive consumer engagement. With FCM, you’ll be able to send two varieties of messages to the shopper app: Notification messages and Knowledge messages, A notification message is the extra lightweight choice, with a 2KB limit and a predefined set of user-visible keys. Knowledge messages let builders ship up to 4KB of custom key-value pairs. Notification messages can include an optionally available knowledge payload, which is delivered when customers faucet on the notification. Use notification messages if you want Firebase cloud messaging (FCM) to deal with displaying a notification on your shopper app’s behalf. Use knowledge messages whenever you need to process the messages on your shopper app.

Firebase Cloud Messaging

Firebase notifications behave in a different way depending on the foreground/background state of the receiving app. If you need foregrounded (still operating) apps to obtain notification messages or knowledge messages, you’ll need to put in writing code to handle the onMessageReceived callback. In the overridden technique FirebaseMessagingService.onMessageReceived(…), you will get the message knowledge from the acquired message and carry out the desired action in response to the needs of your android app.

So to obtain messages, you need to use a service that extends FirebaseMessagingService. Your service should override the onMessageReceived callback. The tactic onMessageReceived(…) won’t get referred to as if the app is in background or killed state, when the message is shipped by way of Firebase Console. But when the message is shipped by way of API (carry out a POST to the next URL : ), the tactic onMessageReceived(…) will get invoked. So on this tutorial we can be sending messages by way of Firebase console in addition to by way of API.

Venture on Github

Now allow us to create an Android software to which we will ship push notifications utilizing Firebase Notifications. The steps can be described in 5 detailed but simple elements.

Part ! : Consists of steps to create a brand new Android software and set up Firebase in it.

1. Create a new Android Undertaking in Android Studio. Give it a name and select the Minimal SDK on which your app will run on. I chose API 16 : Android 4.1 (JELLY_BEAN).

2. If you end up prompted to add an exercise to your software choose Blank Exercise and click on subsequent button.

3. Within the subsequent step click on End and in few seconds your software must be loaded in Android Studio.

4. Open build.gradle(Module:App) file of your software and add the next dependencies inside dependencies section of construct.gradle (Module:App) file.

5. Now add the packagingOptions directive to your build.gradle(Module : App) file as proven under. This is how your build.gradle (Module : App) will look now.

6. The appliance requires following permissions to work. Your app won’t work until you add these permissions to your AndroidManifest.xml file.

Half !! : Consists of Firebase account creation and getting google-services.json file from Firebase console

1. The very first thing it is advisable to do to get began with Firebase is sign up for a free account. You should use your Google account to sign up to your firebase account.

Android Firebase AuthUpdated

2. After clicking on ‘Sign In with Google’ you’ll be requested to allow the Firebase to view your E mail and your primary profile. Click on Permit button to proceed.

Firebase Email Authentication

3. As soon as you’re signed in, Click on Go to your Console button in case you are not mechanically redirected to your firebase console. Click on Create New Challenge button in your firebase console.

Android Firebase AuthUpdatedAndroidBash Firebase Push Notification

four. Now you possibly can see an summary of your new Firebase challenge. Now click on Add Firebase to your Android App as shown under.

AndroidBash Firebase Push Notification

5. Now you’ll want to enter your App particulars (your software’s package deal identify) and in addition get SHA-1 finger print and paste it in the area as proven under.

AndroidBash Firebase Push Notification

Click on on Add App button. Upon clicking it, a file named google-services.json gets downloaded to your machine.

Notice : Following image exhibits find out how to get SHA-1 from Android Studio.

AndroidBash Firebase SHA-1

6. Copy the google-services.json file and paste within the app folder of your Android software.

AndroidBash Firebase

Notice: For those who overlook to copy and paste google-services.json file you’ll get the following error whenever you construct your app.

Android Firebase AuthUpdated

7. Open the build.gradle (challenge degree) file of your android software and add classpath ‘’ in the dependencies part. Your build.gradle (challenge degree) file will look as proven under.

eight. Now add apply plugin: ‘’ at the bottom of your construct.gradle (App degree) file, slightly below dependencies part. Also examine whether or not you’ve added the next dependencies within the dependencies part of construct.gradle (App degree). Click on on sync now or re-build the android software.


9. The next section within the Firebase console exhibits the overview of your venture. Click on on Notifications button within the left menu to go to Notifications tab of your Firebase console. Before you ship your first notification, allow us to code our android app to deal with Firebase Notifications.

AndroidBash Firebase Push Notification

Part !!! : Consists of modifications to build.gradle file and different XML information

1. At this point of time, the build.gradle (Module:app) file ought to seem like under.

2. And in addition, the build.gradle (Module:venture) file ought to appear to be under.

2. To be able to use Firebase notification, open your AndroidManifest.xml and add the next underneath software tag.

three. Create colors.xml file in the values folder and add the following code. You possibly can change the colors to vary the look of the appliance.

four. The menu_main.xml file in the menu folder seems like this. Menu features a signal out button in it.

5. The types.xml file in the values folder appears like this.

6. The strings.xml file within the values folder appears like this. All the time maintain a habit of together with all of your xml string assets on this file.

Part !V : Consists of creation of latest XML format information and modification to activity_main.xml file

1. Create a brand new XML format useful resource file and identify it as activity_another.xml. It consists of an Imageview and a Textview in it. The activity_another.xml file of our software has the next code in it.

2. The acivity_main.xml consists of an ImageView and TextView. The acivity_main.xml has the following code in it.

Half V : Consists of creation of latest Java courses and modification to file

1. Create a new java class in your package deal and identify it as This can be a simple Activity class which gets opened when the consumer taps on a notification message which features a key named as “AnotherActivity”.

2. Now create one other new java class and identify it as It extends FirebaseInstanceIdService class and accommodates an overridden technique onTokenRefresh(). This technique known as if InstanceID token is up to date. This will likely happen if the safety of the earlier token had been compromised. Word that this is referred to as when the InstanceID token is initially generated so that is where you’d retrieve the token. This class has the following code in it.

3. Now create one other java class and identify it as It extends FirebaseMessagingService. It accommodates an overridden technique onMessageReceived(RemoteMessage remoteMessage). If you would like foregrounded (nonetheless operating) apps to obtain notification messages or knowledge messages, you’ll want to put in writing code to handle the onMessageReceived callback. The tactic onMessageReceived(…) won’t get referred to as if the app is in background or killed state, when the message is shipped by way of Firebase Console. However when the message is shipped by way of API (perform a POST to the next URL: ), the tactic onMessageReceived(…) will get invoked. We shall be displaying each the methods to ship notification. This class has the next code in it. Please undergo the comments written in the code.

four. Open and add the following code in it. It has a way named subscribeToPushService(). This technique is invoked every time the consumer opens your app. That is the place the consumer subscribes to notifications. MainActivity is the default activity which will get opened when the consumer taps on the acquired notification. But once the MainActivity is loaded we verify for a key named “AnotherActivity” in the intent knowledge that is passed to it. If the worth of the key equals “True” then we are redirecting the appliance to

5. Finally right here is how your AndroidManifest.xml ought to look.

That’s it. Run the appliance to subscribe to Firebase push notifications. Now you can begin sending notifications to all the subscribed users.

Right here is the way you send Push notifications together with image.

We will probably be displaying 2 methods to send notifications.

1. Utilizing Firebase console : Go to the overview part of your Firebase console and click on Notifications button on the left menu. Notification tab gets opened as proven under. Click on on Ship your First Message button. Enter the small print of your push message within the fields offered.

AndroidBash Firebase Push Notify

We have now included three key-values pairs [(image-> &#zero38; (AnotherActivity->True) &#zero38; (message-> “AndroidBash First Push message”)] in the message as proven in the image under.

AndroidBash Firebase Push Notify

Confirm the small print and click on ship button.

AndroidBash Firebase Push Notify

When despatched using Firebase console, if the app is in the foreground then onMessageReceived() technique shall be referred to as. Therefore a notification with image an fetched from the url is displayed to the consumer (As proven in the End result 1 ). But when the app is background or killed state then onMessageReceived() technique won’t be referred to as. Hence a easy notification containing only a message can be displayed within the notification bar of your android system. (As shown within the Outcome 2).

Outcome 1

PushNotify Result 1

End result 2

PushNotify Result 2

2. Using Messaging API : To ship a message utilizing API, you should use a device referred to as AdvancedREST Shopper, its a chrome extension, and send a message with the following parameters.

Authorization key could be obtained by visiting Google developers console and click on Credentials button on the left menu on your challenge. Among the many API keys listed, the server key shall be your authorization key.

And you have to put tokenID of the receiver within the “to” part of your POST request sent using API. You will get the tokenID if you run your android app. We log the tokenID in the subscribeToPushService() technique of the MainActivity.

AndroidBash Firebase Push Notify

When the notification is shipped using API, onMessageReceived() technique might be referred to as whether or not or not the appliance is in foreground. The following image exhibits the result of the notification despatched utilizing API.

AndroidBash PushNotify Result 3

You’ll be able to obtain the undertaking and add your google-services.json file (downloaded from Firebase console of your Firebase App) beneath app folder and check out it out.

Download Undertaking

Venture on Github

When you’ve got favored our tutorial, you possibly can help us maintain this website free for all Android fanatics either by buying our Android software from Google Play Retailer : Trailers Now Pro or by donating any small quantity on my PayPal account. When you’ve got followed our tutorial please do let us know by commenting under. It makes us write increasingly articles on android software improvement. In case when you get caught somewhere please do comment under and let us know. We often reply within a day.

Get 20% off all New HostGator Hosting plans with Coupon: SNAPPY.

Word : Don’t forget to :

Add google-services.json file beneath “app” folder.

Replace Google Play Providers and Google Repository to the newest variations obtainable.

Get SHA-1 finger print and paste it through the creation of your firebase undertaking.

Add all of the required dependencies in the construct.gradle file of your software.