Sun. Dec 22nd, 2024

To set up Firebase Cloud Messaging (FCM) and generate a messaging token using Android Studio’s Firebase Assistant, follow these steps:

Step 1: Open Firebase Assistant in Android Studio

  1. In Android Studio, go to Tools > Firebase.
  2. In the Firebase Assistant panel that appears on the right, find and click on Cloud Messaging.
  3. Click on Set up Firebase Cloud Messaging to start the setup.

Step 2: Connect Your App to Firebase

  1. In the Firebase Assistant, click on Connect to Firebase.
  2. Select your Firebase project or create a new one if you haven’t already.
  3. Follow the prompts to connect your Android app to Firebase.

Step 3: Add FCM Dependencies

  1. In the Firebase Assistant, click Add FCM to your app.
  2. This step will automatically add the required dependencies to your app’s build.gradle files. It will also include the google-services plugin and the firebase-messaging library.
  3. After the assistant adds the dependencies, sync your project with Gradle to ensure everything is up-to-date.

Step 4: Set Up a Firebase Messaging Service

Create a custom FirebaseMessagingService to handle token generation and incoming messages.

  1. Create a new Kotlin class named MyFirebaseMessagingService:
   import android.util.Log
   import com.google.firebase.messaging.FirebaseMessagingService
   import com.google.firebase.messaging.RemoteMessage

   class MyFirebaseMessagingService : FirebaseMessagingService() {

       override fun onNewToken(token: String) {
           super.onNewToken(token)
           Log.d("FCM", "New token: $token")
           // Send the token to your server if needed
       }

       override fun onMessageReceived(remoteMessage: RemoteMessage) {
           super.onMessageReceived(remoteMessage)
           Log.d("FCM", "Message received: ${remoteMessage.data}")
           // Handle the message here if necessary
       }
   }
  1. Register this service in your AndroidManifest.xml:
   <service
       android:name=".MyFirebaseMessagingService"
       android:exported="true">
       <intent-filter>
           <action android:name="com.google.firebase.MESSAGING_EVENT"/>
       </intent-filter>
   </service>

Step 5: Generate and Retrieve the FCM Token

  1. To retrieve the token, go to your MainActivity (or any other class) and add this code in onCreate to request the FCM token:
   import android.os.Bundle
   import android.util.Log
   import androidx.appcompat.app.AppCompatActivity
   import com.google.firebase.messaging.FirebaseMessaging

   class MainActivity : AppCompatActivity() {

       override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)

           // Generate and retrieve FCM token
           FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
               if (!task.isSuccessful) {
                   Log.w("FCM", "Fetching FCM registration token failed", task.exception)
                   return@addOnCompleteListener
               }

               // Get new FCM token
               val token = task.result
               Log.d("FCM", "FCM token: $token")

               // Send token to your server if needed
           }
       }
   }

Step 6: Test the FCM Token Generation

  1. Run your app on a physical device or an emulator with Google Play services.
  2. Open Logcat in Android Studio and filter by “FCM” to see the generated token.
  3. You should see a log message showing the FCM token.

Step 7: (Optional) Send a Test Notification from Firebase Console

  1. Go to the Firebase Console > Cloud Messaging.
  2. Click Send your first message and enter a title and message.
  3. Under Target, select your app.
  4. Click Test on device, and paste in the FCM token to target your device specifically.
  5. Send the message, and you should receive it on your Android device.

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

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