add notification service
This commit is contained in:
parent
7e8ddbb1be
commit
de23d2277c
113
src/utils/NotificationService.js
Normal file
113
src/utils/NotificationService.js
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
import messaging from '@react-native-firebase/messaging';
|
||||||
|
import notifee from '@notifee/react-native';
|
||||||
|
import { Platform } from 'react-native';
|
||||||
|
|
||||||
|
class NotificationService {
|
||||||
|
|
||||||
|
static async initialize() {
|
||||||
|
try {
|
||||||
|
// Request permissions
|
||||||
|
const authStatus = await messaging().requestPermission({
|
||||||
|
alert: true,
|
||||||
|
sound: true,
|
||||||
|
badge: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const enabled =
|
||||||
|
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
|
||||||
|
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
console.log('Notification permission granted.');
|
||||||
|
|
||||||
|
// Register for remote messages
|
||||||
|
await messaging().registerDeviceForRemoteMessages();
|
||||||
|
|
||||||
|
// Request Notifee permissions for iOS
|
||||||
|
if (Platform.OS === 'ios') {
|
||||||
|
await notifee.requestPermission();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
console.log('Notification permission denied.');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize notifications:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getTokens() {
|
||||||
|
try {
|
||||||
|
const tokens = {};
|
||||||
|
|
||||||
|
// Get FCM token
|
||||||
|
tokens.fcmToken = await messaging().getToken();
|
||||||
|
console.log('FCM Token:', tokens.fcmToken);
|
||||||
|
|
||||||
|
// Get APNs token for iOS
|
||||||
|
if (Platform.OS === 'ios') {
|
||||||
|
let apnsToken = null;
|
||||||
|
let retry = 0;
|
||||||
|
const maxRetries = 15;
|
||||||
|
|
||||||
|
while (!apnsToken && retry < maxRetries) {
|
||||||
|
try {
|
||||||
|
apnsToken = await messaging().getAPNSToken();
|
||||||
|
if (!apnsToken) {
|
||||||
|
console.log(`Waiting for APNs token... (${retry + 1}/${maxRetries})`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Error getting APNs token:', error);
|
||||||
|
}
|
||||||
|
retry++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (apnsToken) {
|
||||||
|
tokens.apnsToken = apnsToken;
|
||||||
|
console.log('APNs Token:', apnsToken);
|
||||||
|
} else {
|
||||||
|
console.log('Failed to get APNs token after retries');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to get tokens:', error);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async setupBackgroundHandler() {
|
||||||
|
// Handle background messages
|
||||||
|
messaging().setBackgroundMessageHandler(async remoteMessage => {
|
||||||
|
console.log('Message handled in the background!', remoteMessage);
|
||||||
|
|
||||||
|
// Display notification using Notifee
|
||||||
|
await notifee.displayNotification({
|
||||||
|
title: remoteMessage.notification?.title || 'New Message',
|
||||||
|
body: remoteMessage.notification?.body || 'You have a new message',
|
||||||
|
data: remoteMessage.data,
|
||||||
|
android: {
|
||||||
|
channelId: 'default',
|
||||||
|
smallIcon: 'ic_launcher',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createNotificationChannel() {
|
||||||
|
if (Platform.OS === 'android') {
|
||||||
|
await notifee.createChannel({
|
||||||
|
id: 'default',
|
||||||
|
name: 'Default Channel',
|
||||||
|
importance: 4, // High importance
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default NotificationService;
|
||||||
Loading…
Reference in New Issue
Block a user