Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. /**
  2. * Create a Notification that is shown as a heads-up notification if possible.
  3. *
  4. * For this project, this is used to show a notification so that you know when different steps
  5. * of the background work chain are starting
  6. *
  7. * @param message Message shown on the notification
  8. * @param context Context needed to create Toast
  9. *
  10. * Created by Arindam Karmakar on 16/5/19.
  11. */
  12.  
  13. internal fun makeStatusNotification(message: String, context: Context) {
  14.  
  15. // Make a channel if necessary
  16. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  17. // Create the NotificationChannel, but only on API 26+ because
  18. // the NotificationChannel class is new and not in the support library
  19. val name = VERBOSE_NOTIFICATION_CHANNEL_NAME
  20. val description = VERBOSE_NOTIFICATION_CHANNEL_DESCRIPTION
  21. val importance = NotificationManager.IMPORTANCE_HIGH
  22. val channel = NotificationChannel(CHANNEL_ID, name, importance)
  23. channel.description = description
  24.  
  25. // Add the channel
  26. val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
  27.  
  28. notificationManager?.createNotificationChannel(channel)
  29. }
  30.  
  31. // Create the notification
  32. val builder = NotificationCompat.Builder(context, CHANNEL_ID)
  33. .setSmallIcon(R.drawable.ic_launcher_foreground)
  34. .setContentTitle(NOTIFICATION_TITLE)
  35. .setContentText(message)
  36. .setPriority(NotificationCompat.PRIORITY_HIGH)
  37. .setVibrate(LongArray(0))
  38.  
  39. // Show the notification
  40. NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, builder.build())
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement