Guest User

Untitled

a guest
Nov 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.11 KB | None | 0 0
  1. package com.fursa.spacex.data.background
  2.  
  3. import android.app.Notification
  4. import android.app.NotificationChannel
  5. import android.app.NotificationManager
  6. import android.app.PendingIntent
  7. import android.content.Context
  8. import android.content.ContextWrapper
  9. import android.content.Intent
  10. import android.os.Build
  11. import androidx.annotation.RequiresApi
  12. import com.fursa.spacex.R
  13. import com.fursa.spacex.presentation.MainActivity
  14.  
  15. const val CHANNEL_ID = "com.fursa.spacex"
  16. const val CHANNEL_NAME = "SpaceXLaunchChannel"
  17.  
  18.  
  19. @RequiresApi(Build.VERSION_CODES.O)
  20. class NotificationHelper(context: Context) : ContextWrapper(context) {
  21.  
  22.     private val vibrationPattern by lazy {
  23.         longArrayOf(100, 200, 300, 400, 500, 400, 500, 200, 500)
  24.     }
  25.  
  26.     private val mainActivityIntent by lazy {
  27.         Intent(this@NotificationHelper, MainActivity::class.java)
  28.     }
  29.  
  30.  
  31.     val notificationManager by lazy {
  32.         context.getSystemService(Context.NOTIFICATION_SERVICE)
  33.                 as NotificationManager
  34.     }
  35.  
  36.     init {
  37.         val notificationChannel = NotificationChannel(
  38.             CHANNEL_ID,
  39.             CHANNEL_NAME,
  40.             NotificationManager.IMPORTANCE_HIGH
  41.         )
  42.  
  43.         notificationChannel.enableLights(true)
  44.         notificationChannel.enableVibration(true)
  45.         notificationChannel.vibrationPattern = vibrationPattern
  46.         notificationManager.createNotificationChannel(notificationChannel)
  47.     }
  48.  
  49.     companion object {
  50.         fun getNotificationBuilder(title: String, content: String, context: Context): Notification.Builder {
  51.             val mainActivityIntent = Intent(context, MainActivity::class.java)
  52.             val pendingIntent = PendingIntent.getActivity(context, 0, mainActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
  53.  
  54.             return Notification.Builder(
  55.                 context.applicationContext, CHANNEL_ID
  56.             ).setContentTitle(title)
  57.                 .setContentText(content)
  58.                 .setContentIntent(pendingIntent)
  59.                 .setSmallIcon(R.drawable.ic_upcoming_tab)
  60.                 .setAutoCancel(true)
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment