From fc8e552f650cc465f35a67a375ab68cedaf3a4d6 Mon Sep 17 00:00:00 2001 From: sumeruchat Date: Fri, 10 Jan 2025 21:49:30 +0100 Subject: [PATCH] Logic fix suggestion for Push Notifications (#859) * [MOB-9446] Logic fix * [MOB-9446] Logic fix * [MOB-9446] Logic fix * [MOB-9446] Logic fix * [MOB-9446] Logic fix * adds register push path for system notifications * [MOB-9446] Logic fix --------- Co-authored-by: Evan Greer Co-authored-by: Evan Takeo Kanaiaupuni Greer <56953678+evantk91@users.noreply.github.com> --- .../com/iterable/iterableapi/IterableApi.java | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java index 6b7dcfe55..ff23fd388 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java @@ -390,26 +390,33 @@ public void onSwitchToBackground() {} }; private void onForeground() { - boolean systemNotificationEnabled = NotificationManagerCompat.from(_applicationContext).areNotificationsEnabled(); - SharedPreferences sharedPref = sharedInstance.getMainActivityContext().getSharedPreferences(IterableConstants.SHARED_PREFS_FILE, Context.MODE_PRIVATE); - boolean isNotificationEnabled = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, false); - if (!_firstForegroundHandled) { _firstForegroundHandled = true; + if (sharedInstance.config.autoPushRegistration && sharedInstance.isInitialized()) { + sharedInstance.registerForPush(); + } fetchRemoteConfiguration(); } - if (sharedInstance.config.autoPushRegistration && sharedInstance.isInitialized() && isNotificationEnabled != systemNotificationEnabled) { - if (systemNotificationEnabled) { - sharedInstance.registerForPush(); - } else { - sharedInstance.disablePush(); + boolean systemNotificationEnabled = NotificationManagerCompat.from(_applicationContext).areNotificationsEnabled(); + SharedPreferences sharedPref = sharedInstance.getMainActivityContext().getSharedPreferences(IterableConstants.SHARED_PREFS_FILE, Context.MODE_PRIVATE); + + boolean hasStoredPermission = sharedPref.contains(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED); + boolean isNotificationEnabled = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, false); + + if (sharedInstance.isInitialized()) { + if (hasStoredPermission && (isNotificationEnabled != systemNotificationEnabled)) { + if (!systemNotificationEnabled) { + sharedInstance.disablePush(); + } else { + sharedInstance.registerForPush(); + } } - } - SharedPreferences.Editor editor = sharedPref.edit(); - editor.putBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, systemNotificationEnabled); - editor.apply(); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, systemNotificationEnabled); + editor.apply(); + } } private boolean isInitialized() {