1/ First Create an Empty Project: Firebase Push Notification
2/ Open Firebase window from Tool Window: Tools -> Firebase
3/ From the Firebase window click the cloud messaging option. You can see two options click on the set up the firebase cloud messaging [ Java ]
After clicking on the link you will see this page
4/ Click on Connect Firebase
when you click on the Connect Firebase link you will be redirected to your default browser in the Firebase console
4.1/ Click on Create a project then Firebase will auto-suggest a name for you and then Click on Continue
4.2/ You will see another window click on Continue
4.3/In The next window you need to select Default account for Firebase and click on Create Project
4.4/ After that your project will create and wait for some time after completing the project click on Continue
4.5/ Then you will be redirected to your project and you will see another window for connecting to your app with Firebase Click on Connect
4.6/ Now you will see the message that the Firebase is connected with the Android app
Close this window and again open your project.
5/ Then add FCM to your app and click on accept changes
6/ Create a new Java class: FirebasePushNotificationServices
package com.example.fireabasepushnotification;
import android.content.pm.PackageManager;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FirebasePushNotificationServices extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage message) {
super.onMessageReceived(message);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "fcm_notification")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(message.getNotification().getTitle())
.setContentText(message.getNotification().getBody())
.setAutoCancel(true);
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
notificationManagerCompat.notify(111, builder.build());
}
}
7/ Modify Your AndroidManifest.xml
Add Internet and post notification permission and register the Service (FirebasePushNotificationServices)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FirebasePushNotification"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".FirebasePushNotificationServices"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
8/ Now you can send notifications from firebase to your app
8.1/ First open your Firebase project and click on Messaging form Run option
Then click on Create your first campaign
8.2/ Then Select Firebase Notification Message
8.3/ Create a notification
1. Set Notification Title
2. Set Notification Text
3. Set Notification Image (this is optional )
after that click on Next
8.4 / Select Target: From the Target option select your app from the dropdown
And then Click on Review then click on Publish
Run your app
** Note Your phone or emulator must have permission for notifications for your apps
you can check this -> open app info and here you can see if permission is denied then give the permission