/firebase-tutorials

How to send notification to a topic in Firebase?

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

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to send notification to a topic in Firebase?

 

Step 1: Set up Firebase Project

 

  • Go to the Firebase Console.
  • Click on "Add project" to create a new Firebase project.
  • Enter the project name and click "Continue."
  • Follow the prompts to set up Google Analytics if needed, and then click "Create Project."
  • Once the project is created, click "Continue" to navigate to the Firebase dashboard.

 

Step 2: Set Up an Android App in Firebase

 

  • In your Firebase project overview, click on the Android icon to add a new app.
  • Enter your Android package name (usually found in your AndroidManifest.xml file) and app nickname.
  • Click "Register app."
  • Download the google-services.json file and place it in your app's app/ directory.

 

Step 3: Configure Your Android Project

 

  1. 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'
        }
    }
    
  2. Open your build.gradle (App level) and apply the Google services plugin:

    
    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services' 
    
  3. 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

 

  • Go to the Firebase Console.
  • In the left sidebar, click on “Cloud Messaging.”
  • Click on “Send your first message.”
  • Enter the notification text and other details.
  • Click on the “Send to topic” option and enter the name of your topic (e.g., "news").
  • Click “Next”, review your message, and finally click “Publish.”

 

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

 

  • Run your app on a physical device or emulator.
  • Make sure the device/emulator has internet access and check the logcat for the subscription success message.
  • Use the Firebase console to send a test notification to your topic.
  • Verify the notification is received and handled in your app.

 

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022