Skip to content

Instantly share code, notes, and snippets.

@arindamxd
Created August 8, 2019 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arindamxd/baf3992a0283a207848399ede4026ec1 to your computer and use it in GitHub Desktop.
Save arindamxd/baf3992a0283a207848399ede4026ec1 to your computer and use it in GitHub Desktop.
Post Notification
/**
* Create a Notification that is shown as a heads-up notification if possible.
*
* For this project, this is used to show a notification so that you know when different steps
* of the background work chain are starting
*
* @param message Message shown on the notification
* @param context Context needed to create Toast
*
* Created by Arindam Karmakar on 16/5/19.
*/
internal fun makeStatusNotification(message: String, context: Context) {
// Make a channel if necessary
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
val name = VERBOSE_NOTIFICATION_CHANNEL_NAME
val description = VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(CHANNEL_ID, name, importance)
channel.description = description
// Add the channel
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
notificationManager?.createNotificationChannel(channel)
}
// Create the notification
val builder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(LongArray(0))
// Show the notification
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment