Send notifications to Firebase topics easily with our step-by-step guide. Learn Firebase project setup, Android configuration, topic subscription, and notification testing.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set up Firebase Project
Step 2: Set Up an Android App in Firebase
AndroidManifest.xml file) and app nickname.google-services.json file and place it in your app's app/ directory.
Step 3: Configure Your Android Project
Open your build.gradle (Project level) and add the Firebase and Google services dependencies:
buildscript {
dependencies {
// Add this line
classpath 'com.google.gms:google-services:4.3.10'
}
}
Open your build.gradle (App level) and apply the Google services plugin:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
Add Firebase Cloud Messaging dependency:
dependencies {
// Add this line
implementation 'com.google.firebase:firebase-messaging:23.0.0'
}
Step 4: Subscribe to a Topic in Your Android App
In your app's main activity (or wherever appropriate), subscribe to a topic:
import com.google.firebase.messaging.FirebaseMessaging;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\_main);
// Subscribe to a Firebase topic
FirebaseMessaging.getInstance().subscribeToTopic("news")
.addOnCompleteListener(task -> {
String msg = "Subscription successful";
if (!task.isSuccessful()) {
msg = "Subscription failed";
}
Log.d("FirebaseTopic", msg);
});
}
}
Step 5: Send a Notification to a Topic from Firebase Console
Step 6: Handle Notifications in Your App
If your app is in the foreground, messages will not be displayed in the system tray, so you need to handle them manually. Extend FirebaseMessagingService:
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle FCM messages here.
if (remoteMessage.getNotification() != null) {
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
sendNotification(title, body);
}
}
private void sendNotification(String title, String messageBody) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL\_ID)
.setContentTitle(title)
.setContentText(messageBody)
.setPriority(NotificationCompat.PRIORITY\_HIGH)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic\_notification);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION\_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Step 7: Test Your Setup
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.