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

[expo-notifications] Add private InstallationIdProvider #6749

Merged
merged 3 commits into from
Jan 21, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/bare-expo/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,6 @@ SPEC CHECKSUMS:
Yoga: ba3d99dbee6c15ea6bbe3783d1f0cb1ffb79af0f
ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb

PODFILE CHECKSUM: f8c0544004c7ca9ffcae29a1b9efb91846964d98
PODFILE CHECKSUM: f9b310a7da0a72b720dd3e0eaf31394e24c3f736

COCOAPODS: 1.8.4

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.

2 changes: 1 addition & 1 deletion apps/bare-expo/ios/Pods/Manifest.lock

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());
}
}

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