Created
August 8, 2019 10:31
-
-
Save arindamxd/baf3992a0283a207848399ede4026ec1 to your computer and use it in GitHub Desktop.
Post Notification
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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