Skip to content

Commit

Permalink
✨ (getDeviceToken) add getDeviceToken to get token using DeviceCheck …
Browse files Browse the repository at this point in the history
…API (ios only)
  • Loading branch information
AntoineDoubovetzky authored and mikehardy committed Dec 10, 2019
1 parent e1b10e2 commit 8273cad
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Release Notes

- feat: Added getDeviceToken() using DeviceCheck API on iOS 11.0+

## 5.3.1
- types: fix Flow types (thanks @grit96!)

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ Every API returns a Promise but also has a corresponding API with 'Sync' on the
| [getDeviceType()](#getDeviceType) | `string` |||||
| [getDisplay()](#getdisplay) | `Promise<string>` |||||
| [getDeviceName()](#getdevicename) | `Promise<string>` |||||
| [getDeviceToken()](#getdevicetoken) | `Promise<string>` |||||
| [getFirstInstallTime()](#getfirstinstalltime) | `Promise<number>` |||||
| [getFingerprint()](#getfingerprint) | `Promise<string>` |||||
| [getFontScale()](#getfontscale) | `Promise<number>` |||||
Expand Down Expand Up @@ -629,6 +630,21 @@ DeviceInfo.getDeviceName().then(deviceName => {

This used to require the android.permission.BLUETOOTH but the new implementation in v3 does not need it. You may remove that from your AndroidManifest.xml if you had it for this API.

---

### getDeviceToken()

Gets the device token (see [DeviceCheck](https://developer.apple.com/documentation/devicecheck)). Only available for iOS 11.0+.

#### Examples

```js
DeviceInfo.getDeviceToken().then(deviceToken => {
// iOS: "a2Jqsd0kanz..."
});
```


---

### getFirstInstallTime()
Expand Down
1 change: 1 addition & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export default class App extends Component {
deviceJSON.incremental = await DeviceInfo.getIncremental();
deviceJSON.supported32BitAbis = await DeviceInfo.supported32BitAbis();
deviceJSON.supported64BitAbis = await DeviceInfo.supported64BitAbis();
deviceJSON.deviceToken = await DeviceInfo.getDeviceToken();
} catch (e) {
console.log('Trouble getting device info ', e);
}
Expand Down
22 changes: 22 additions & 0 deletions ios/RNDeviceInfo/RNDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import <React/RCTUtils.h>
#import "RNDeviceInfo.h"
#import "DeviceUID.h"
#import <DeviceCheck/DeviceCheck.h>

#if !(TARGET_OS_TV)
#import <WebKit/WebKit.h>
Expand Down Expand Up @@ -375,6 +376,27 @@ - (BOOL) isTablet {
}
}

RCT_EXPORT_METHOD(getDeviceToken:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
if (@available(iOS 11.0, *)) {
DCDevice *device = DCDevice.currentDevice;
if([device isSupported])
{
[DCDevice.currentDevice generateTokenWithCompletionHandler:^(NSData * _Nullable token, NSError * _Nullable error) {
if(error) {
reject(@"ERROR GENERATING TOKEN", error.localizedDescription, error);
}
resolve([token base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]);
}];
}
else {
reject(@"NOT SUPPORTED", @"Device check is not supported by this device", nil);
}
}
else {
reject(@"NOT AVAILABLE", @"Device check is only available for iOS > 11", nil);
}
}

- (float) getFontScale {
// Font scales based on font sizes from https://developer.apple.com/ios/human-interface-guidelines/visual-design/typography/
float fontScale = 1.0;
Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,13 @@ export function getAvailableLocationProvidersSync() {
return {};
}

export async function getDeviceToken() {
if (Platform.OS === 'ios') {
return RNDeviceInfo.getDeviceToken();
}
return 'unknown';
}

const deviceInfoEmitter = new NativeEventEmitter(NativeModules.RNDeviceInfo);
export function useBatteryLevel(): number | null {
const [batteryLevel, setBatteryLevel] = useState<number | null>(null);
Expand Down Expand Up @@ -1322,6 +1329,7 @@ const deviceInfoModule: DeviceInfoModule = {
getDeviceName,
getDeviceNameSync,
getDeviceSync,
getDeviceToken,
getDeviceType,
getDisplay,
getDisplaySync,
Expand Down
1 change: 1 addition & 0 deletions src/internal/privateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ interface ExposedNativeMethods {
getDeviceName: () => Promise<string>;
getDeviceNameSync: () => string;
getDeviceSync: () => string;
getDeviceToken: () => Promise<string>;
getDisplay: () => Promise<string>;
getDisplaySync: () => string;
getFingerprint: () => Promise<string>;
Expand Down

0 comments on commit 8273cad

Please sign in to comment.