3333
3434import com .google .firebase .messaging .FirebaseMessagingService ;
3535import com .google .firebase .messaging .RemoteMessage ;
36+ import com .simonesestito .shopsqueue .api .DateJsonAdapter ;
3637import com .simonesestito .shopsqueue .api .dto .Booking ;
3738import com .simonesestito .shopsqueue .api .dto .BookingWithCount ;
3839import com .simonesestito .shopsqueue .api .dto .FcmToken ;
40+ import com .simonesestito .shopsqueue .api .dto .ShoppingList ;
3941import com .simonesestito .shopsqueue .api .service .FcmService ;
4042import com .simonesestito .shopsqueue .ui .MainActivity ;
4143import com .squareup .moshi .Moshi ;
@@ -50,9 +52,13 @@ public class FcmReceiverService extends FirebaseMessagingService {
5052 private static final String KEY_MESSAGE_DATA = "data" ;
5153 private static final String MESSAGE_TYPE_QUEUE_NOTICE = "queue-notice" ;
5254 private static final String MESSAGE_TYPE_BOOKING_CANCELLED = "booking-cancelled" ;
55+ private static final String MESSAGE_TYPE_ORDER_READY = "order-ready" ;
56+ private static final String MESSAGE_TYPE_ORDER_CANCELLED = "order-cancelled" ;
5357 private static final String NOTIFICATION_CHANNEL_BOOKINGS_NOTICE_ID = "bookings-notice" ;
58+ private static final String NOTIFICATION_CHANNEL_ORDERS_NOTICE_ID = "orders-notice" ;
5459 @ Inject FcmService fcmService ;
5560 @ Inject SharedPreferencesStore sharedPreferencesStore ;
61+ @ Inject DateJsonAdapter dateJsonAdapter ;
5662
5763 @ Override
5864 public void onCreate () {
@@ -90,11 +96,17 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
9096 return ;
9197
9298 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
93- NotificationChannel channel = new NotificationChannel (
99+ NotificationChannel bookingsChannel = new NotificationChannel (
94100 NOTIFICATION_CHANNEL_BOOKINGS_NOTICE_ID ,
95101 getString (R .string .notification_channel_booking_notice ),
96102 NotificationManager .IMPORTANCE_HIGH );
97- notificationManager .createNotificationChannel (channel );
103+ notificationManager .createNotificationChannel (bookingsChannel );
104+
105+ NotificationChannel ordersChannel = new NotificationChannel (
106+ NOTIFICATION_CHANNEL_ORDERS_NOTICE_ID ,
107+ getString (R .string .notification_channel_orders_notice ),
108+ NotificationManager .IMPORTANCE_HIGH );
109+ notificationManager .createNotificationChannel (ordersChannel );
98110 }
99111
100112 switch (messageType != null ? messageType : "null" ) {
@@ -104,6 +116,12 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
104116 case MESSAGE_TYPE_BOOKING_CANCELLED :
105117 handleBookingCancellation (jsonData );
106118 break ;
119+ case MESSAGE_TYPE_ORDER_READY :
120+ handleOrderReady (jsonData );
121+ break ;
122+ case MESSAGE_TYPE_ORDER_CANCELLED :
123+ handleOrderCancellation (jsonData );
124+ break ;
107125 default :
108126 Log .e (TAG , "Received payload with unknown type: " + messageType );
109127 }
@@ -112,7 +130,7 @@ public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
112130 private void handleQueueNotice (String jsonString ) {
113131 BookingWithCount data = parseJson (jsonString , BookingWithCount .class );
114132 if (data == null ) {
115- Log .e (TAG , "Received payload with empty Booking data" );
133+ Log .e (TAG , "Received payload with invalid Booking data" );
116134 return ;
117135 }
118136
@@ -134,7 +152,7 @@ private void handleQueueNotice(String jsonString) {
134152 private void handleBookingCancellation (String jsonString ) {
135153 Booking data = parseJson (jsonString , Booking .class );
136154 if (data == null ) {
137- Log .e (TAG , "Received payload with empty Booking data" );
155+ Log .e (TAG , "Received payload with invalid Booking data" );
138156 return ;
139157 }
140158
@@ -145,13 +163,42 @@ private void handleBookingCancellation(String jsonString) {
145163 );
146164 }
147165
166+ private void handleOrderReady (String json ) {
167+ ShoppingList data = parseJson (json , ShoppingList .class );
168+ if (data == null ) {
169+ Log .e (TAG , "Received payload with invalid ShoppingList data" );
170+ return ;
171+ }
172+
173+ showNotification (
174+ data .getId () + 100_000 ,
175+ data .getShop ().getName (),
176+ getString (R .string .notification_order_ready_message )
177+ );
178+ }
179+
180+ private void handleOrderCancellation (String json ) {
181+ ShoppingList data = parseJson (json , ShoppingList .class );
182+ if (data == null ) {
183+ Log .e (TAG , "Received payload with invalid ShoppingList data" );
184+ return ;
185+ }
186+
187+ showNotification (
188+ data .getId () + 100_000 ,
189+ data .getShop ().getName (),
190+ getString (R .string .notification_order_cancelled_message )
191+ );
192+ }
193+
148194 @ Nullable
149195 private <T > T parseJson (String json , Class <T > clazz ) {
150196 if (json == null || json .isEmpty ())
151197 return null ;
152198
153199 try {
154200 return new Moshi .Builder ()
201+ .add (dateJsonAdapter )
155202 .build ()
156203 .adapter (clazz )
157204 .fromJson (json );
0 commit comments