How to send firebase push notifications to Android app

In this android programming source code example, we are going to send firebase push notifications to Android app

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to send firebase push notifications to Android app.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity11">

    <TextView
        android:id="@+id/fcmToken"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fcm token"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.bluapp.androidview2.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class DeviceFeaturesActivity11 extends AppCompatActivity {
    private TextView fcmToken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features11);
        fcmToken = (TextView) findViewById(R.id.fcmToken);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (task.isSuccessful()) {
                            String token = task.getResult().getToken();
                            fcmToken.setText("Fcm Token: "+token);
                            Log.d("AndroidView2", "Fcm Token: "+token);
                        }
                    }
                });

    }
}

MyFirebaseMessagingService11.java

import android.app.NotificationManager;
import android.content.Context;
import android.media.RingtoneManager;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.bluapp.androidview2.R;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService11 extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getData().size() > 0) {
            Log.d("AndroidView2", "Message data payload: " + remoteMessage.getData());
        }

        if (remoteMessage.getData() != null) {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "AndroidView2")
                    .setContentTitle(remoteMessage.getData().get("title"))
                    .setContentText(remoteMessage.getData().get("body"))
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setStyle(new NotificationCompat.BigTextStyle())
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
            Log.d("AndroidView2", "Message Notification Title: "+remoteMessage.getData().get("title")+" Message Notification Body: "+remoteMessage.getData().get("body"));
        }

    }
}

AndroidManifest.xml

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

build.gradle

implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-messaging:19.0.1'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.