From 73b6338ff57a8bce356944056443c7ad563f9510 Mon Sep 17 00:00:00 2001 From: balzack Date: Tue, 8 Nov 2022 23:25:55 -0800 Subject: [PATCH] passing device token to server --- app/mobile/index.js | 21 ++----------------- .../ios/Databag.xcodeproj/project.pbxproj | 2 ++ app/mobile/ios/Podfile.lock | 6 ++++++ app/mobile/package.json | 1 + app/mobile/src/api/setAccountAccess.js | 5 +++-- app/mobile/src/api/setLogin.js | 5 +++-- app/mobile/src/context/useAppContext.hook.js | 13 +++++++++--- app/mobile/yarn.lock | 5 +++++ 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/app/mobile/index.js b/app/mobile/index.js index 1378f753..87db9ee8 100644 --- a/app/mobile/index.js +++ b/app/mobile/index.js @@ -1,27 +1,10 @@ import 'react-native-gesture-handler'; import { registerRootComponent } from 'expo'; - import App from './App'; - import messaging from '@react-native-firebase/messaging'; - -fetch(`https://balzack.coredb.org/account/flag/DEXPO`, { method: 'POST' } ).then(() => { console.log("FETCHED") }); - -console.log("REGISTER"); -messaging().registerDeviceForRemoteMessages().then(() => { -console.log("TOKEN!"); - fetch(`https://balzack.coredb.org/account/flag/REGISTERED`, { method: 'POST' } ); - messaging().getToken().then(token => { - fetch(`https://balzack.coredb.org/account/flag/TOKEN?topic=${token}`, { method: 'POST' } ); - }) -}); - -messaging().setBackgroundMessageHandler(async remoteMessage => { -fetch(`https://balzack.coredb.org/account/flag/BACKGROUND?topic=${JSON.stringify(remoteMessage)}`, { method: 'POST' } ); - console.log('Message handled in the background!', remoteMessage); -});fetch(`https://balzack.coredb.org/account/flag/DEXPO`, { method: 'POST' } ); - +messaging().registerDeviceForRemoteMessages(); +messaging().setBackgroundMessageHandler(async remoteMessage => {}); // registerRootComponent calls AppRegistry.registerComponent('main', () => App); // It also ensures that whether you load the app in Expo Go or in a native build, diff --git a/app/mobile/ios/Databag.xcodeproj/project.pbxproj b/app/mobile/ios/Databag.xcodeproj/project.pbxproj index 8bac4fd6..8d493de1 100644 --- a/app/mobile/ios/Databag.xcodeproj/project.pbxproj +++ b/app/mobile/ios/Databag.xcodeproj/project.pbxproj @@ -293,6 +293,7 @@ "${BUILT_PRODUCTS_DIR}/RCT-Folly/folly.framework", "${BUILT_PRODUCTS_DIR}/RCTTypeSafety/RCTTypeSafety.framework", "${BUILT_PRODUCTS_DIR}/RNCClipboard/RNCClipboard.framework", + "${BUILT_PRODUCTS_DIR}/RNDeviceInfo/RNDeviceInfo.framework", "${BUILT_PRODUCTS_DIR}/RNGestureHandler/RNGestureHandler.framework", "${BUILT_PRODUCTS_DIR}/RNImageCropPicker/RNImageCropPicker.framework", "${BUILT_PRODUCTS_DIR}/RNReanimated/RNReanimated.framework", @@ -340,6 +341,7 @@ "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/folly.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RCTTypeSafety.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RNCClipboard.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RNDeviceInfo.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RNGestureHandler.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RNImageCropPicker.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RNReanimated.framework", diff --git a/app/mobile/ios/Podfile.lock b/app/mobile/ios/Podfile.lock index 03345bb3..2e908c83 100644 --- a/app/mobile/ios/Podfile.lock +++ b/app/mobile/ios/Podfile.lock @@ -410,6 +410,8 @@ PODS: - ReactCommon/turbomodule/core (= 0.69.5) - RNCClipboard (1.11.1): - React-Core + - RNDeviceInfo (10.3.0): + - React-Core - RNFBApp (16.4.3): - Firebase/CoreOnly (= 10.1.0) - React-Core @@ -511,6 +513,7 @@ DEPENDENCIES: - React-runtimeexecutor (from `../node_modules/react-native/ReactCommon/runtimeexecutor`) - ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`) - "RNCClipboard (from `../node_modules/@react-native-clipboard/clipboard`)" + - RNDeviceInfo (from `../node_modules/react-native-device-info`) - "RNFBApp (from `../node_modules/@react-native-firebase/app`)" - "RNFBMessaging (from `../node_modules/@react-native-firebase/messaging`)" - RNGestureHandler (from `../node_modules/react-native-gesture-handler`) @@ -626,6 +629,8 @@ EXTERNAL SOURCES: :path: "../node_modules/react-native/ReactCommon" RNCClipboard: :path: "../node_modules/@react-native-clipboard/clipboard" + RNDeviceInfo: + :path: "../node_modules/react-native-device-info" RNFBApp: :path: "../node_modules/@react-native-firebase/app" RNFBMessaging: @@ -700,6 +705,7 @@ SPEC CHECKSUMS: React-runtimeexecutor: c778439c3c430a5719d027d3c67423b390a221fe ReactCommon: ab1003b81be740fecd82509c370a45b1a7dda0c1 RNCClipboard: 2834e1c4af68697089cdd455ee4a4cdd198fa7dd + RNDeviceInfo: 4701f0bf2a06b34654745053db0ce4cb0c53ada7 RNFBApp: e2157f61e4769b53641df8948c534580630cb50a RNFBMessaging: 00da9a5a7136caa61a7b75d87e1dd83864251136 RNGestureHandler: 7673697e7c0e9391adefae4faa087442bc04af33 diff --git a/app/mobile/package.json b/app/mobile/package.json index 2e5af782..9627f4c1 100644 --- a/app/mobile/package.json +++ b/app/mobile/package.json @@ -28,6 +28,7 @@ "react-dom": "18.0.0", "react-native": "0.69.5", "react-native-base64": "^0.2.1", + "react-native-device-info": "^10.3.0", "react-native-document-picker": "^8.1.1", "react-native-gesture-handler": "^2.7.0", "react-native-image-crop-picker": "^0.38.0", diff --git a/app/mobile/src/api/setAccountAccess.js b/app/mobile/src/api/setAccountAccess.js index 8582cdf8..a3cca7fb 100644 --- a/app/mobile/src/api/setAccountAccess.js +++ b/app/mobile/src/api/setAccountAccess.js @@ -1,8 +1,9 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; -export async function setAccountAccess(server, token) { +export async function setAccountAccess(server, token, notifications) { let app = { Name: "indicom", Description: "decentralized communication" } - let access = await fetchWithTimeout(`https://${server}/account/access?token=${token}`, { method: 'PUT', body: JSON.stringify(app) }) + let types = encodeURIComponent(JSON.stringify(notifications)); + let access = await fetchWithTimeout(`https://${server}/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}¬ifications=${types}`, { method: 'PUT', body: JSON.stringify(app) }) checkResponse(access) return await access.json() } diff --git a/app/mobile/src/api/setLogin.js b/app/mobile/src/api/setLogin.js index 074f60c8..1c20fca1 100644 --- a/app/mobile/src/api/setLogin.js +++ b/app/mobile/src/api/setLogin.js @@ -1,11 +1,12 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; import base64 from 'react-native-base64' -export async function setLogin(username, server, password) { +export async function setLogin(username, server, password, appName, appVersion, platform, deviceToken, notifications) { let headers = new Headers() headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); + let types = encodeURIComponent(JSON.stringify(notifications)); let app = { Name: "topics", Description: "decentralized communication" } - let login = await fetchWithTimeout(`https://${server}/account/apps`, { method: 'POST', body: JSON.stringify(app), headers: headers }) + let login = await fetchWithTimeout(`https://${server}/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}¬ifications=${types}`, { method: 'POST', body: JSON.stringify(app), headers: headers }) checkResponse(login) return await login.json() } diff --git a/app/mobile/src/context/useAppContext.hook.js b/app/mobile/src/context/useAppContext.hook.js index 774ac13e..e6718017 100644 --- a/app/mobile/src/context/useAppContext.hook.js +++ b/app/mobile/src/context/useAppContext.hook.js @@ -10,12 +10,15 @@ import { AccountContext } from 'context/AccountContext'; import { ProfileContext } from 'context/ProfileContext'; import { CardContext } from 'context/CardContext'; import { ChannelContext } from 'context/ChannelContext'; +import { getVersion, getApplicationName, getDeviceId } from 'react-native-device-info'; +import messaging from '@react-native-firebase/messaging'; export function useAppContext() { const [state, setState] = useState({ session: null, loginTimestamp: null, disconnected: null, + deviceToken: null, }); const store = useContext(StoreContext); const account = useContext(AccountContext); @@ -31,6 +34,10 @@ export function useAppContext() { } useEffect(() => { + messaging().getToken().then(token => { + updateState({ deviceToken: token }); + }) + init(); }, []); @@ -68,18 +75,18 @@ export function useAppContext() { username: getUsername, create: async (server, username, password, token) => { await addAccount(server, username, password, token); - const access = await setLogin(username, server, password) + const access = await setLogin(username, server, password, getApplicatioName(), getVersion(), getDeviceId(), state.deviceToken, ['contact', 'channel']) await store.actions.setSession({ ...access, server}); await setSession({ ...access, server }); }, access: async (server, token) => { - const access = await setAccountAccess(server, token); + const access = await setAccountAccess(server, token, getApplicationName(), getVersion(), getDeviceId(), state.deviceToken, ['contact', 'channel']); await store.actions.setSession({ ...access, server}); await setSession({ ...access, server }); }, login: async (username, password) => { const acc = username.split('@'); - const access = await setLogin(acc[0], acc[1], password) + const access = await setLogin(acc[0], acc[1], password, getApplicationName(), getVersion(), getDeviceId(), state.deviceToken, ['contact', 'channel']) await store.actions.setSession({ ...access, server: acc[1]}); await setSession({ ...access, server: acc[1] }); }, diff --git a/app/mobile/yarn.lock b/app/mobile/yarn.lock index cf591235..2957e6f3 100644 --- a/app/mobile/yarn.lock +++ b/app/mobile/yarn.lock @@ -5706,6 +5706,11 @@ react-native-codegen@^0.69.2: jscodeshift "^0.13.1" nullthrows "^1.1.1" +react-native-device-info@^10.3.0: + version "10.3.0" + resolved "https://registry.yarnpkg.com/react-native-device-info/-/react-native-device-info-10.3.0.tgz#6bab64d84d3415dd00cc446c73ec5e2e61fddbe7" + integrity sha512-/ziZN1sA1REbJTv5mQZ4tXggcTvSbct+u5kCaze8BmN//lbxcTvWsU6NQd4IihLt89VkbX+14IGc9sVApSxd/w== + react-native-document-picker@^8.1.1: version "8.1.1" resolved "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-8.1.1.tgz"