Skip to content

Commit 3730763

Browse files
AmauryLietMinishlink
authored andcommitted
Add getClientUniqueId/reset (#1)
1 parent bf0a042 commit 3730763

8 files changed

Lines changed: 49 additions & 0 deletions

File tree

android/app/src/main/java/com/appzung/codepush/react/CodePushNativeModule.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,20 @@ public void clearUpdates() {
731731
mCodePush.clearUpdates();
732732
}
733733

734+
@ReactMethod
735+
public void resetClientUniqueId(Promise promise) {
736+
try {
737+
String newClientUniqueId = UUID.randomUUID().toString();
738+
SharedPreferences preferences = mCodePush.getContext().getSharedPreferences(CodePushConstants.CODE_PUSH_PREFERENCES, 0);
739+
preferences.edit().putString(CodePushConstants.CLIENT_UNIQUE_ID_KEY, newClientUniqueId).apply();
740+
mClientUniqueId = newClientUniqueId;
741+
promise.resolve(mClientUniqueId);
742+
} catch (Exception e) {
743+
CodePushUtils.log(e);
744+
promise.reject(e);
745+
}
746+
}
747+
734748
@ReactMethod
735749
public void addListener(String eventName) {
736750
// Set up any upstream listeners or background tasks as necessary

ios/CodePush/CodePush.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
@property (readonly) NSString *buildVersion;
102102
@property (readonly) NSDictionary *configuration;
103103
@property (copy) NSString *releaseChannelPublicId;
104+
@property (readonly) NSString *clientUniqueId;
104105
@property (copy) NSString *serverURL;
105106
@property (copy) NSString *publicKey;
106107

ios/CodePush/CodePush.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,13 @@ - (void)restartAppInternal:(BOOL)onlyIfUpdateIsPending
10421042
[CodePush clearUpdates];
10431043
}
10441044

1045+
RCT_EXPORT_METHOD(resetClientUniqueId:(RCTPromiseResolveBlock)resolve
1046+
rejecter:(RCTPromiseRejectBlock)reject)
1047+
{
1048+
CPLog(@"On iOS, resetting client device ID does nothing as it is the iOS IDFV.");
1049+
resolve([[CodePushConfig current] clientUniqueId]);
1050+
}
1051+
10451052
#pragma mark - JavaScript-exported module methods (Private)
10461053

10471054
/*

src/getClientUniqueId.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getConfiguration } from './internals/getConfiguration';
2+
3+
/**
4+
* Gets the client's unique ID set by the module. You may also see resetClientUniqueId.
5+
*/
6+
export const getClientUniqueId = async () => {
7+
const nativeConfig = await getConfiguration();
8+
return nativeConfig.clientUniqueId;
9+
};

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export * from './getUpdateMetadata';
88
export * from './notifyAppReady';
99
export * from './restartApp';
1010
export * from './sync';
11+
export * from './getClientUniqueId';
12+
export * from './resetClientUniqueId';
1113

1214
export * from './types';
1315

src/internals/RNAppZungCodePushModuleSpec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface Spec extends TurboModule {
2020
};
2121

2222
getConfiguration(): Promise<Configuration>;
23+
resetClientUniqueId(): Promise<string>;
2324

2425
getUpdateMetadata(updateState: UpdateState): Promise<OmitFunctions<LocalPackage>>;
2526
installUpdate(

src/internals/getConfiguration.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export async function getConfiguration() {
1010

1111
return config;
1212
}
13+
14+
export async function reloadCachedConfiguration() {
15+
config = await NativeRNAppZungCodePushModule.getConfiguration();
16+
}

src/resetClientUniqueId.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NativeRNAppZungCodePushModule } from './internals/NativeRNAppZungCodePushModule';
2+
import { reloadCachedConfiguration } from './internals/getConfiguration';
3+
4+
/**
5+
* Resets the client's unique ID. You may use this in some GDPR compliance scenarios. Note that this will create a new MAU if a new request is sent later.
6+
*/
7+
export const resetClientUniqueId = async () => {
8+
const newId = await NativeRNAppZungCodePushModule.resetClientUniqueId();
9+
await reloadCachedConfiguration();
10+
return newId;
11+
};

0 commit comments

Comments
 (0)