Skip to content
This repository was archived by the owner on Dec 16, 2023. It is now read-only.

Commit 52d4698

Browse files
committed
Update notifications
1 parent 1301bc2 commit 52d4698

3 files changed

Lines changed: 29 additions & 39 deletions

File tree

app/src/main/java/com/marknkamau/justjava/data/network/MyFirebaseMessagingService.kt

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,24 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
2828
remoteMessage.notification?.let {
2929
notificationHelper.showNotification(it.title ?: "JustJava", it.body ?: "")
3030
}
31+
3132
remoteMessage.data?.let {
3233
Timber.d(it.toString())
33-
if (it["reason"] == "completed-order") {
34-
notificationHelper.showCompletedOrderNotification("Your order has been completed.")
35-
}
36-
37-
when (it["reason"]) {
34+
when(it["reason"]){
3835
"completed-order" -> {
39-
notificationHelper.showCompletedOrderNotification("Your order has been completed.")
36+
notificationHelper.showCompletedOrderNotification()
4037
}
4138
"mpesa" -> {
42-
it["body"]?.let { body ->
43-
notificationHelper.showNotification("Payment", body)
44-
}
45-
it["status"]?.let { status ->
46-
it["orderId"]?.let { orderId ->
47-
if (status == "completed") {
48-
val intent = Intent(MPESA_ORDER_PAID_ACTION)
49-
intent.putExtra(ORDER_ID, orderId)
50-
broadcastManager.sendBroadcast(intent)
51-
}
52-
}
39+
// Show notification
40+
notificationHelper.showPaymentNotification(it["body"]!!)
41+
42+
// Send local broadcast so that if the user is on the order's page, the status updates
43+
if (it["status"] == "completed") {
44+
val intent = Intent(MPESA_ORDER_PAID_ACTION)
45+
intent.putExtra(ORDER_ID, it["orderId"])
46+
broadcastManager.sendBroadcast(intent)
5347
}
5448
}
55-
else -> {
56-
}
5749
}
5850
}
5951
}

app/src/main/java/com/marknkamau/justjava/utils/NotificationHelper.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package com.marknkamau.justjava.utils
33
import android.app.NotificationChannel
44
import android.app.NotificationManager
55
import android.content.Context
6-
import android.graphics.Color
76
import android.os.Build
87
import androidx.annotation.RequiresApi
98
import androidx.core.app.NotificationCompat
109
import androidx.core.content.ContextCompat
1110
import 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

1919
class 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
}

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
<item name="masked" type="id" />
7474

7575
<!--Notifications-->
76-
<string name="default_notification_channel">defaultNotificationChannel</string>
7776
<string name="drink_image_content_desc">Drink Image</string>
7877
<string name="visibility_icon_content_desc">Visibility icon</string>
7978
<string name="app_icon_content_desc">App icon</string>

0 commit comments

Comments
 (0)