Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fursa.spacex.data.background
- import android.app.Notification
- import android.app.NotificationChannel
- import android.app.NotificationManager
- import android.app.PendingIntent
- import android.content.Context
- import android.content.ContextWrapper
- import android.content.Intent
- import android.os.Build
- import androidx.annotation.RequiresApi
- import com.fursa.spacex.R
- import com.fursa.spacex.presentation.MainActivity
- const val CHANNEL_ID = "com.fursa.spacex"
- const val CHANNEL_NAME = "SpaceXLaunchChannel"
- @RequiresApi(Build.VERSION_CODES.O)
- class NotificationHelper(context: Context) : ContextWrapper(context) {
- private val vibrationPattern by lazy {
- longArrayOf(100, 200, 300, 400, 500, 400, 500, 200, 500)
- }
- private val mainActivityIntent by lazy {
- Intent(this@NotificationHelper, MainActivity::class.java)
- }
- val notificationManager by lazy {
- context.getSystemService(Context.NOTIFICATION_SERVICE)
- as NotificationManager
- }
- init {
- val notificationChannel = NotificationChannel(
- CHANNEL_ID,
- CHANNEL_NAME,
- NotificationManager.IMPORTANCE_HIGH
- )
- notificationChannel.enableLights(true)
- notificationChannel.enableVibration(true)
- notificationChannel.vibrationPattern = vibrationPattern
- notificationManager.createNotificationChannel(notificationChannel)
- }
- companion object {
- fun getNotificationBuilder(title: String, content: String, context: Context): Notification.Builder {
- val mainActivityIntent = Intent(context, MainActivity::class.java)
- val pendingIntent = PendingIntent.getActivity(context, 0, mainActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT)
- return Notification.Builder(
- context.applicationContext, CHANNEL_ID
- ).setContentTitle(title)
- .setContentText(content)
- .setContentIntent(pendingIntent)
- .setSmallIcon(R.drawable.ic_upcoming_tab)
- .setAutoCancel(true)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment