Bird
Raised Fist0
Android Kotlinmobile~10 mins

Cloud Messaging (push notifications) in Android Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize Firebase Cloud Messaging in an Android app.

Android Kotlin
FirebaseMessaging.getInstance().[1]()
Drag options to blanks, or click blank then click option'
AsubscribeToTopic
BdeleteToken
CsendMessage
DgetToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribeToTopic instead of getToken
Trying to send a message from the client side
Calling deleteToken which removes the token
2fill in blank
medium

Complete the code to subscribe the device to a topic named "news" for receiving group notifications.

Android Kotlin
FirebaseMessaging.getInstance().[1]("news")
Drag options to blanks, or click blank then click option'
AgetToken
BunsubscribeFromTopic
CsubscribeToTopic
DsendMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using getToken instead of subscribeToTopic
Trying to unsubscribe instead of subscribe
Calling sendMessage which is not a client method
3fill in blank
hard

Fix the error in the code to handle incoming messages by overriding the correct method in FirebaseMessagingService.

Android Kotlin
override fun [1](remoteMessage: RemoteMessage) {
    // Handle message here
}
Drag options to blanks, or click blank then click option'
AonNewToken
BonMessageReceived
ConMessageSent
DonDeletedMessages
Attempts:
3 left
💡 Hint
Common Mistakes
Overriding onNewToken instead of onMessageReceived
Using onMessageSent which is for message sending confirmation
Using onDeletedMessages which handles deleted messages
4fill in blank
hard

Fill both blanks to create a notification channel for Android 8.0+ devices.

Android Kotlin
val channel = NotificationChannel("channel_id", "[1]", [2])
notificationManager.createNotificationChannel(channel)
Drag options to blanks, or click blank then click option'
AGeneral Notifications
BNotificationManager.IMPORTANCE_HIGH
CIMPORTANCE_LOW
DNews Alerts
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong importance constant like IMPORTANCE_LOW
Putting the channel ID instead of the channel name
Not creating the notification channel at all
5fill in blank
hard

Fill all three blanks to build and show a notification with a title and text.

Android Kotlin
val notification = NotificationCompat.Builder(this, "channel_id")
    .setContentTitle("[1]")
    .setContentText("[2]")
    .setSmallIcon([3])
    .build()
notificationManager.notify(1, notification)
Drag options to blanks, or click blank then click option'
ANew Message
BYou have a new notification
CR.drawable.ic_notification
DR.drawable.ic_launcher
Attempts:
3 left
💡 Hint
Common Mistakes
Using launcher icon instead of notification icon
Including quotes around title and text strings
Omitting the small icon which causes errors

Practice

(1/5)
1. What is the main purpose of Firebase Cloud Messaging (FCM) in Android apps?
easy
A. To manage app permissions and settings
B. To store user data locally on the device
C. To send push notifications to users even when the app is closed
D. To create user interfaces with buttons and text

Solution

  1. Step 1: Understand FCM's role

    Firebase Cloud Messaging is designed to send messages or notifications to users' devices.
  2. Step 2: Identify the correct purpose

    It works even if the app is closed, keeping users updated with push notifications.
  3. Final Answer:

    To send push notifications to users even when the app is closed -> Option C
  4. Quick Check:

    FCM purpose = push notifications [OK]
Hint: Remember: FCM = push notifications, not UI or storage [OK]
Common Mistakes:
  • Confusing FCM with local data storage
  • Thinking FCM manages UI elements
  • Assuming FCM controls app permissions
2. Which Kotlin class should you extend to handle incoming Firebase push messages in your Android app?
easy
A. FirebaseMessagingService
B. FirebaseDatabase
C. FirebaseAuth
D. FirebaseStorage

Solution

  1. Step 1: Identify the service for message handling

    FirebaseMessagingService is the class designed to receive and handle push notifications.
  2. Step 2: Exclude unrelated Firebase classes

    FirebaseDatabase, FirebaseAuth, and FirebaseStorage serve different purposes like data storage, authentication, and file storage.
  3. Final Answer:

    FirebaseMessagingService -> Option A
  4. Quick Check:

    Message handler class = FirebaseMessagingService [OK]
Hint: Use FirebaseMessagingService to receive messages [OK]
Common Mistakes:
  • Choosing FirebaseDatabase for message handling
  • Confusing FirebaseAuth with messaging
  • Using FirebaseStorage instead of messaging service
3. Given the following Kotlin code snippet inside a FirebaseMessagingService subclass, what will be logged when a message is received?
override fun onMessageReceived(remoteMessage: RemoteMessage) {
    val title = remoteMessage.notification?.title ?: "No Title"
    val body = remoteMessage.notification?.body ?: "No Body"
    Log.d("FCM", "Title: $title, Body: $body")
}
medium
A. Title: No Title, Body: No Body if notification is null
B. App crashes if notification is null
C. Title and Body are always empty strings
D. Logs only the body, not the title

Solution

  1. Step 1: Analyze null safety with Elvis operator

    The code uses ?: to provide default strings if notification or its fields are null.
  2. Step 2: Understand logging behavior

    If notification is null, title and body become "No Title" and "No Body" respectively, so the log shows these defaults.
  3. Final Answer:

    Title: No Title, Body: No Body if notification is null -> Option A
  4. Quick Check:

    Elvis operator handles nulls = default text logged [OK]
Hint: Elvis operator ?: gives default when null [OK]
Common Mistakes:
  • Assuming app crashes on null notification
  • Thinking title/body become empty strings
  • Believing only body is logged
4. You wrote this code to get the device token for FCM but it never logs the token. What is the error?
FirebaseMessaging.getInstance().token.addOnCompleteListener {
    if (!it.isSuccessful) return@addOnCompleteListener
    val token = it.result
    Log.d("FCM", "Token: $token")
}
medium
A. You need to call addOnSuccessListener instead of addOnCompleteListener
B. You forgot to add internet permission in AndroidManifest.xml
C. You must call getInstance() with context parameter
D. You should use getToken() instead of token property

Solution

  1. Step 1: Check the API for token retrieval

    FirebaseMessaging.getInstance().getToken() is the correct method; there is no .token property.
  2. Step 2: Verify other options

    Internet permission is needed to run, but the code won't compile due to undefined token property. getInstance() without context works, and addOnCompleteListener is correct.
  3. Final Answer:

    You should use getToken() instead of token property -> Option D
  4. Quick Check:

    No token property = use getToken() [OK]
Hint: Use getToken() method to fetch FCM token [OK]
Common Mistakes:
  • Forgetting internet permission
  • Thinking context is required for getInstance()
  • Using wrong listener type for token retrieval
5. You want to send a push notification only to devices that have a specific user preference enabled. Which step is essential to implement this targeting correctly?
hard
A. Use FirebaseDatabase to store notifications and query them on the device
B. Send the device token to your server and filter tokens based on user preferences
C. Send notifications to all devices and let the app ignore unwanted ones
D. Use FirebaseAuth to authenticate users before sending notifications

Solution

  1. Step 1: Understand device token role

    Device tokens uniquely identify devices to receive push notifications.
  2. Step 2: Implement server-side filtering

    Send tokens to your server and filter them by user preferences before sending notifications.
  3. Final Answer:

    Send the device token to your server and filter tokens based on user preferences -> Option B
  4. Quick Check:

    Token + server filtering = targeted notifications [OK]
Hint: Always send tokens to server for targeted notifications [OK]
Common Mistakes:
  • Sending notifications to all devices without filtering
  • Using FirebaseDatabase instead of server filtering
  • Confusing authentication with notification targeting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard