Skip to content

Commit

Permalink
Logic fix suggestion for Push Notifications (#859)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
Co-authored-by: Evan Takeo Kanaiaupuni Greer <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent 4fcbe15 commit fc8e552
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit fc8e552

Please sign in to comment.