14
14
15
15
import java .text .SimpleDateFormat ;
16
16
import java .util .Date ;
17
- import java .util .HashMap ;
18
17
import java .util .List ;
19
18
import java .util .Locale ;
20
19
import java .util .Map ;
21
- import java .util .Objects ;
22
20
23
21
import euromsg .com .euromobileandroid .Constants ;
24
22
import euromsg .com .euromobileandroid .EuroMobileManager ;
@@ -360,7 +358,7 @@ public static void updatePayload(Context context, String pushId) {
360
358
Log .e (LOG_TAG , e .getMessage ());
361
359
}
362
360
}
363
- public static void readAllPushMessages (Context context , String pushId ) {
361
+ public static boolean readAllPushMessages (Context context ) {
364
362
try {
365
363
String jsonString = SharedPreference .getString (context , Constants .PAYLOAD_SP_KEY );
366
364
JSONObject jsonObject = new JSONObject (jsonString );
@@ -370,28 +368,17 @@ public static void readAllPushMessages(Context context, String pushId) {
370
368
if (payloadsArray != null ) {
371
369
for (int i = 0 ; i < payloadsArray .length (); i ++) {
372
370
JSONObject payloadObject = payloadsArray .getJSONObject (i );
373
- String existingPushId = payloadObject .optString ("pushId" , "" );
374
-
375
- if (pushId != null && !pushId .isEmpty ()) {
376
- if (existingPushId .equals (pushId )) {
377
- payloadObject .put ("status" , "O" );
378
- payloadObject .put ("openDate" , new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ()).format (new Date ()));
379
371
380
- SharedPreference .saveString (context , Constants .PAYLOAD_SP_KEY , jsonObject .toString ());
381
- return ;
382
- }
383
- } else {
384
372
payloadObject .put ("status" , "O" );
385
373
payloadObject .put ("openDate" , new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ()).format (new Date ()));
386
-
387
- SharedPreference .saveString (context , Constants .PAYLOAD_SP_KEY , jsonObject .toString ());
388
- return ;
389
- }
390
374
}
375
+ jsonObject .put (Constants .PAYLOAD_SP_ARRAY_KEY , payloadsArray );
376
+ SharedPreference .saveString (context , Constants .PAYLOAD_SP_KEY , jsonObject .toString ());
377
+ return true ;
391
378
392
- Log .e (LOG_TAG , "Payload with pushId " + pushId + " not found!" );
393
379
} else {
394
380
Log .e (LOG_TAG , "Payload array is null or empty!" );
381
+ return false ;
395
382
}
396
383
} catch (Exception e ) {
397
384
StackTraceElement element = new Throwable ().getStackTrace ()[0 ];
@@ -404,9 +391,11 @@ public static void readAllPushMessages(Context context, String pushId) {
404
391
Log .e (LOG_TAG , "Could not update the push message!" );
405
392
Log .e (LOG_TAG , e .getMessage ());
406
393
}
394
+ return false ;
407
395
}
408
396
409
- public static void readPushMessagesWithPushId (Context context , String pushId ) {
397
+ public boolean readPushMessagesWithPushId (Context context , String pushId ) {
398
+ boolean isUpdated = false ;
410
399
try {
411
400
String jsonString = SharedPreference .getString (context , Constants .PAYLOAD_SP_KEY );
412
401
JSONObject jsonObject = new JSONObject (jsonString );
@@ -417,20 +406,20 @@ public static void readPushMessagesWithPushId(Context context, String pushId) {
417
406
for (int i = 0 ; i < payloadsArray .length (); i ++) {
418
407
JSONObject payloadObject = payloadsArray .getJSONObject (i );
419
408
String existingPushId = payloadObject .optString ("pushId" , "" );
420
- if (pushId != null && !pushId .isEmpty ()) {
421
- if (existingPushId .equals (pushId )) {
422
- payloadObject .put ("status" , "O" );
423
- payloadObject .put ("openDate" , new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ()).format (new Date ()));
424
-
425
- SharedPreference .saveString (context , Constants .PAYLOAD_SP_KEY , jsonObject .toString ());
426
- return ;
427
- }
409
+ if (pushId != null && !pushId .isEmpty () && existingPushId .equals (pushId )) {
410
+ payloadObject .put ("status" , "O" );
411
+ payloadObject .put ("openDate" , new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" , Locale .getDefault ()).format (new Date ()));
412
+ isUpdated = true ;
428
413
}
429
414
}
430
-
431
- Log .e (LOG_TAG , "Payload with pushId " + pushId + " not found!" );
415
+ if (isUpdated ) {
416
+ jsonObject .put (Constants .PAYLOAD_SP_ARRAY_KEY , payloadsArray );
417
+ SharedPreference .saveString (context , Constants .PAYLOAD_SP_KEY , jsonObject .toString ());
418
+ }
419
+ return isUpdated ;
432
420
} else {
433
421
Log .e (LOG_TAG , "Payload array is null or empty!" );
422
+ return false ;
434
423
}
435
424
} catch (Exception e ) {
436
425
StackTraceElement element = new Throwable ().getStackTrace ()[0 ];
@@ -442,6 +431,7 @@ public static void readPushMessagesWithPushId(Context context, String pushId) {
442
431
);
443
432
Log .e (LOG_TAG , "Could not update the push message!" );
444
433
Log .e (LOG_TAG , e .getMessage ());
434
+ return false ;
445
435
}
446
436
}
447
437
0 commit comments