From a7128e69d98db1835d4c98125b055d31904180d5 Mon Sep 17 00:00:00 2001 From: kanyanat Date: Wed, 3 Dec 2025 13:39:00 +0700 Subject: [PATCH] add @notifee/react-native lib for push noti foreground --- App.js | 26 ++++++++++++++++++++++++++ ios/csareactrn60/AppDelegate.mm | 9 ++++++++- package.json | 1 + yarn.lock | 5 +++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/App.js b/App.js index adcf05e..3cba2ba 100644 --- a/App.js +++ b/App.js @@ -10,6 +10,7 @@ import React, { Component } from 'react' import { StatusBar, StyleSheet, Text, TouchableOpacity, Platform } from 'react-native' import MainNav from './src/navigation/MainNav' import messaging from "@react-native-firebase/messaging" +import notifee from '@notifee/react-native'; import { persistor, store } from './src/redux/store' import { Provider } from 'react-redux' import { registerDevice } from './src/api/UserApi' @@ -60,6 +61,31 @@ class App extends Component { // Foreground message handler messaging().onMessage(async remoteMessage => { console.log('Foreground notification:', remoteMessage); + + // Create a channel (required for Android) + const channelId = await notifee.createChannel({ + id: 'default', + name: 'Default Channel', + }); + + // Display a notification + await notifee.displayNotification({ + title: remoteMessage.notification?.title || 'New Notification', + body: remoteMessage.notification?.body || '', + android: { + channelId, + pressAction: { + id: 'default', + }, + }, + ios: { + foregroundPresentationOptions: { + banner: true, + badge: true, + sound: true, + }, + }, + }); }); // Background/Quit message handler diff --git a/ios/csareactrn60/AppDelegate.mm b/ios/csareactrn60/AppDelegate.mm index 2a92ecd..fcd0910 100644 --- a/ios/csareactrn60/AppDelegate.mm +++ b/ios/csareactrn60/AppDelegate.mm @@ -80,7 +80,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { - completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound); + + // ถ้าเป็น Remote Notification (จาก FCM) ให้ซ่อนไว้ (None) เพื่อไม่ให้ซ้ำกับที่ Notifee จะแสดง + if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { + completionHandler(UNNotificationPresentationOptionNone); + } else { + // ถ้าเป็น Local Notification (จาก Notifee) ให้แสดงผล (Alert/Sound/Badge) + completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound); + } } - (void)userNotificationCenter:(UNUserNotificationCenter *)center diff --git a/package.json b/package.json index bc0f8fc..a0116b9 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "dependencies": { "@invertase/react-native-apple-authentication": "^1.1.2", + "@notifee/react-native": "^9.1.8", "@react-native-async-storage/async-storage": "^2.1.2", "@react-native-camera-roll/camera-roll": "^7.10.0", "@react-native-community/datetimepicker": "^3.0.2", diff --git a/yarn.lock b/yarn.lock index 2402f83..4156eee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1500,6 +1500,11 @@ "@nodelib/fs.scandir" "2.1.5" fastq "^1.6.0" +"@notifee/react-native@^9.1.8": + version "9.1.8" + resolved "https://registry.yarnpkg.com/@notifee/react-native/-/react-native-9.1.8.tgz#3d55cb3fbcc21f9e35931e366afdf64b294da891" + integrity sha512-Az/dueoPerJsbbjRxu8a558wKY+gONUrfoy3Hs++5OqbeMsR0dYe6P+4oN6twrLFyzAhEA1tEoZRvQTFDRmvQg== + "@react-native-async-storage/async-storage@^1.13.4": version "1.24.0" resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.24.0.tgz#888efbc62a26f7d9464b32f4d3027b7f2771999b"