Skip to content

Commit

Permalink
Merge pull request #92 from FYP-2024-IQMA/SCRUM-139-Implement-notific…
Browse files Browse the repository at this point in the history
…ation-in-mobile-app

Scrum 139 implement notification in mobile app
  • Loading branch information
tanyipeng834 authored Oct 17, 2024
2 parents 53db5ac + 26629ff commit 844f924
Show file tree
Hide file tree
Showing 3 changed files with 20,479 additions and 19,689 deletions.
37 changes: 35 additions & 2 deletions frontend/iQMA-Skills-Builder/context/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {User, useAuth0} from 'react-native-auth0';
import {createContext, useEffect, useState} from 'react';

import AsyncStorage from '@react-native-async-storage/async-storage';
import {router} from 'expo-router';
import messaging from '@react-native-firebase/messaging';
import { AppRegistry, PermissionsAndroid } from 'react-native';

export const AuthContext = createContext<any>(null);

Expand All @@ -12,9 +13,38 @@ export const AuthProvider = ({children}: {children: React.ReactNode}) => {
const [token, setToken] = useState<string | null>(null); // Store Access Token of current User
const [isLoading, setIsLoading] = useState(true);

messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
});

const requestUserPermission = async () => {
try {
const permission = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
);
if (permission === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Notification permission granted");
}
else {
console.log("Notification permission denied");
}
} catch (e) {
console.error(e);
}
}

const getToken = async () => {
const token = await messaging().getToken();
console.log("Token = ", token);
}

useEffect(() => {
watchUserSession();
requestUserPermission();
getToken();
})

useEffect(() => {
watchUserSession();
// checkFirstLogin();
}, [user]);

Expand Down Expand Up @@ -71,6 +101,9 @@ export const AuthProvider = ({children}: {children: React.ReactNode}) => {
const logIn = async () => {
try {
await authorize();
AppRegistry.registerHeadlessTask('ReactNativeFirebaseMessagingHeadlessTask', () => async (remoteMessage) => {
console.log('Message handled in the background (Headless Task):', remoteMessage);
});
} catch (e) {
console.log(e);
}
Expand Down
Loading

0 comments on commit 844f924

Please sign in to comment.