Skip to content

Instantly share code, notes, and snippets.

@ngangavic
Created December 2, 2021 16:35
Show Gist options
  • Save ngangavic/68646a44c1b33ee6d0addc65ad53600f to your computer and use it in GitHub Desktop.
Save ngangavic/68646a44c1b33ee6d0addc65ad53600f to your computer and use it in GitHub Desktop.
private fun sendNotification(messageBody: String) {
val channelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(getString(R.string.fcm_message))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Water Drop",
NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
notificationBuilder.build().flags.and(Notification.FLAG_AUTO_CANCEL)
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment