Add Firebase Notifications in Android App

Hello Guys, Today We are going to develop an App with Firebase Cloud Messaging. I’m gonna Explain How To Add Firebase Notifications in Android App.

We don’t write whole code for Messaging. We achieve this with a single line of code that shows Notifications to Our App.

So Let’s Start.

Open Android Studio and Create a Project.

After Creating Project We add this project to firebase console.

Copy the Package name and Open Firebase Console.

Now Add your Package name in Firebase.

Download google-services.json file and copy it to app in the Android Studio.

Now Add Firebase SDK to your App.

dependencies {
        
        classpath 'com.google.gms:google-services:4.3.8'
       
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
dependencies {

  
    implementation platform('com.google.firebase:firebase-bom:28.2.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-messaging'

}

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

Also Add Cloud Messaging SDK to your App.

Now We Create an Application Class for our App.

As I said Now we are going to write the single line for Showing Notifications using a method called Topics.

We give a name to our Topic and Use that topic in Firebase to send Notifications.

I have created a topic with the name sample_notification. Users who install my App will automatically subscribe to this topic.

MyApplication.java

import android.app.Application;

import com.google.firebase.messaging.FirebaseMessaging;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseMessaging.getInstance().subscribeToTopic("sample_notification");
      

    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firebasenotificationssample">

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.FirebaseNotificationsSample">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>

</manifest>

Now Let’s Test this.

Go to Firebase Console and Open Cloud Messaging.

In Target Section select Topic.

Enter the Topic we created in Android studio.

Now We successfully sent a Notification to our App using Topic.

You Can Integrate this in any of your App.

Check Out Our Android Tutorials Here

Leave a Reply

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

Back to top button
close