109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import messaging from '@react-native-firebase/messaging';
|
|
import notifee, { AndroidImportance } from '@notifee/react-native';
|
|
import axios from 'axios';
|
|
import { apiCall } from '../ApiHelper';
|
|
import { REMOVE_FCM_TOKEN, STORE_FCM_TOKEN } from '../ApiUrls';
|
|
|
|
// replace with your backend
|
|
|
|
class FirebaseService {
|
|
// 🔹 Request notification permission
|
|
static async requestPermission() {
|
|
await messaging().requestPermission();
|
|
await notifee.requestPermission();
|
|
}
|
|
|
|
// 🔹 Display local notification
|
|
static async displayNotification(title: string, body: string) {
|
|
await notifee.displayNotification({
|
|
title,
|
|
body,
|
|
android: {
|
|
channelId: 'default',
|
|
smallIcon: 'ic_launcher', // ensure you have this icon in android/app/src/main/res/mipmap
|
|
importance: AndroidImportance.HIGH,
|
|
pressAction: {
|
|
id: 'default',
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
// 🔹 Get & send token to backend
|
|
static async getFcmToken(userId: string) {
|
|
console.log('FCM Token o');
|
|
try {
|
|
console.log('FCM Token 1');
|
|
const token = await messaging().getToken();
|
|
console.log('FCM Token 2');
|
|
console.log('FCM Token login: ', token);
|
|
await apiCall(STORE_FCM_TOKEN+token, 'POST');
|
|
console.log('FCM Token sent to backend');
|
|
return token;
|
|
} catch (error) {
|
|
console.error('Error getting FCM token:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 🔹 Delete token from backend
|
|
static async deleteFcmToken() {
|
|
try {
|
|
console.log('FCM Token 1');
|
|
const token = await messaging().getToken();
|
|
console.log('FCM Token 2');
|
|
console.log("fcm token logout -",token)
|
|
await apiCall(REMOVE_FCM_TOKEN+token, 'POST');
|
|
console.log('FCM Token deleted from backend');
|
|
} catch (error) {
|
|
console.error('Error deleting FCM token:', error);
|
|
}
|
|
}
|
|
|
|
// 🔹 Foreground notifications
|
|
static foregroundListener() {
|
|
return messaging().onMessage(async remoteMessage => {
|
|
console.log('Foreground message:', remoteMessage);
|
|
if (remoteMessage.notification) {
|
|
await this.displayNotification(
|
|
remoteMessage.notification.title ?? 'New Message',
|
|
remoteMessage.notification.body ?? ''
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 🔹 Background & Quit state
|
|
static backgroundHandler() {
|
|
messaging().setBackgroundMessageHandler(async remoteMessage => {
|
|
console.log('Background message:', remoteMessage);
|
|
if (remoteMessage.notification) {
|
|
await this.displayNotification(
|
|
remoteMessage.notification.title ?? 'New Message',
|
|
remoteMessage.notification.body ?? ''
|
|
);
|
|
}
|
|
});
|
|
|
|
messaging().onNotificationOpenedApp(remoteMessage => {
|
|
console.log('App opened from background:', remoteMessage);
|
|
});
|
|
|
|
messaging().getInitialNotification().then(remoteMessage => {
|
|
if (remoteMessage) {
|
|
console.log('App opened from quit state:', remoteMessage);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 🔹 Token auto-refresh
|
|
static tokenRefreshHandler() {
|
|
return messaging().onTokenRefresh(async token => {
|
|
console.log('New refreshed FCM Token:', token);
|
|
// 👉 send to backend
|
|
});
|
|
}
|
|
}
|
|
|
|
export default FirebaseService;
|