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
- In Android Studio, go to Tools > Firebase.
- In the Firebase Assistant panel that appears on the right, find and click on Cloud Messaging.
- Click on Set up Firebase Cloud Messaging to start the setup.
Step 2: Connect Your App to Firebase
- In the Firebase Assistant, click on Connect to Firebase.
- Select your Firebase project or create a new one if you haven’t already.
- Follow the prompts to connect your Android app to Firebase.
Step 3: Add FCM Dependencies
- In the Firebase Assistant, click Add FCM to your app.
- This step will automatically add the required dependencies to your app’s
build.gradle
files. It will also include thegoogle-services
plugin and thefirebase-messaging
library. - 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.
- 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
}
}
- 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
- To retrieve the token, go to your
MainActivity
(or any other class) and add this code inonCreate
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
- Run your app on a physical device or an emulator with Google Play services.
- Open Logcat in Android Studio and filter by “FCM” to see the generated token.
- You should see a log message showing the FCM token.
Step 7: (Optional) Send a Test Notification from Firebase Console
- Go to the Firebase Console > Cloud Messaging.
- Click Send your first message and enter a title and message.
- Under Target, select your app.
- Click Test on device, and paste in the FCM token to target your device specifically.
- Send the message, and you should receive it on your Android device.