121 lines
3.7 KiB
JavaScript
121 lines
3.7 KiB
JavaScript
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 = {};
|
|
|
|
if (Platform.OS === 'ios') {
|
|
console.log('Getting APNs token first...');
|
|
|
|
// รอให้ระบบพร้อมก่อน
|
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
|
|
// ลองใช้วิธีอื่นในการดึง APNs token
|
|
try {
|
|
const apnsToken = await messaging().getAPNSToken();
|
|
if (apnsToken) {
|
|
tokens.apnsToken = apnsToken;
|
|
console.log('APNs Token received:', apnsToken);
|
|
} else {
|
|
console.log('APNs token is null, continuing without it');
|
|
}
|
|
} catch (apnsError) {
|
|
console.log('APNs token error:', apnsError.message);
|
|
}
|
|
}
|
|
|
|
// ลอง FCM token โดยไม่รอ APNs
|
|
console.log('Getting FCM token...');
|
|
try {
|
|
tokens.fcmToken = await messaging().getToken();
|
|
console.log('FCM Token received:', tokens.fcmToken);
|
|
} catch (fcmError) {
|
|
console.log('FCM token error:', fcmError.message);
|
|
|
|
// ถ้า FCM ล้มเหลว ลองอีกครั้งหลังจากรอ
|
|
console.log('Retrying FCM token after delay...');
|
|
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
try {
|
|
tokens.fcmToken = await messaging().getToken();
|
|
console.log('FCM Token received on retry:', tokens.fcmToken);
|
|
} catch (retryError) {
|
|
console.log('FCM token retry failed:', retryError.message);
|
|
}
|
|
}
|
|
|
|
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; |