Skip to content

Commit

Permalink
[expo-notifications] Add private InstallationIdProvider (#6749)
Browse files Browse the repository at this point in the history
# Why

To be able to register at Expo server we need to identify the device with a UUID.

# How

Implemented a non-exported module which:
- uses the same storage as kernel does for the UUID (in native, on web uses `localStorage`) 
- if the ID is missing, it generates and saves a new one.

# Test Plan

None yet, implementing `getExpoPushTokenAsync` will be a test for this module.
  • Loading branch information
sjchmiela authored and EvanBacon committed Feb 13, 2020
1 parent bb8de83 commit 039f06a
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 68 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 73 additions & 65 deletions apps/bare-expo/ios/Pods/EXNotifications.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import org.unimodules.core.ExportedModule;
import org.unimodules.core.interfaces.SingletonModule;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import expo.modules.notifications.installationid.InstallationIdProvider;
import expo.modules.notifications.tokens.PushTokenManager;
import expo.modules.notifications.tokens.PushTokenModule;

public class NotificationsPackage extends BasePackage {
@Override
public List<ExportedModule> createExportedModules(Context context) {
return Collections.singletonList((ExportedModule) new PushTokenModule(context));
return Arrays.asList(
new PushTokenModule(context),
new InstallationIdProvider(context)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package expo.modules.notifications.installationid;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.UUID;

public class InstallationId {
private static final String PREFERENCES_KEY = "host.exp.exponent.SharedPreferences";
private static final String UUID_KEY = "uuid";
private SharedPreferences mSharedPreferences;

public InstallationId(Context context) {
mSharedPreferences = context.getSharedPreferences(PREFERENCES_KEY, Context.MODE_PRIVATE);
}

public String getId() {
return getOrCreateId();
}

private String getOrCreateId() {
String uuid = mSharedPreferences.getString(UUID_KEY, null);
if (uuid != null) {
return uuid;
}

uuid = UUID.randomUUID().toString();
mSharedPreferences.edit().putString(UUID_KEY, uuid).apply();
return uuid;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package expo.modules.notifications.installationid;

import android.content.Context;

import org.unimodules.core.ExportedModule;
import org.unimodules.core.Promise;
import org.unimodules.core.interfaces.ExpoMethod;

public class InstallationIdProvider extends ExportedModule {
private static final String EXPORTED_NAME = "NotificationsInstallationIdProvider";

private InstallationId mInstallationId;

public InstallationIdProvider(Context context) {
super(context);
mInstallationId = new InstallationId(context);
}

@Override
public String getName() {
return EXPORTED_NAME;
}

@ExpoMethod
public void getInstallationIdAsync(Promise promise) {
promise.resolve(mInstallationId.getId());
}
}
3 changes: 3 additions & 0 deletions packages/expo-notifications/build/InstallationIdProvider.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/expo-notifications/build/InstallationIdProvider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions packages/expo-notifications/build/InstallationIdProvider.web.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2018-present 650 Industries. All rights reserved.

#import <UMCore/UMExportedModule.h>

NS_ASSUME_NONNULL_BEGIN

@interface EXInstallationIdProvider : UMExportedModule

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-present 650 Industries. All rights reserved.

#import <EXNotifications/EXInstallationIdProvider.h>

NSString * const kEXDeviceInstallUUIDKey = @"EXDeviceInstallUUIDKey";

@implementation EXInstallationIdProvider

UM_EXPORT_MODULE(NotificationsInstallationIdProvider)

UM_EXPORT_METHOD_AS(getInstallationIdAsync, getInstallationIdAsyncWithResolver:(UMPromiseResolveBlock)resolve rejecter:(UMPromiseRejectBlock)reject)
{
resolve([self getInstallationId]);
}

- (NSString *)getInstallationId
{
NSString *uuid = [[NSUserDefaults standardUserDefaults] stringForKey:kEXDeviceInstallUUIDKey];
if (!uuid) {
uuid = [[NSUUID UUID] UUIDString];
[[NSUserDefaults standardUserDefaults] setObject:uuid forKey:kEXDeviceInstallUUIDKey];
}
return uuid;
}

@end
6 changes: 5 additions & 1 deletion packages/expo-notifications/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
"@unimodules/core": "~5.0.0",
"expo-constants": "~8.0.0"
},
"gitHead": "ec7878b9ce54f2537721218ae0fe4017e4004806"
"gitHead": "ec7878b9ce54f2537721218ae0fe4017e4004806",
"dependencies": {
"@types/uuid-js": "^0.7.2",
"uuid-js": "^0.7.5"
}
}
5 changes: 5 additions & 0 deletions packages/expo-notifications/src/InstallationIdProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NativeModulesProxy } from '@unimodules/core';

import { InstallationIdProvider } from './InstallationIdProvider.types';

export default (NativeModulesProxy.NotificationsInstallationIdProvider as any) as InstallationIdProvider;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ProxyNativeModule } from '@unimodules/core';

export interface InstallationIdProvider extends ProxyNativeModule {
getInstallationIdAsync: () => Promise<string>;
}
32 changes: 32 additions & 0 deletions packages/expo-notifications/src/InstallationIdProvider.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import UUID from 'uuid-js';

import { InstallationIdProvider } from './InstallationIdProvider.types';

const INSTALLATION_ID_KEY = 'EXPO_NOTIFICATIONS_INSTALLATION_ID';

// Lazy fallback installationId per session initializer
let getFallbackInstallationId = () => {
const sessionInstallationId = UUID.create().toString();
getFallbackInstallationId = () => sessionInstallationId;
};

export default {
getInstallationIdAsync: async () => {
let installationId;

try {
installationId = localStorage.getItem(INSTALLATION_ID_KEY);
if (!installationId || typeof installationId !== 'string') {
installationId = UUID.create().toString();
localStorage.setItem(INSTALLATION_ID_KEY, installationId);
}
} catch (error) {
installationId = getFallbackInstallationId();
}

return installationId;
},
// mock implementations
addListener: () => {},
removeListeners: () => {},
} as InstallationIdProvider;
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2918,7 +2918,7 @@
resolved "https://registry.yarnpkg.com/@types/url-parse/-/url-parse-1.4.3.tgz#fba49d90f834951cb000a674efee3d6f20968329"
integrity sha512-4kHAkbV/OfW2kb5BLVUuUMoumB3CP8rHqlw48aHvFy5tf9ER0AfOonBlX29l/DD68G70DmyhRlSYfQPSYpC5Vw==

"@types/uuid-js@^0.7.1":
"@types/uuid-js@^0.7.1", "@types/uuid-js@^0.7.2":
version "0.7.2"
resolved "https://registry.yarnpkg.com/@types/uuid-js/-/uuid-js-0.7.2.tgz#5b5552fcbaaf4acf026fb6dc66f7e5bd6b4be92f"
integrity sha512-9R+mA6mMXkFVQnXEeX5fMQDR2SYND7cafJTqbeMpLhgsL7qr7MF4ZBxWpLexml3lZsBsyAmqVWbOiB0N10m15w==
Expand Down

0 comments on commit 039f06a

Please sign in to comment.