16
16
17
17
package com.example.platform.ui.live_updates
18
18
19
- import android.app.Notification
20
- import android.app.Notification.ProgressStyle
21
19
import android.app.NotificationChannel
22
20
import android.app.NotificationManager
23
21
import android.app.NotificationManager.IMPORTANCE_DEFAULT
24
22
import android.content.Context
23
+ import androidx.core.app.NotificationCompat.ProgressStyle
25
24
import android.graphics.Color
26
25
import android.os.Build
27
- import android.os.Bundle
28
26
import android.os.Handler
29
27
import android.os.Looper
30
28
import androidx.annotation.RequiresApi
29
+ import androidx.core.app.NotificationCompat
31
30
import androidx.core.graphics.drawable.IconCompat
31
+ import java.util.logging.Level
32
+ import java.util.logging.Logger
32
33
33
34
object SnackbarNotificationManager {
34
35
private lateinit var notificationManager: NotificationManager
@@ -49,7 +50,7 @@ object SnackbarNotificationManager {
49
50
private enum class OrderState (val delay : Long ) {
50
51
INITIALIZING (5000 ) {
51
52
@RequiresApi(Build .VERSION_CODES .BAKLAVA )
52
- override fun buildNotification (): Notification .Builder {
53
+ override fun buildNotification (): NotificationCompat .Builder {
53
54
return buildBaseNotification(appContext, INITIALIZING )
54
55
.setSmallIcon(R .drawable.ic_launcher_foreground)
55
56
.setContentTitle(" You order is being placed" )
@@ -59,7 +60,7 @@ object SnackbarNotificationManager {
59
60
},
60
61
FOOD_PREPARATION (9000 ) {
61
62
@RequiresApi(Build .VERSION_CODES .BAKLAVA )
62
- override fun buildNotification (): Notification .Builder {
63
+ override fun buildNotification (): NotificationCompat .Builder {
63
64
return buildBaseNotification(appContext, FOOD_PREPARATION )
64
65
.setContentTitle(" Your order is being prepared" )
65
66
.setContentText(" Next step will be delivery" )
@@ -73,7 +74,7 @@ object SnackbarNotificationManager {
73
74
},
74
75
FOOD_ENROUTE (13000 ) {
75
76
@RequiresApi(Build .VERSION_CODES .BAKLAVA )
76
- override fun buildNotification (): Notification .Builder {
77
+ override fun buildNotification (): NotificationCompat .Builder {
77
78
return buildBaseNotification(appContext, FOOD_ENROUTE )
78
79
.setContentTitle(" Your order is on its way" )
79
80
.setContentText(" Enroute to destination" )
@@ -82,7 +83,7 @@ object SnackbarNotificationManager {
82
83
.setProgressTrackerIcon(
83
84
IconCompat .createWithResource(
84
85
appContext, R .drawable.shopping_bag
85
- ).toIcon(appContext)
86
+ )
86
87
)
87
88
.setProgress(50 )
88
89
)
@@ -95,7 +96,7 @@ object SnackbarNotificationManager {
95
96
},
96
97
FOOD_ARRIVING (18000 ) {
97
98
@RequiresApi(Build .VERSION_CODES .BAKLAVA )
98
- override fun buildNotification (): Notification .Builder {
99
+ override fun buildNotification (): NotificationCompat .Builder {
99
100
return buildBaseNotification(appContext, FOOD_ARRIVING )
100
101
.setContentTitle(" Your order is arriving and has been dropped off" )
101
102
.setContentText(" Enjoy & don't forget to refrigerate any perishable items." )
@@ -104,7 +105,7 @@ object SnackbarNotificationManager {
104
105
.setProgressTrackerIcon(
105
106
IconCompat .createWithResource(
106
107
appContext, R .drawable.delivery_truck
107
- ).toIcon(appContext)
108
+ )
108
109
)
109
110
.setProgress(75 )
110
111
)
@@ -117,7 +118,7 @@ object SnackbarNotificationManager {
117
118
},
118
119
ORDER_COMPLETE (21000 ) {
119
120
@RequiresApi(Build .VERSION_CODES .BAKLAVA )
120
- override fun buildNotification (): Notification .Builder {
121
+ override fun buildNotification (): NotificationCompat .Builder {
121
122
return buildBaseNotification(appContext, ORDER_COMPLETE )
122
123
.setContentTitle(" Your order is complete." )
123
124
.setContentText(" Thank you for using JetSnack for your snacking needs." )
@@ -126,7 +127,7 @@ object SnackbarNotificationManager {
126
127
.setProgressTrackerIcon(
127
128
IconCompat .createWithResource(
128
129
appContext, R .drawable.check_circle
129
- ).toIcon(appContext)
130
+ )
130
131
)
131
132
.setProgress(100 )
132
133
)
@@ -143,7 +144,7 @@ object SnackbarNotificationManager {
143
144
fun buildBaseProgressStyle (orderState : OrderState ): ProgressStyle {
144
145
val pointColor = Color .valueOf(236f , 183f , 255f , 1f ).toArgb()
145
146
val segmentColor = Color .valueOf(134f , 247f , 250f , 1f ).toArgb()
146
- var progressStyle = ProgressStyle ()
147
+ var progressStyle = NotificationCompat . ProgressStyle ()
147
148
.setProgressPoints(
148
149
listOf (
149
150
ProgressStyle .Point (25 ).setColor(pointColor),
@@ -188,14 +189,11 @@ object SnackbarNotificationManager {
188
189
}
189
190
190
191
@RequiresApi(Build .VERSION_CODES .O )
191
- fun buildBaseNotification (appContext : Context , orderState : OrderState ): Notification .Builder {
192
- val promotedExtras = Bundle ()
193
- promotedExtras.putBoolean(" android.requestPromotedOngoing" , true )
194
- val notificationBuilder = Notification .Builder (appContext, CHANNEL_ID )
192
+ fun buildBaseNotification (appContext : Context , orderState : OrderState ): NotificationCompat .Builder {
193
+ val notificationBuilder = NotificationCompat .Builder (appContext, CHANNEL_ID )
195
194
.setSmallIcon(R .drawable.ic_launcher_foreground)
196
195
.setOngoing(true )
197
- .setColorized(true )
198
- .addExtras(promotedExtras)
196
+ .setRequestPromotedOngoing(true )
199
197
200
198
when (orderState) {
201
199
INITIALIZING -> {}
@@ -204,27 +202,38 @@ object SnackbarNotificationManager {
204
202
FOOD_ARRIVING ->
205
203
notificationBuilder
206
204
.addAction(
207
- Notification .Action .Builder (null , " Got it" , null ).build()
205
+ NotificationCompat .Action .Builder (null , " Got it" , null ).build()
208
206
)
209
207
.addAction(
210
- Notification .Action .Builder (null , " Tip" , null ).build()
208
+ NotificationCompat .Action .Builder (null , " Tip" , null ).build()
211
209
)
212
210
ORDER_COMPLETE ->
213
211
notificationBuilder
214
212
.addAction(
215
- Notification .Action .Builder (
213
+ NotificationCompat .Action .Builder (
216
214
null , " Rate delivery" , null ).build()
217
215
)
218
216
}
219
217
return notificationBuilder
220
218
}
221
219
222
- abstract fun buildNotification (): Notification .Builder
220
+ abstract fun buildNotification (): NotificationCompat .Builder
223
221
}
224
222
223
+ @RequiresApi(Build .VERSION_CODES .BAKLAVA )
225
224
fun start () {
226
225
for (state in OrderState .entries) {
227
226
val notification = state.buildNotification().build()
227
+
228
+ Logger .getLogger(" canPostPromotedNotifications" )
229
+ .log(
230
+ Level .INFO ,
231
+ notificationManager.canPostPromotedNotifications().toString())
232
+ Logger .getLogger(" hasPromotableCharacteristics" )
233
+ .log(
234
+ Level .INFO ,
235
+ notification.hasPromotableCharacteristics().toString())
236
+
228
237
Handler (Looper .getMainLooper()).postDelayed({
229
238
notificationManager.notify(NOTIFICATION_ID , notification)
230
239
}, state.delay)
0 commit comments