From 22d7d36d24b6e884896f23221da7bda4d1db36cd Mon Sep 17 00:00:00 2001 From: Kamogelo Moeketse Date: Thu, 4 Jul 2024 01:23:48 +0200 Subject: [PATCH 1/2] added notification setup files --- frontend/occupi-mobile4/.gitignore | 2 + .../.gradle/8.1.1/checksums/checksums.lock | Bin 0 -> 17 bytes .../dependencies-accessors.lock | Bin 0 -> 17 bytes .../dependencies-accessors/gc.properties | 0 .../.gradle/8.1.1/fileChanges/last-build.bin | Bin 0 -> 1 bytes .../.gradle/8.1.1/fileHashes/fileHashes.lock | Bin 0 -> 17 bytes .../.gradle/8.1.1/gc.properties | 0 .../buildOutputCleanup.lock | Bin 0 -> 17 bytes .../buildOutputCleanup/cache.properties | 2 + .../buildOutputCleanup/outputFiles.bin | Bin 0 -> 18749 bytes .../.gradle/vcs-1/gc.properties | 0 frontend/occupi-mobile4/app.json | 3 +- frontend/occupi-mobile4/app/_layout.tsx | 1 + frontend/occupi-mobile4/app/build.gradle.kts | 22 +++ frontend/occupi-mobile4/app/notiftester.tsx | 7 + frontend/occupi-mobile4/build.gradle.kts | 7 + frontend/occupi-mobile4/package-lock.json | 34 +++++ frontend/occupi-mobile4/package.json | 3 +- .../screens/Login/SplashScreen.tsx | 2 +- .../screens/Settings/NotifTester.tsx | 131 ++++++++++++++++++ 20 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/checksums/checksums.lock create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/dependencies-accessors/dependencies-accessors.lock create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/dependencies-accessors/gc.properties create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/fileChanges/last-build.bin create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/fileHashes/fileHashes.lock create mode 100644 frontend/occupi-mobile4/.gradle/8.1.1/gc.properties create mode 100644 frontend/occupi-mobile4/.gradle/buildOutputCleanup/buildOutputCleanup.lock create mode 100644 frontend/occupi-mobile4/.gradle/buildOutputCleanup/cache.properties create mode 100644 frontend/occupi-mobile4/.gradle/buildOutputCleanup/outputFiles.bin create mode 100644 frontend/occupi-mobile4/.gradle/vcs-1/gc.properties create mode 100644 frontend/occupi-mobile4/app/build.gradle.kts create mode 100644 frontend/occupi-mobile4/app/notiftester.tsx create mode 100644 frontend/occupi-mobile4/build.gradle.kts create mode 100644 frontend/occupi-mobile4/screens/Settings/NotifTester.tsx diff --git a/frontend/occupi-mobile4/.gitignore b/frontend/occupi-mobile4/.gitignore index d3e535a6..b68c06ef 100644 --- a/frontend/occupi-mobile4/.gitignore +++ b/frontend/occupi-mobile4/.gitignore @@ -12,6 +12,8 @@ web-build/ eas.json app.json +occupi-4d02a-firebase-adminsdk-4ea8s-545f5460aa.json +google-services.json # macOS .DS_Store diff --git a/frontend/occupi-mobile4/.gradle/8.1.1/checksums/checksums.lock b/frontend/occupi-mobile4/.gradle/8.1.1/checksums/checksums.lock new file mode 100644 index 0000000000000000000000000000000000000000..ba1fe25879abc98718d0a2479cde92aa8f5a368c GIT binary patch literal 17 TcmZQRu&Do>rhs-A0~7!NF)jnK literal 0 HcmV?d00001 diff --git a/frontend/occupi-mobile4/.gradle/8.1.1/dependencies-accessors/dependencies-accessors.lock b/frontend/occupi-mobile4/.gradle/8.1.1/dependencies-accessors/dependencies-accessors.lock new file mode 100644 index 0000000000000000000000000000000000000000..97885e53c270e486a7c59ac56830e1382caea0eb GIT binary patch literal 17 TcmZQx;krDiTxPL0&sA+N{0~7!NHyH#( literal 0 HcmV?d00001 diff --git a/frontend/occupi-mobile4/.gradle/8.1.1/gc.properties b/frontend/occupi-mobile4/.gradle/8.1.1/gc.properties new file mode 100644 index 00000000..e69de29b diff --git a/frontend/occupi-mobile4/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/frontend/occupi-mobile4/.gradle/buildOutputCleanup/buildOutputCleanup.lock new file mode 100644 index 0000000000000000000000000000000000000000..efd5aa3b034b2053789c1f8876890135b49527b6 GIT binary patch literal 17 UcmZRMoU*UO^pnMX1_%%Y05--1h5!Hn literal 0 HcmV?d00001 diff --git a/frontend/occupi-mobile4/.gradle/buildOutputCleanup/cache.properties b/frontend/occupi-mobile4/.gradle/buildOutputCleanup/cache.properties new file mode 100644 index 00000000..c0066b57 --- /dev/null +++ b/frontend/occupi-mobile4/.gradle/buildOutputCleanup/cache.properties @@ -0,0 +1,2 @@ +#Thu Jul 04 00:58:00 SAST 2024 +gradle.version=8.1.1 diff --git a/frontend/occupi-mobile4/.gradle/buildOutputCleanup/outputFiles.bin b/frontend/occupi-mobile4/.gradle/buildOutputCleanup/outputFiles.bin new file mode 100644 index 0000000000000000000000000000000000000000..0de379d8d954522500448a7248a89c40e70f4d9b GIT binary patch literal 18749 zcmeI%y)Oe{9Ki7_>QE`wpf%J>BnBPYG|?~+38AxyRxBM%H3{iZ7DI!@AeLGT28ISP z7%JMyONhijU|?a87%Cm>ntOg4X>10O?+#Nq7L2UXvdeibfw&$c};s>h|j*9OA=rL}d{N2Tk& zqFVK|bX&($OZsqOS@j3$&O}dcxc7KY^{n*1%T(7*&B}@D^U_B$@$vSeL)QI} z9@&}Q8nCsw&F4e9^yt0cwVHhms_y>k`5}utkvUbpU;5&0-O)w9(5cpamM(Wqvqa4~ zt_UE200IagfB*srAbh-0 + ); diff --git a/frontend/occupi-mobile4/app/build.gradle.kts b/frontend/occupi-mobile4/app/build.gradle.kts new file mode 100644 index 00000000..9711936a --- /dev/null +++ b/frontend/occupi-mobile4/app/build.gradle.kts @@ -0,0 +1,22 @@ +plugins { + id("com.android.application") + + // Add the Google services Gradle plugin + id("com.google.gms.google-services") + + ... +} + +dependencies { + // Import the Firebase BoM + implementation(platform("com.google.firebase:firebase-bom:33.1.1")) + + + // TODO: Add the dependencies for Firebase products you want to use + // When using the BoM, don't specify versions in Firebase dependencies + implementation("com.google.firebase:firebase-analytics") + + + // Add the dependencies for any other desired Firebase products + // https://firebase.google.com/docs/android/setup#available-libraries +} \ No newline at end of file diff --git a/frontend/occupi-mobile4/app/notiftester.tsx b/frontend/occupi-mobile4/app/notiftester.tsx new file mode 100644 index 00000000..a4309ff1 --- /dev/null +++ b/frontend/occupi-mobile4/app/notiftester.tsx @@ -0,0 +1,7 @@ +import NotifTester from "../screens/Settings/NotifTester"; + +export default function Home() { + return ( + + ); +} diff --git a/frontend/occupi-mobile4/build.gradle.kts b/frontend/occupi-mobile4/build.gradle.kts new file mode 100644 index 00000000..a1f19aa9 --- /dev/null +++ b/frontend/occupi-mobile4/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + // ... + + // Add the dependency for the Google services Gradle plugin + id("com.google.gms.google-services") version "4.4.2" apply false + +} \ No newline at end of file diff --git a/frontend/occupi-mobile4/package-lock.json b/frontend/occupi-mobile4/package-lock.json index 5d9cd007..393ddead 100644 --- a/frontend/occupi-mobile4/package-lock.json +++ b/frontend/occupi-mobile4/package-lock.json @@ -25,6 +25,7 @@ "expo": "~51.0.14", "expo-blur": "^13.0.2", "expo-constants": "~16.0.2", + "expo-device": "~6.0.2", "expo-file-system": "^17.0.1", "expo-font": "~12.0.7", "expo-linear-gradient": "^13.0.2", @@ -12393,6 +12394,39 @@ "expo": "*" } }, + "node_modules/expo-device": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/expo-device/-/expo-device-6.0.2.tgz", + "integrity": "sha512-sCt91CuTmAuMXX4SlFOn4lIos2UIr8vb0jDstDDZXys6kErcj0uynC7bQAMreU5uRUTKMAl4MAMpKt9ufCXPBw==", + "dependencies": { + "ua-parser-js": "^0.7.33" + }, + "peerDependencies": { + "expo": "*" + } + }, + "node_modules/expo-device/node_modules/ua-parser-js": { + "version": "0.7.38", + "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-0.7.38.tgz", + "integrity": "sha512-fYmIy7fKTSFAhG3fuPlubeGaMoAd6r0rSnfEsO5nEY55i26KSLt9EH7PLQiiqPUhNqYIJvSkTy1oArIcXAbPbA==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/ua-parser-js" + }, + { + "type": "paypal", + "url": "https://paypal.me/faisalman" + }, + { + "type": "github", + "url": "https://github.com/sponsors/faisalman" + } + ], + "engines": { + "node": "*" + } + }, "node_modules/expo-file-system": { "version": "17.0.1", "resolved": "https://registry.npmjs.org/expo-file-system/-/expo-file-system-17.0.1.tgz", diff --git a/frontend/occupi-mobile4/package.json b/frontend/occupi-mobile4/package.json index 4e416484..376f072e 100644 --- a/frontend/occupi-mobile4/package.json +++ b/frontend/occupi-mobile4/package.json @@ -69,7 +69,8 @@ "react-native-svg": "^15.3.0", "react-native-web": "~0.19.10", "zod": "^3.23.8", - "expo-notifications": "~0.28.9" + "expo-notifications": "~0.28.9", + "expo-device": "~6.0.2" }, "devDependencies": { "@babel/core": "^7.20.0", diff --git a/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx b/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx index 3e0c538a..701545a4 100644 --- a/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx +++ b/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx @@ -93,7 +93,7 @@ export default function SplashScreen() { useEffect(() => { const timer = setTimeout(() => { setSelectedIndex(1); // Assuming Onboarding1 is at index 1 - router.replace('/settings'); // Navigate to Onboarding1 screen + router.replace('/notiftester'); // Navigate to Onboarding1 screen }, 5000); // 8 seconds return () => clearTimeout(timer); // Clean up timer on component unmount diff --git a/frontend/occupi-mobile4/screens/Settings/NotifTester.tsx b/frontend/occupi-mobile4/screens/Settings/NotifTester.tsx new file mode 100644 index 00000000..ed623297 --- /dev/null +++ b/frontend/occupi-mobile4/screens/Settings/NotifTester.tsx @@ -0,0 +1,131 @@ +import { useState, useEffect, useRef } from 'react'; +import { Text, View, Button, Platform } from 'react-native'; +import * as Device from 'expo-device'; +import * as Notifications from 'expo-notifications'; +import Constants from 'expo-constants'; + + +Notifications.setNotificationHandler({ + handleNotification: async () => ({ + shouldShowAlert: true, + shouldPlaySound: false, + shouldSetBadge: false, + }), +}); + + + +async function sendPushNotification(expoPushToken: string) { + const message = { + to: expoPushToken, + sound: 'default', + title: 'Original Title', + body: 'And here is the body!', + data: { someData: 'goes here' }, + }; + + await fetch('https://exp.host/--/api/v2/push/send', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Accept-encoding': 'gzip, deflate', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(message), + }); +} + + +function handleRegistrationError(errorMessage: string) { + alert(errorMessage); + throw new Error(errorMessage); +} + +async function registerForPushNotificationsAsync() { + if (Platform.OS === 'android') { + Notifications.setNotificationChannelAsync('default', { + name: 'default', + importance: Notifications.AndroidImportance.MAX, + vibrationPattern: [0, 250, 250, 250], + lightColor: '#FF231F7C', + }); + } + + if (Device.isDevice) { + const { status: existingStatus } = await Notifications.getPermissionsAsync(); + let finalStatus = existingStatus; + if (existingStatus !== 'granted') { + const { status } = await Notifications.requestPermissionsAsync(); + finalStatus = status; + } + if (finalStatus !== 'granted') { + handleRegistrationError('Permission not granted to get push token for push notification!'); + return; + } + const projectId = + Constants?.expoConfig?.extra?.eas?.projectId ?? Constants?.easConfig?.projectId; + if (!projectId) { + handleRegistrationError('Project ID not found'); + } + try { + const pushTokenString = ( + await Notifications.getExpoPushTokenAsync({ + projectId, + }) + ).data; + console.log(pushTokenString); + return pushTokenString; + } catch (e: unknown) { + handleRegistrationError(`${e}`); + } + } else { + handleRegistrationError('Must use physical device for push notifications'); + } +} + +export default function NotifTester() { + const [expoPushToken, setExpoPushToken] = useState(''); + const [notification, setNotification] = useState( + undefined + ); + const notificationListener = useRef(); + const responseListener = useRef(); + + useEffect(() => { + registerForPushNotificationsAsync() + .then(token => setExpoPushToken(token ?? '')) + .catch((error: any) => setExpoPushToken(`${error}`)); + + notificationListener.current = Notifications.addNotificationReceivedListener(notification => { + setNotification(notification); + }); + + responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { + console.log(response); + }); + + return () => { + notificationListener.current && + Notifications.removeNotificationSubscription(notificationListener.current); + responseListener.current && + Notifications.removeNotificationSubscription(responseListener.current); + }; + }, []); + + return ( + + Your Expo push token: {expoPushToken} + + Title: {notification && notification.request.content.title} + Body: {notification && notification.request.content.body} + Data: {notification && JSON.stringify(notification.request.content.data)} + +