From 0947baeffe61d6e5691b93a270f8a233afed94a0 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Sat, 4 Sep 2021 11:33:34 -0400 Subject: [PATCH] Android clear notification preferences on version change (#5666) * Android clear notification preferences on version change * remove spacings --- .../rnbeta/CustomPushNotification.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java index 588c0c9f9..2bf701de3 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/CustomPushNotification.java @@ -5,6 +5,8 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.os.Bundle; import android.service.notification.StatusBarNotification; import android.util.Log; @@ -33,6 +35,7 @@ import org.json.JSONObject; public class CustomPushNotification extends PushNotification { private static final String PUSH_NOTIFICATIONS = "PUSH_NOTIFICATIONS"; + private static final String VERSION_PREFERENCE = "VERSION_PREFERENCE"; private static final String PUSH_TYPE_MESSAGE = "message"; private static final String PUSH_TYPE_CLEAR = "clear"; private static final String PUSH_TYPE_SESSION = "session"; @@ -41,6 +44,29 @@ public class CustomPushNotification extends PushNotification { public CustomPushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) { super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper); CustomPushNotificationHelper.createNotificationChannels(context); + + try { + PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); + String version = String.valueOf(pInfo.versionCode); + String storedVersion = null; + SharedPreferences pSharedPref = context.getSharedPreferences(VERSION_PREFERENCE, Context.MODE_PRIVATE); + if (pSharedPref != null) { + storedVersion = pSharedPref.getString("Version", ""); + } + + if (!version.equals(storedVersion)) { + if (pSharedPref != null) { + SharedPreferences.Editor editor = pSharedPref.edit(); + editor.putString("Version", version); + editor.apply(); + } + + Map> inputMap = new HashMap<>(); + saveNotificationsMap(context, inputMap); + } + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + } } public static void cancelNotification(Context context, String channelId, Integer notificationId) {