Skip to content

Commit 33f69c3

Browse files
authored
Merge pull request #855 from Iterable/evan/MOB-9446-Enhance-Push-Notification-State-Tracking-in-SDKs
[MOB-9446] enhance push notification state tracking
2 parents f4cdb19 + 530427c commit 33f69c3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import androidx.annotation.Nullable;
1111
import androidx.annotation.RestrictTo;
1212
import androidx.annotation.VisibleForTesting;
13+
import androidx.core.app.NotificationManagerCompat;
1314

1415
import com.iterable.iterableapi.util.DeviceInfoUtils;
1516

@@ -49,7 +50,7 @@ public class IterableApi {
4950
private String inboxSessionId;
5051
private IterableAuthManager authManager;
5152
private HashMap<String, String> deviceAttributes = new HashMap<>();
52-
private IterableKeychain keychain;
53+
public IterableKeychain keychain;
5354

5455
void fetchRemoteConfiguration() {
5556
apiClient.getRemoteConfiguration(new IterableHelper.IterableActionHandler() {
@@ -389,14 +390,26 @@ public void onSwitchToBackground() {}
389390
};
390391

391392
private void onForeground() {
393+
boolean systemNotificationEnabled = NotificationManagerCompat.from(_applicationContext).areNotificationsEnabled();
394+
SharedPreferences sharedPref = sharedInstance.getMainActivityContext().getSharedPreferences(IterableConstants.SHARED_PREFS_FILE, Context.MODE_PRIVATE);
395+
boolean isNotificationEnabled = sharedPref.getBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, false);
396+
392397
if (!_firstForegroundHandled) {
393398
_firstForegroundHandled = true;
394-
if (sharedInstance.config.autoPushRegistration && sharedInstance.isInitialized()) {
395-
IterableLogger.d(TAG, "Performing automatic push registration");
399+
fetchRemoteConfiguration();
400+
}
401+
402+
if (sharedInstance.config.autoPushRegistration && sharedInstance.isInitialized() && isNotificationEnabled != systemNotificationEnabled) {
403+
if (systemNotificationEnabled) {
396404
sharedInstance.registerForPush();
405+
} else {
406+
sharedInstance.disablePush();
397407
}
398-
fetchRemoteConfiguration();
399408
}
409+
410+
SharedPreferences.Editor editor = sharedPref.edit();
411+
editor.putBoolean(IterableConstants.SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED, systemNotificationEnabled);
412+
editor.apply();
400413
}
401414

402415
private boolean isInitialized() {

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public final class IterableConstants {
118118
public static final String SHARED_PREFS_FCM_MIGRATION_DONE_KEY = "itbl_fcm_migration_done";
119119
public static final String SHARED_PREFS_SAVED_CONFIGURATION = "itbl_saved_configuration";
120120
public static final String SHARED_PREFS_OFFLINE_MODE_KEY = "itbl_offline_mode";
121+
public static final String SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED = "itbl_notifications_enabled";
121122

122123
//Action buttons
123124
public static final String ITBL_BUTTON_IDENTIFIER = "identifier";

0 commit comments

Comments
 (0)