Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store "HasRequestedNotificationPermission" in native LocalStorage #240

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/android/verification/SensorControlForegroundDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/*
Expand Down Expand Up @@ -53,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;
Expand Down Expand Up @@ -775,11 +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");
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:
Expand Down
11 changes: 9 additions & 2 deletions src/ios/Verification/SensorControlForegroundDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#import "LocalNotificationManager.h"
#import "BEMAppDelegate.h"
#import "BEMActivitySync.h"
#import "BEMBuiltinUserCache.h"
#import "DataUtils.h"

#import <CoreMotion/CoreMotion.h>

Expand All @@ -13,6 +15,8 @@ @interface SensorControlForegroundDelegate() {
}
@end

static NSString* const HAS_REQUESTED_NOTIFS_KEY = @"HasRequestedNotificationPermission";

@implementation SensorControlForegroundDelegate

/*
Expand Down Expand Up @@ -259,7 +263,8 @@ -(void) didRecieveFitnessPermission:(BOOL)isPermitted
- (void)checkAndPromptNotificationPermission {
NSString* callbackId = [command callbackId];
@try {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasPromptedForUserNotification"]) {
// 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];
Expand All @@ -282,7 +287,9 @@ - (void)checkAndPromptNotificationPermission {
}

- (void) didRegisterUserNotificationSettings:(UIUserNotificationSettings*)newSettings {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasPromptedForUserNotification"];
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) {
Expand Down