0
0
Android Kotlinmobile~10 mins

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

Choose your learning style9 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