From de23d2277c9d7ff8ef4aff2a99d9d0a2b8a05643 Mon Sep 17 00:00:00 2001 From: kanyanat Date: Wed, 3 Dec 2025 18:15:39 +0700 Subject: [PATCH] add notification service --- src/utils/NotificationService.js | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/utils/NotificationService.js diff --git a/src/utils/NotificationService.js b/src/utils/NotificationService.js new file mode 100644 index 0000000..befba01 --- /dev/null +++ b/src/utils/NotificationService.js @@ -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; \ No newline at end of file