Skip to content

Commit

Permalink
NotificationHelper : buildNotification()
Browse files Browse the repository at this point in the history
  • Loading branch information
LavishSwarnkar committed Mar 9, 2024
1 parent 06daaa2 commit e332529
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.streamliners.helpers

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
Expand Down Expand Up @@ -92,12 +93,31 @@ class NotificationHelper(

createNotificationChannel(channelId)

/* Post */
val id = Random().nextInt(30000) + 1000
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(
id,
buildNotification(title, body, channelId, pendingIntentActivity, modifyNotification, modifyIntent)
)
return id
}

@SuppressLint("MissingPermission")
fun buildNotification(
title: String,
body: String,
channelId: String = DEFAULT_CHANNEL,
pendingIntentActivity: Class<Activity>,
modifyNotification: NotificationCompat.Builder.() -> Unit = {},
modifyIntent: Intent.() -> Unit = {}
): Notification {
/* PendingIntent */
val intent = Intent(context, pendingIntentActivity).apply { modifyIntent() }
val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT xor PendingIntent.FLAG_IMMUTABLE)

/* Build */
val notification = NotificationCompat.Builder(context, channelId)
return NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
Expand All @@ -107,12 +127,6 @@ class NotificationHelper(
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.apply(modifyNotification)
.build()

/* Post */
val id = Random().nextInt(30000) + 1000
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(id, notification)
return id
}

fun hideNotification(
Expand Down

0 comments on commit e332529

Please sign in to comment.