Firebase Push Notification From Server Android Studio Example

Welcome dear developers to firebase push notification android studio example.

Earlier we were using google cloud messaging(GCM) for push notifications in android.

Firebase is the upgraded version of GCM.

Firebase push notification Android tutorial guide you to send push notification from server(PHP-MySQL.)

If you don’t want to send notification from server, then check Firebase Cloud Messaging Android.

If you want to send notification to multiple Android devices then check Android Firebase Push Notification to Multiple Devices.

First, check the output of Firebase push notification Android Studio example then we will develop it.

Download Source Code for Firebase push notification Android

Step 1: Create a new project in Android Studio.

Step 2: Developing app in Firebase Console

Watch a video for how to make app in firebase console website.

Describing Quick steps of above video

  1. Go to https://firebase.google.com/ and sign in to your google account.
  2. Click on Add Project and give project name and country.
  3. Click on CREATE PROJECT button.
  4. After project is created, click on Add firebase to your Android app button.
  5. Give your Android package name and click on Register App button.
  6. Click on Download google-services.json button.
  7. Move your downloaded JSON file into app folder of your android project as shown in the video.

Step 3: Updating build.gradle(Project: project_name) file:

we need to add following in dependencies{} structure

So final code for build.gradle(Project: project_name)  will look like this:

Step 4: Updating build.gradle(Module:app) file

Add following code into dependencies{} 

Add following at the end of the file

Final code for build.gradle(Module:app)

Step 5: Creating Service Classes

Creating GettingDeviceTokenService

Create a new JAVA class named GettingDeviceTokenService”  and add below source code

In this service, we will get device token.

Creating NotificationService

Create a new JAVA class named NotificationService” and add below source code

Description of NotificationService

remoteMessage.getData().toString() will give us JSON data as following

Following source code will parse above JSON data

Now following source will decide whether user has sent image with notification or not.

If image is not sent, then generateNotification() will be run.

Otherwise notificationWithImage() will be run.

getBitmapFromURL() method will give us bitmap of image(Popey Image.)

Now consider below source code

Above code will decide which activity to open when user clicks on Push Notification.

Here GotNotificationActivity will be open. So let’s create GotNotificationActivity.

Create a new activity named GotNotificationActivity and update activity_got_notification.xml as follow:

Developing Server Side(PHP) script to send firebase push notification

Create a new PHP file and name it sendSingleFCM.php

Add below source code into it

I have save above file here in mu server: https://demonuts.com/Demonuts/JsonTest/Tennis/sendSingleFCM.php

Upgrade above file where there is a code like

header(“Location: https://demonuts.com/Demonuts/JsonTest/Tennis/sendSingleFCM.php”)

with your server path or localhost path.

Testing your push notification without creating PHP Script

If you want to send push notification directly to your android device, then go to https://demonuts.com/Demonuts/JsonTest/Tennis/sendSingleFCM.php

Fill required details (Firebase Server Key, Device Token etc.) and click on the Go Go Go button.

Use comment section to give your review or to ask any question.

So all for firebase push notification Android Studio programmatically example tutorial. Thank you.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *