From 153e3f242088833cef05fcf5c228f7be035e99c4 Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Tue, 8 Oct 2024 17:17:14 -0400 Subject: [PATCH 1/3] store "HasRequestedNotificationPermission" in native LocalStorage --- .../verification/SensorControlForegroundDelegate.java | 4 ++++ src/ios/Verification/SensorControlForegroundDelegate.m | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/android/verification/SensorControlForegroundDelegate.java b/src/android/verification/SensorControlForegroundDelegate.java index 32badc9..03d9ff8 100644 --- a/src/android/verification/SensorControlForegroundDelegate.java +++ b/src/android/verification/SensorControlForegroundDelegate.java @@ -5,6 +5,7 @@ import edu.berkeley.eecs.emission.cordova.tracker.Constants; import edu.berkeley.eecs.emission.cordova.unifiedlogger.Log; +import edu.berkeley.eecs.emission.cordova.usercache.UserCacheFactory; /* @@ -775,6 +776,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { case SensorControlConstants.ENABLE_NOTIFICATIONS: Log.d(mAct, TAG, requestCode + " is our code, handling callback"); Log.d(mAct, TAG, "Got notification callback from launching app settings"); + JSONObject jo = new JSONObject(); + jo.put("value", true); + UserCacheFactory.getUserCache(cordova.getActivity()).putLocalStorage("HasRequestedNotificationPermission", jo); if (SensorControlChecks.checkNotificationsEnabled(cordova.getActivity())) { SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity()); cordovaCallback.success(); diff --git a/src/ios/Verification/SensorControlForegroundDelegate.m b/src/ios/Verification/SensorControlForegroundDelegate.m index 4e8e432..86f2d3b 100644 --- a/src/ios/Verification/SensorControlForegroundDelegate.m +++ b/src/ios/Verification/SensorControlForegroundDelegate.m @@ -4,6 +4,7 @@ #import "LocalNotificationManager.h" #import "BEMAppDelegate.h" #import "BEMActivitySync.h" +#import "BEMBuiltinUserCache.h" #import @@ -259,7 +260,7 @@ -(void) didRecieveFitnessPermission:(BOOL)isPermitted - (void)checkAndPromptNotificationPermission { NSString* callbackId = [command callbackId]; @try { - if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasPromptedForUserNotification"]) { + if ([[BuiltinUserCache database] getLocalStorage:@"HasRequestedNotificationPermission" withMetadata:NO] != NULL) { NSLog(@"Already prompted request for user notification. Launching app settings."); [AppDelegate registerForegroundDelegate:self]; [self openAppSettings]; @@ -282,7 +283,8 @@ - (void)checkAndPromptNotificationPermission { } - (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings*)newSettings { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasPromptedForUserNotification"]; + [[BuiltinUserCache database] putLocalStorage:@"HasRequestedNotificationPermission" + jsonValue:@{ @"value" : @(TRUE) }]; NSString* callbackId = [command callbackId]; UIUserNotificationSettings* requestedSettings = [TripDiarySensorControlChecks REQUESTED_NOTIFICATION_TYPES]; if (requestedSettings.types == newSettings.types) { From e062604a12a2728c7d5ad93ad12f606b4dbf5f48 Mon Sep 17 00:00:00 2001 From: Jack Greenlee Date: Tue, 8 Oct 2024 17:54:20 -0400 Subject: [PATCH 2/3] store ts in 'HasRequestedNotificationPermission'; declare as constant --- .../verification/SensorControlForegroundDelegate.java | 5 +++-- .../Verification/SensorControlForegroundDelegate.m | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/android/verification/SensorControlForegroundDelegate.java b/src/android/verification/SensorControlForegroundDelegate.java index 03d9ff8..85f4d30 100644 --- a/src/android/verification/SensorControlForegroundDelegate.java +++ b/src/android/verification/SensorControlForegroundDelegate.java @@ -54,6 +54,7 @@ public class SensorControlForegroundDelegate { public static final String TAG = "SensorPermissionsAndSettingsForegroundDelegate"; + private static final String HAS_REQUESTED_NOTIFS_KEY = "HasRequestedNotificationPermission"; private CordovaPlugin plugin = null; private CordovaInterface cordova = null; @@ -777,8 +778,8 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(mAct, TAG, requestCode + " is our code, handling callback"); Log.d(mAct, TAG, "Got notification callback from launching app settings"); JSONObject jo = new JSONObject(); - jo.put("value", true); - UserCacheFactory.getUserCache(cordova.getActivity()).putLocalStorage("HasRequestedNotificationPermission", jo); + jo.put("ts", System.currentTimeMillis() / 1000); + UserCacheFactory.getUserCache(cordova.getActivity()).putLocalStorage(HAS_REQUESTED_NOTIFS_KEY, jo); if (SensorControlChecks.checkNotificationsEnabled(cordova.getActivity())) { SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity()); cordovaCallback.success(); diff --git a/src/ios/Verification/SensorControlForegroundDelegate.m b/src/ios/Verification/SensorControlForegroundDelegate.m index 86f2d3b..b71e525 100644 --- a/src/ios/Verification/SensorControlForegroundDelegate.m +++ b/src/ios/Verification/SensorControlForegroundDelegate.m @@ -5,6 +5,7 @@ #import "BEMAppDelegate.h" #import "BEMActivitySync.h" #import "BEMBuiltinUserCache.h" +#import "DataUtils.h" #import @@ -14,6 +15,8 @@ @interface SensorControlForegroundDelegate() { } @end +static NSString* const HAS_REQUESTED_NOTIFS_KEY = @"HasRequestedNotificationPermission"; + @implementation SensorControlForegroundDelegate /* @@ -260,7 +263,8 @@ -(void) didRecieveFitnessPermission:(BOOL)isPermitted - (void)checkAndPromptNotificationPermission { NSString* callbackId = [command callbackId]; @try { - if ([[BuiltinUserCache database] getLocalStorage:@"HasRequestedNotificationPermission" withMetadata:NO] != NULL) { + // If we have prompted in the past, the popup will not work. Instead, we'll open app settings + if ([[BuiltinUserCache database] getLocalStorage:HAS_REQUESTED_NOTIFS_KEY withMetadata:NO] != NULL) { NSLog(@"Already prompted request for user notification. Launching app settings."); [AppDelegate registerForegroundDelegate:self]; [self openAppSettings]; @@ -283,8 +287,9 @@ - (void)checkAndPromptNotificationPermission { } - (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings*)newSettings { - [[BuiltinUserCache database] putLocalStorage:@"HasRequestedNotificationPermission" - jsonValue:@{ @"value" : @(TRUE) }]; + NSDate* now = [NSDate date]; + [[BuiltinUserCache database] putLocalStorage:HAS_REQUESTED_NOTIFS_KEY + jsonValue:@{ @"ts": @(now.timeIntervalSince1970) }]; NSString* callbackId = [command callbackId]; UIUserNotificationSettings* requestedSettings = [TripDiarySensorControlChecks REQUESTED_NOTIFICATION_TYPES]; if (requestedSettings.types == newSettings.types) { From 1ff879b5f0bf7de6ddce39a84a7335e62348ea63 Mon Sep 17 00:00:00 2001 From: Shankari Date: Tue, 8 Oct 2024 15:26:35 -0700 Subject: [PATCH 3/3] Catch JSON exception and return an error to get android to compile Testing done: ``` $ npx cordova build ** BUILD SUCCEEDED ** ``` --- .../SensorControlForegroundDelegate.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/android/verification/SensorControlForegroundDelegate.java b/src/android/verification/SensorControlForegroundDelegate.java index 85f4d30..14b5617 100644 --- a/src/android/verification/SensorControlForegroundDelegate.java +++ b/src/android/verification/SensorControlForegroundDelegate.java @@ -777,14 +777,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { case SensorControlConstants.ENABLE_NOTIFICATIONS: Log.d(mAct, TAG, requestCode + " is our code, handling callback"); Log.d(mAct, TAG, "Got notification callback from launching app settings"); - JSONObject jo = new JSONObject(); - jo.put("ts", System.currentTimeMillis() / 1000); - UserCacheFactory.getUserCache(cordova.getActivity()).putLocalStorage(HAS_REQUESTED_NOTIFS_KEY, jo); - if (SensorControlChecks.checkNotificationsEnabled(cordova.getActivity())) { - SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity()); - cordovaCallback.success(); - } else { - cordovaCallback.error(cordova.getActivity().getString(R.string.notifications_blocked)); + try { + JSONObject jo = new JSONObject(); + jo.put("ts", System.currentTimeMillis() / 1000); + UserCacheFactory.getUserCache(cordova.getActivity()).putLocalStorage(HAS_REQUESTED_NOTIFS_KEY, jo); + if (SensorControlChecks.checkNotificationsEnabled(cordova.getActivity())) { + SensorControlBackgroundChecker.restartFSMIfStartState(cordova.getActivity()); + cordovaCallback.success(); + } else { + cordovaCallback.error(cordova.getActivity().getString(R.string.notifications_blocked)); + } + } catch (JSONException e) { + cordovaCallback.error(e.getLocalizedMessage()); } break; case SensorControlConstants.REMOVE_UNUSED_APP_RESTRICTIONS: