Skip to content

Commit

Permalink
readPushMessages private to public
Browse files Browse the repository at this point in the history
  • Loading branch information
FatihUtkuKara committed May 20, 2024
1 parent dd7690d commit 3c5794d
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -274,8 +275,24 @@ public void onClick(View v) {
binding.btnTextDeletePushWithId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EuroMobileManager.deletePushNotificationWithId(getApplicationContext(),123);
PushMessageInterface pushMessageInterface = new PushMessageInterface() {
@Override
public void success(List<Message> pushMessages) {
for (Message pushMessage : pushMessages) {
EuroMobileManager.deletePushNotificationWithId(getApplicationContext(), pushMessage.getNotificationId());
}

}

@Override
public void fail(String errorMessage) {


}
};

}

});
binding.btnRegisteremail.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package euromsg.com.euromobileandroid;

import static euromsg.com.euromobileandroid.Constants.LOG_TAG;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
Expand All @@ -24,8 +26,10 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -1037,6 +1041,83 @@ public static boolean deletePushNotificationWithId(Context context, Integer noti
return false;
}

public static boolean readAllPushMessages(Context context) {
try {
String jsonString = SharedPreference.getString(context, Constants.PAYLOAD_SP_KEY);
JSONObject jsonObject = new JSONObject(jsonString);

JSONArray payloadsArray = jsonObject.optJSONArray(Constants.PAYLOAD_SP_ARRAY_KEY);

if (payloadsArray != null) {
for (int i = 0; i < payloadsArray.length(); i++) {
JSONObject payloadObject = payloadsArray.getJSONObject(i);

payloadObject.put("status", "O");
payloadObject.put("openDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
}
jsonObject.put(Constants.PAYLOAD_SP_ARRAY_KEY, payloadsArray);
SharedPreference.saveString(context, Constants.PAYLOAD_SP_KEY, jsonObject.toString());
return true;

} else {
Log.e(LOG_TAG, "Payload array is null or empty!");
return false;
}
} catch (Exception e) {
StackTraceElement element = new Throwable().getStackTrace()[0];
LogUtils.formGraylogModel(
context,
"e",
"Updating push message string : " + e.getMessage(),
element.getClassName() + "/" + element.getMethodName() + "/" + element.getLineNumber()
);
Log.e(LOG_TAG, "Could not update the push message!");
Log.e(LOG_TAG, e.getMessage());
}
return false;
}

public boolean readPushMessagesWithPushId(Context context, String pushId) {
boolean isUpdated = false;
try {
String jsonString = SharedPreference.getString(context, Constants.PAYLOAD_SP_KEY);
JSONObject jsonObject = new JSONObject(jsonString);

JSONArray payloadsArray = jsonObject.optJSONArray(Constants.PAYLOAD_SP_ARRAY_KEY);

if (payloadsArray != null) {
for (int i = 0; i < payloadsArray.length(); i++) {
JSONObject payloadObject = payloadsArray.getJSONObject(i);
String existingPushId = payloadObject.optString("pushId", "");
if (pushId != null && !pushId.isEmpty() && existingPushId.equals(pushId)) {
payloadObject.put("status", "O");
payloadObject.put("openDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
isUpdated = true;
}
}
if (isUpdated) {
jsonObject.put(Constants.PAYLOAD_SP_ARRAY_KEY, payloadsArray);
SharedPreference.saveString(context, Constants.PAYLOAD_SP_KEY, jsonObject.toString());
}
return isUpdated;
} else {
Log.e(LOG_TAG, "Payload array is null or empty!");
return false;
}
} catch (Exception e) {
StackTraceElement element = new Throwable().getStackTrace()[0];
LogUtils.formGraylogModel(
context,
"e",
"Updating push message string : " + e.getMessage(),
element.getClassName() + "/" + element.getMethodName() + "/" + element.getLineNumber()
);
Log.e(LOG_TAG, "Could not update the push message!");
Log.e(LOG_TAG, e.getMessage());
return false;
}
}

/**
* This method returns the list of push messages sent in the last 30 days.
* The messages are ordered in terms of their timestamps e.g. most recent one is at index 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Message implements Serializable {
private String deliver;
private String silent;
private PushType pushType;
private Integer notificationId;
private String collapseKey;
private Map<String, String> params = new HashMap<>();
private ArrayList<Element> elements;
Expand Down Expand Up @@ -207,6 +208,14 @@ public void setStatus(String status) {
this.status = status;
}

public Integer setNotifiactionId() {
return notificationId;
}

public Integer getNotificationId() {
return notificationId;
}

public String getAltUrl() {
return altUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
import euromsg.com.euromobileandroid.utils.AppUtils;
import euromsg.com.euromobileandroid.utils.ImageUtils;
import euromsg.com.euromobileandroid.utils.LogUtils;
import euromsg.com.euromobileandroid.utils.PayloadUtils;
import euromsg.com.euromobileandroid.utils.SharedPreference;

public class PushNotificationManager {

Intent intent;

public Integer lastNotificationId;

public void generateCarouselNotification(Context context, Message pushMessage, int notificationId) {

Expand Down Expand Up @@ -76,6 +77,7 @@ public void generateNotification(Context context, Message pushMessage, Bitmap im

if(mNotificationManager != null) {
mNotificationManager.notify(notificationId, mBuilder.build());
PayloadUtils.updatePayloadWithId(context,pushMessage.getPushId(),notificationId);
}

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import euromsg.com.euromobileandroid.Constants;
import euromsg.com.euromobileandroid.EuroMobileManager;
import euromsg.com.euromobileandroid.model.Message;
import euromsg.com.euromobileandroid.notification.PushNotificationManager;

public final class PayloadUtils {
private static final String LOG_TAG = "PayloadUtils";
Expand Down Expand Up @@ -358,44 +359,8 @@ public static void updatePayload(Context context, String pushId) {
Log.e(LOG_TAG, e.getMessage());
}
}
public static boolean readAllPushMessages(Context context) {
try {
String jsonString = SharedPreference.getString(context, Constants.PAYLOAD_SP_KEY);
JSONObject jsonObject = new JSONObject(jsonString);

JSONArray payloadsArray = jsonObject.optJSONArray(Constants.PAYLOAD_SP_ARRAY_KEY);

if (payloadsArray != null) {
for (int i = 0; i < payloadsArray.length(); i++) {
JSONObject payloadObject = payloadsArray.getJSONObject(i);

payloadObject.put("status", "O");
payloadObject.put("openDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
}
jsonObject.put(Constants.PAYLOAD_SP_ARRAY_KEY, payloadsArray);
SharedPreference.saveString(context, Constants.PAYLOAD_SP_KEY, jsonObject.toString());
return true;

} else {
Log.e(LOG_TAG, "Payload array is null or empty!");
return false;
}
} catch (Exception e) {
StackTraceElement element = new Throwable().getStackTrace()[0];
LogUtils.formGraylogModel(
context,
"e",
"Updating push message string : " + e.getMessage(),
element.getClassName() + "/" + element.getMethodName() + "/" + element.getLineNumber()
);
Log.e(LOG_TAG, "Could not update the push message!");
Log.e(LOG_TAG, e.getMessage());
}
return false;
}

public boolean readPushMessagesWithPushId(Context context, String pushId) {
boolean isUpdated = false;
public static void updatePayloadWithId(Context context, String pushId, Integer notificationId) {
try {
String jsonString = SharedPreference.getString(context, Constants.PAYLOAD_SP_KEY);
JSONObject jsonObject = new JSONObject(jsonString);
Expand All @@ -406,22 +371,23 @@ public boolean readPushMessagesWithPushId(Context context, String pushId) {
for (int i = 0; i < payloadsArray.length(); i++) {
JSONObject payloadObject = payloadsArray.getJSONObject(i);
String existingPushId = payloadObject.optString("pushId", "");
if (pushId != null && !pushId.isEmpty() && existingPushId.equals(pushId)) {
payloadObject.put("status", "O");
payloadObject.put("openDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date()));
isUpdated = true;

if (existingPushId.equals(pushId)) {
// Güncelleme işlemlerini yap
payloadObject.put("notificationId", notificationId);

// Güncellenmiş JSON'ı kaydet
SharedPreference.saveString(context, Constants.PAYLOAD_SP_KEY, jsonObject.toString());
return; // Güncelleme işlemi tamamlandı, fonksiyondan çık
}
}
if (isUpdated) {
jsonObject.put(Constants.PAYLOAD_SP_ARRAY_KEY, payloadsArray);
SharedPreference.saveString(context, Constants.PAYLOAD_SP_KEY, jsonObject.toString());
}
return isUpdated;

// Eğer bu noktaya gelinirse, belirtilen pushId ile bir payload bulunamamıştır.
Log.e(LOG_TAG, "Payload with pushId " + pushId + " not found!");
} else {
Log.e(LOG_TAG, "Payload array is null or empty!");
return false;
}
} catch (Exception e) {
} catch (JSONException e) {
StackTraceElement element = new Throwable().getStackTrace()[0];
LogUtils.formGraylogModel(
context,
Expand All @@ -431,11 +397,11 @@ public boolean readPushMessagesWithPushId(Context context, String pushId) {
);
Log.e(LOG_TAG, "Could not update the push message!");
Log.e(LOG_TAG, e.getMessage());
return false;
}
}



private static boolean compareDates(Context context, String str1, String str2) {
boolean res = false;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Expand Down

0 comments on commit 3c5794d

Please sign in to comment.