This service listens for push notifications and shows a simple notification with title and message.
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
}
}