@@ -3,12 +3,12 @@ package com.marknkamau.justjava.utils
33import android.app.NotificationChannel
44import android.app.NotificationManager
55import android.content.Context
6- import android.graphics.Color
76import android.os.Build
87import androidx.annotation.RequiresApi
98import androidx.core.app.NotificationCompat
109import androidx.core.content.ContextCompat
1110import com.marknkamau.justjava.R
11+ import kotlin.random.Random
1212
1313/* *
1414 * Created by Mark Njung'e.
@@ -18,50 +18,49 @@ import com.marknkamau.justjava.R
1818
1919class NotificationHelper (private val context : Context ) {
2020
21+ private val defaultChannelId = " defaultNotificationChannel"
22+ private val ordersChannelId = " ordersNotificationChannel"
23+ private val paymentsChannelId = " ordersNotificationChannel"
24+
2125 private val notificationManager by lazy {
2226 context.getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
2327 }
2428
2529 init {
2630 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
27- createDefaultChannel()
31+ createChannel(defaultChannelId, " Default channel" , " Notifications" )
32+ createChannel(ordersChannelId, " Orders channel" , " Notifications for orders" )
33+ createChannel(paymentsChannelId, " Payments channel" , " Notifications for payments" )
2834 }
2935 }
3036
3137 @RequiresApi(Build .VERSION_CODES .O )
32- private fun createDefaultChannel () {
33- val id = context.getString(R .string.default_notification_channel)
34- val channel = NotificationChannel (id, " Default notification channel" , NotificationManager .IMPORTANCE_DEFAULT )
38+ private fun createChannel (id : String , name : String , description : String ) {
39+ val channel = NotificationChannel (id, name, NotificationManager .IMPORTANCE_DEFAULT )
3540
36- channel.description = " Default notifications "
41+ channel.description = description
3742 channel.enableLights(true )
38- channel.lightColor = Color .RED
3943 channel.enableVibration(true )
4044
4145 notificationManager.createNotificationChannel(channel)
4246 }
4347
44- fun showCompletedOrderNotification (text : String ) {
45- val channelId = context.getString(R .string.default_notification_channel)
46- val notification = NotificationCompat .Builder (context, channelId)
47- .setSmallIcon(R .drawable.ic_just_java_logo_black)
48- .setContentTitle(" Completed order" )
49- .setContentText(text)
50- .setColor(ContextCompat .getColor(context, R .color.colorAccent))
51- .build()
48+ fun showCompletedOrderNotification () {
49+ showNotification(" Completed Order" , " Your order has been completed." , ordersChannelId)
50+ }
5251
53- notificationManager.notify(1 , notification)
52+ fun showPaymentNotification (body : String ) {
53+ showNotification(" Order Payment" , body, paymentsChannelId)
5454 }
5555
56- fun showNotification (title : String , body : String ) {
57- val channelId = context.getString(R .string.default_notification_channel)
56+ fun showNotification (title : String , body : String , channelId : String = defaultChannelId) {
5857 val notification = NotificationCompat .Builder (context, channelId)
5958 .setSmallIcon(R .drawable.ic_just_java_logo_black)
6059 .setContentTitle(title)
6160 .setContentText(body)
6261 .setColor(ContextCompat .getColor(context, R .color.colorAccent))
6362 .build()
6463
65- notificationManager.notify(2 , notification)
64+ notificationManager.notify(Random .nextInt() , notification)
6665 }
6766}
0 commit comments