fix push notification with @notifee/react-native lib

This commit is contained in:
kanyanat 2025-12-03 18:12:54 +07:00
parent 6fd1ac61a2
commit 7e8ddbb1be
2 changed files with 37 additions and 40 deletions

View File

@ -12,6 +12,7 @@
#import <Firebase.h> #import <Firebase.h>
#import "RNSplashScreen.h" #import "RNSplashScreen.h"
#import <RNNotifee/RNNotifee.h>
#import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKCoreKit/FBSDKCoreKit.h>
@ -27,6 +28,11 @@
[FIRApp configure]; [FIRApp configure];
} }
// ตั้งค่า Notifee delegate ก่อน
[RNNotifee onBackgroundEvent:^(NSDictionary * _Nonnull event) {
// Handle background events
}];
// เพิ่ม notification center delegate // เพิ่ม notification center delegate
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self; center.delegate = self;
@ -81,23 +87,31 @@
willPresentNotification:(UNNotification *)notification willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// ถ้าเป็น Remote Notification (จาก FCM) ให้ซ่อนไว้ (None) เพื่อไม่ให้ซ้ำกับที่ Notifee จะแสดง // ให้ Notifee จัดการ notification ก่อน
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { if ([RNNotifee willPresentNotification:notification withCompletionHandler:completionHandler]) {
completionHandler(UNNotificationPresentationOptionNone); return;
} else {
// ถ้าเป็น Local Notification (จาก Notifee) ให้แสดงผล (Alert/Sound/Badge)
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
} }
// ถ้า Notifee ไม่จัดการ ให้แสดงแบบปกติ
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound);
} }
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler { withCompletionHandler:(void (^)(void))completionHandler {
// ให้ Notifee จัดการ response ก่อน
if ([RNNotifee didReceiveNotificationResponse:response withCompletionHandler:completionHandler]) {
return;
}
completionHandler(); completionHandler();
} }
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// ส่ง token ให้ทั้ง Firebase และ Notifee
[FIRMessaging messaging].APNSToken = deviceToken; [FIRMessaging messaging].APNSToken = deviceToken;
[RNNotifee didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
} }
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

View File

@ -16,6 +16,7 @@ import IndicatorLoading from '../../components/IndicatorLoading';
import { locale, t } from '../../utils/i18n' import { locale, t } from '../../utils/i18n'
import moment from "moment"; import moment from "moment";
import messaging from "@react-native-firebase/messaging"; import messaging from "@react-native-firebase/messaging";
import NotificationService from '../../utils/NotificationService';
class LoginScreen extends Component { class LoginScreen extends Component {
@ -36,7 +37,11 @@ class LoginScreen extends Component {
async componentDidMount() { async componentDidMount() {
await messaging().deleteToken() await messaging().deleteToken()
console.log('deletetoken'); console.log('deletetoken');
// Initialize notification service
await NotificationService.initialize();
await NotificationService.createNotificationChannel();
await NotificationService.setupBackgroundHandler();
} }
async _login() { async _login() {
@ -50,45 +55,24 @@ class LoginScreen extends Component {
if (regex.test(this.state.username) === true) { if (regex.test(this.state.username) === true) {
console.log('true') console.log('true')
try { try {
const authStatus = await messaging().requestPermission({ // ใช้ NotificationService
alert: true, const isPermissionGranted = await NotificationService.initialize();
sound: true,
badge: true, if (isPermissionGranted) {
}); const tokens = await NotificationService.getTokens();
fcmToken = tokens.fcmToken;
console.log('authStatus >>> ', authStatus)
const enabled = console.log('FCM Token:', fcmToken);
authStatus === messaging.AuthorizationStatus.AUTHORIZED || if (tokens.apnsToken) {
authStatus === messaging.AuthorizationStatus.PROVISIONAL; console.log('APNs Token:', tokens.apnsToken);
if (enabled) {
console.log('Notification permission granted.');
await messaging().registerDeviceForRemoteMessages();
if (Platform.OS === 'ios') {
let apnsToken = await messaging().getAPNSToken();
let retry = 0;
while (!apnsToken && retry < 10) {
console.log('Waiting for APNs token...');
await new Promise(resolve => setTimeout(resolve, 1000));
apnsToken = await messaging().getAPNSToken();
retry++;
}
console.log('APNs Token:', apnsToken);
} }
fcmToken = await messaging().getToken();
console.log('fcmToken >>>> ', fcmToken)
} else { } else {
console.log('Notification permission denied.'); console.log('Notification permission denied.');
} }
} catch (error) { } catch (error) {
console.error('Failed to request notification permission:', error); console.error('Failed to get notification tokens:', error);
} }
// const fcmToken = await messaging().getAPNSToken()
console.log('await messaging().getAPNSToken() <<<<<< ', fcmToken)
let params = { let params = {
// mobile: this.state.username, // mobile: this.state.username,
@ -98,7 +82,6 @@ class LoginScreen extends Component {
} }
console.log('params >> ', params) console.log('params >> ', params)
// console.log('fcmToken >>>>>>>>>>>>>>>>>>>>>>>>> ', fcmToken)
this.setState({ isLoading: true }) this.setState({ isLoading: true })
login(params) login(params)
.then((res) => { .then((res) => {