import { PermissionsAndroid, Platform } from 'react-native'; import NativeGroupLinkSDK from './NativeGrouplinkSdk'; export async function getUserId(): Promise { if (_isAndroid()) { const userID: string = await NativeGroupLinkSDK.getUserIdAndroid(); if (userID !== 'User not registered yet.') { return Promise.resolve(userID); } else { return Promise.reject('UserID not registered yet'); } } else if (_isiOS()) { return NativeGroupLinkSDK.getUserID(); } return Promise.reject('Internal Error'); } /** * @deprecated No longer supported. */ export function setFirebaseToken(token: string) { if (token == null || token === '') { _reportError( 'setFirebaseToken(token: string)', 'Requires a valid Firebase (FCM) token string.' ); return; } if (_isAndroid()) { NativeGroupLinkSDK.setFirebaseToken(token); } } export function startBluetoothIOS() { if (_isiOS()) { NativeGroupLinkSDK.startBluetoothIOS(); } } /** * @deprecated No longer supported. */ export function setDevicePushTokenIOS(token: string) { if (token == null || token === '') { _reportError( 'setDevicePushTokenIOS(token: string)', 'Requires a valid device push token string.' ); return; } if (_isiOS()) { NativeGroupLinkSDK.setDevicePushToken(token); } } export function startLocationIOS() { if (_isiOS()) { NativeGroupLinkSDK.startLocationIOS(); } } export function startGrouplink(appLabel: string, debbugable?: boolean | null) { if (appLabel == null || appLabel === '') { _reportError( 'startGrouplink(appLabel: string, debbugable?: boolean | null)', 'Requires a valid GroupLink token string.' ); return; } const debug = debbugable ?? false; if (_isiOS()) { NativeGroupLinkSDK.startIOS(appLabel); } else if (_isAndroid()) { NativeGroupLinkSDK.startAndroid(appLabel, debug); } } export function startGrouplinkAndroid(appLabel: string, debbugable?: boolean | null) { if (appLabel == null || appLabel === '') { _reportError( 'startGrouplinkAndroid(appLabel: string, debbugable?: boolean | null)', 'Requires a valid GroupLink token string.' ); return; } const debug = debbugable ?? false; if (_isAndroid()) { NativeGroupLinkSDK.startAndroid(appLabel, debug); } } export function startGrouplinkIOS(appLabel: string) { if (appLabel == null || appLabel === '') { _reportError( 'startGrouplinkIOS(appLabel: string)', 'Requires a valid GroupLink token string.' ); return; } if (_isiOS()) { NativeGroupLinkSDK.startIOS(appLabel); } } export function checkBluetoothIOS() { if (_isiOS()) { NativeGroupLinkSDK.checkBluetoothIOS(); } } export async function startBluetoothAndroid() { if (!(Platform.OS === 'android' && Platform.Version >= 31)) return; const bluetoothPermission = await PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN!, PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE!, PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT!, ]); if ( bluetoothPermission['android.permission.BLUETOOTH_SCAN'] === PermissionsAndroid.RESULTS.GRANTED && bluetoothPermission['android.permission.BLUETOOTH_ADVERTISE'] === PermissionsAndroid.RESULTS.GRANTED && bluetoothPermission['android.permission.BLUETOOTH_CONNECT'] === PermissionsAndroid.RESULTS.GRANTED ) { NativeGroupLinkSDK.startAndroid('', false); } } export async function startLocationAndroid() { if (!(Platform.OS === 'android' && Platform.Version >= 23)) return; const locationPermission = await PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION!, PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION!, ]); if ( locationPermission['android.permission.ACCESS_COARSE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED && locationPermission['android.permission.ACCESS_FINE_LOCATION'] === PermissionsAndroid.RESULTS.GRANTED ) { // Permissions granted } } export async function startLocation() { if (_isiOS()) { NativeGroupLinkSDK.startLocationIOS(); } else if (_isAndroid()) { await startLocationAndroid(); } } export async function startBluetooth() { if (_isiOS()) { NativeGroupLinkSDK.startBluetoothIOS(); } else if (_isAndroid()) { await startBluetoothAndroid(); } } export function requestFullIntentPermissionAndroid() { if (_isAndroid()) { NativeGroupLinkSDK.requestFullIntentPermissionAndroid(); } } export function setRemoteId(remoteId: string) { if (remoteId == null || remoteId === '') { _reportError( 'setRemoteId(remoteId: string)', 'Requires a valid remote id string.' ); return; } if (_isiOS()) { NativeGroupLinkSDK.setRemoteIdIOS(remoteId); } if(_isAndroid()){ NativeGroupLinkSDK.setRemoteId(remoteId); } } /** * @deprecated The method should not be used. use setRemoteId() instead */ export function setNotificationNameAndroid(name: string) { if (name == null || name === '') { _reportError( 'setNotificationNameAndroid(name: string)', 'Requires a valid notification name string.' ); return; } if (_isAndroid()) { NativeGroupLinkSDK.setNotificationNameAndroid(name); } } /** * @deprecated No longer supported. */ export async function getNotificationNameAndroid(): Promise { if (_isAndroid()) { return NativeGroupLinkSDK.getNotificationNameAndroid(); } return null; } /** * @deprecated The method should not be used. use setRemoteId() instead */ export function setNotificationNameIOS(name: string) { if (name == null || name === '') { _reportError( 'setNotificationNameIOS(name: string)', 'Requires a valid notification name string.' ); return; } if (_isiOS()) { NativeGroupLinkSDK.setNotificationNameIOS(name); } } /** * @deprecated No longer supported. */ export async function getNotificationNameIOS(): Promise { if (_isiOS()) { return NativeGroupLinkSDK.getNotificationNameIOS(); } return null; } /** * @deprecated The method should not be used. use setRemoteId() instead */ export function setNotificationName(name: string) { if (name == null || name === '') { _reportError( 'setNotificationName(name: string)', 'Requires a valid notification name string.' ); return; } if (_isAndroid()) { NativeGroupLinkSDK.setNotificationNameAndroid(name); } else if (_isiOS()) { NativeGroupLinkSDK.setNotificationNameIOS(name); } } export function requestScheduleExactAlarmPermissionAndroid() { if (_isAndroid()) { NativeGroupLinkSDK.requestScheduleExactAlarm(); } } function _reportError(signature: string, message: string) { const err = new Error(`[GroupLinkSDK] ${signature} → ${message}`); if (err.stack) { const lines = err.stack.split('\n'); err.stack = [lines[0], ...lines.slice(3)].join('\n'); } console.error(err); } function _isAndroid() { return Platform.OS === 'android'; } function _isiOS() { return Platform.OS === 'ios'; }