Firebase Push Notification

Firebase Push Notification Android Tutorial

Firebase Push Notification – The 2016 Google I/O released major improvements with their amazing product – Firebase – a cloud program with a whole lot of amazing features for mobile software developers. One of these is Firebase Cloud Messaging (FCM) ,a cross-platform messaging solution that let us users reliably deliver communications free. Yes, FCM is a free of charge service from Google. Contrasting to the sooner Google Cloud Messaging (GCM), FCM is a lot more developer-friendly because you do not even need to see the server code engaged.

firebase push notification

Its  tutorial about sending push notifications to Google android through Firebase, predicated on the era of Firebase this season (2017). This article shows how to create the skeleton for sending and receiving push notifications via FCM with instructions on server code. If You Doesn’t Know About Firebase Cloud Messaging, Then You have To learn it first.

After reading this tutorial, I am hoping you will be able to make use of it in your brand-new Android software or add it to your existing software for sending out firebase push notification. Let’s start!

Get started

  • Put in a new project or to Firebase.
  • If you opt to create a fresh project, you will need to create the project name and country. For instance, I am going to call my task FirebaseDemo and country Pakistan as shown in image below.

firebase push notification

  • Then go for “Add Firebase to your Google android app”.

firebase push notification

  • Enter a package name for your app. I only place my offer name and omit the SHA-1 because I avoid Firebase for my app’s authentication.

firebase push notification

  • Click on the ADD APP button here to download google-services.json. That is an important document and you’ll need to place it into the app.

Add google-services.json to your app folder

Replace the google-services.json in your app folder. The Google services plugin for Gradle will insert the google-services.json data file you merely downloaded.

Configure gradle files

Open Android Studio room and adjust your build.gradle documents to make use of the Google services plugin.

Update the project-level build.gradle (the one in your project folder)

 Add the following code to the build.gradle file:

buildscript {
  dependencies {
    classpath 'com.google.gms:google-services:3.0.0' // Add this line
  }
}

 

Update the app-level build.gradle (the one in your project/your app-module)

(a) Add this code to the bottom of the build.gradle file

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

 

(b) Add Firebase related dependencies

And Firebase related dependencies under dependencies in the same build.gradle file.


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


dependencies {
    compile 'com.google.firebase:firebase-core:9.2.0'                        // this line must be included to integrate with Firebase
    compile 'com.google.firebase:firebase-messaging:9.2.0'                   // this line must be included to use FCM
}

 

(c) Revise services using com.google.android.gms:play-services

In the event that you add Firebase into a preexisting task which uses any function of gms:play-services, such as gps navigation location, you have to revise their editions, too. Upon writing this guide, 9.2.0 is effective. In the event that you get compilation problems, you will need to check on find out the right version number.

compile 'com.google.android.gms:play-services-location:9.2.0'  
   compile 'com.google.android.gms:play-services-places:9.2.0'

 

(d) Add the applicationId to the defaultConfig section

android {

    defaultConfig {
        applicationId "com.example.my.app"  // this is the id that your app has
    }
}

 

Add services to your app

Two services should be put into use Firebase Cloud Messaging service: a simple code for evaluating if firebase push notification works, and other rules to handle obtaining message or mailing message in your app matching to your design.

(1) Put in a service that stretches FirebaseMessagingService

To have the ability to obtain any notification in your app, you should put in a service which expands FirebaseMessagingService such as this:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
 private static final String TAG = "FCM Service";
 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

Then add it into the AndroidManifest.xml file.

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

 (2) Add a service that extends FirebaseInstanceIdService

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

Add it into the AndroidManifest.xml file, this makes sure that the service is loaded

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

 

Ensure that you send your first firebase push notification!

To see if the installation works, operate a test by mailing a test meaning to your own mobile.

firebase push notification

  • Jot down your note and choose an app. Click “SEND MEssage”.

firebase push notification

You now should get a firebase push notification on your Android os mobile. Should your application is running on the backdrop, you’ll get it on the mobile’s notification middle; otherwise you can view it in your Android os Screen log (we must put a code to log inbound messages)

If the installation is prosperous, you should get a notification on your mobile. Sometimes, normally it takes a short while for the concept to send and appear, so you need to be patient for a time.

See Also:

Firebase Cloud Messaging Tutorial Android

Firebase Loign And Registration Authentication

Firebase Github Authentication

 


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed