0
0
Android Kotlinmobile~5 mins

Cloud Messaging (push notifications) in Android Kotlin

Choose your learning style9 modes available
Introduction

Push notifications let apps send messages to users even when the app is closed. This helps keep users informed and engaged.

To alert users about new messages or updates in a chat app.
To remind users about upcoming events or appointments.
To send promotional offers or news from a shopping app.
To notify users about important system alerts or security issues.
Syntax
Android Kotlin
class MyFirebaseMessagingService : FirebaseMessagingService() {
  override fun onMessageReceived(remoteMessage: RemoteMessage) {
    // Handle the received message here
  }

  override fun onNewToken(token: String) {
    // Send the new token to your server
  }
}
Extend FirebaseMessagingService to handle incoming messages.
Override onMessageReceived to react when a push notification arrives.
Examples
This example gets the notification text and shows it to the user.
Android Kotlin
override fun onMessageReceived(remoteMessage: RemoteMessage) {
  val message = remoteMessage.notification?.body
  showNotification(message ?: "You have a new message")
}
When the device gets a new token, send it to your server to keep notifications working.
Android Kotlin
override fun onNewToken(token: String) {
  sendTokenToServer(token)
}
Sample App

This service listens for push notifications and shows a simple notification with title and message.

Android Kotlin
class MyFirebaseMessagingService : FirebaseMessagingService() {
  override fun onMessageReceived(remoteMessage: RemoteMessage) {
    val notification = remoteMessage.notification
    val title = notification?.title ?: "App Notification"
    val message = notification?.body ?: "You have a new message"

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channelId = "default_channel"

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      val channel = NotificationChannel(channelId, "Default Channel", NotificationManager.IMPORTANCE_DEFAULT)
      notificationManager.createNotificationChannel(channel)
    }

    val builder = NotificationCompat.Builder(this, channelId)
      .setContentTitle(title)
      .setContentText(message)
      .setSmallIcon(android.R.drawable.ic_dialog_info)
      .setAutoCancel(true)

    notificationManager.notify(0, builder.build())
  }

  override fun onNewToken(token: String) {
    // Normally send token to your server here
  }
}
OutputSuccess
Important Notes

Make sure to add the service in your AndroidManifest.xml with the proper intent filter.

Push notifications require internet permission and Firebase setup in your project.

Test notifications using Firebase Console or your backend server.

Summary

Push notifications keep users updated even when the app is closed.

Use FirebaseMessagingService to receive and handle messages.

Always send the device token to your server to target notifications correctly.