Deskripsi: - buat halaman setting - isinya edit profile, ganti tema aplikasi, nonaktifkan notifikasi, sign out No Issues
112 lines
3.1 KiB
TypeScript
112 lines
3.1 KiB
TypeScript
import { getApp, getApps, initializeApp } from '@react-native-firebase/app';
|
|
import {
|
|
getMessaging,
|
|
getToken as getMessagingToken,
|
|
setAutoInitEnabled,
|
|
} from '@react-native-firebase/messaging';
|
|
import * as Notifications from 'expo-notifications';
|
|
import { useEffect } from 'react';
|
|
import { Linking, PermissionsAndroid, Platform } from 'react-native';
|
|
import { ConstEnv } from '@/constants/ConstEnv';
|
|
|
|
const RNfirebaseConfig = {
|
|
apiKey: ConstEnv.firebase.apiKey,
|
|
authDomain: ConstEnv.firebase.authDomain,
|
|
projectId: ConstEnv.firebase.projectId,
|
|
storageBucket: ConstEnv.firebase.storageBucket,
|
|
messagingSenderId: ConstEnv.firebase.messagingSenderId,
|
|
appId: ConstEnv.firebase.appId,
|
|
databaseURL: ConstEnv.firebase.databaseURL
|
|
};
|
|
|
|
const initializeFirebase = async () => {
|
|
try {
|
|
let app;
|
|
const apps = getApps();
|
|
if (apps.length) {
|
|
app = getApp() as any;
|
|
} else {
|
|
app = initializeApp(RNfirebaseConfig) as any;
|
|
}
|
|
const mess = getMessaging(app);
|
|
await setAutoInitEnabled(mess, true);
|
|
|
|
return mess;
|
|
} catch (error) {
|
|
console.error('Failed to initialize Firebase:', error);
|
|
}
|
|
};
|
|
|
|
export const checkPermission = async () => {
|
|
try {
|
|
if (Platform.OS === 'android') {
|
|
return await PermissionsAndroid.check(
|
|
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
|
);
|
|
} else if (Platform.OS === 'ios') {
|
|
const { status } = await Notifications.getPermissionsAsync();
|
|
return status === 'granted';
|
|
}
|
|
} catch (err) {
|
|
console.warn('Error checking notification permissions:', err);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export const openSettings = () => {
|
|
if (Platform.OS === 'ios') {
|
|
Linking.openURL('app-settings:');
|
|
} else {
|
|
Linking.openSettings();
|
|
}
|
|
};
|
|
|
|
export const requestPermission = async () => {
|
|
try {
|
|
if (Platform.OS === 'android') {
|
|
const cek = await PermissionsAndroid.check(
|
|
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
|
);
|
|
if (!cek) {
|
|
const granted = await PermissionsAndroid.request(
|
|
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
|
);
|
|
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
|
}
|
|
return true;
|
|
} else if (Platform.OS === 'ios') {
|
|
const { status } = await Notifications.requestPermissionsAsync();
|
|
return status === 'granted';
|
|
}
|
|
} catch (err) {
|
|
console.warn('Error requesting notification permissions:', err);
|
|
}
|
|
};
|
|
|
|
export const getToken = async () => {
|
|
try {
|
|
const mess = await initializeFirebase();
|
|
if (!mess) return null;
|
|
|
|
// pakai modular API
|
|
const token = await getMessagingToken(mess);
|
|
return token;
|
|
} catch (error) {
|
|
console.error('Error getting token:', error);
|
|
}
|
|
};
|
|
|
|
export const useNotification = () => {
|
|
useEffect(() => {
|
|
const initializeAndSetup = async () => {
|
|
try {
|
|
await getToken();
|
|
} catch (error) {
|
|
console.error('Failed to setup notifications:', error);
|
|
}
|
|
};
|
|
|
|
initializeAndSetup();
|
|
}, []);
|
|
};
|