From 86eec87b8cebbee4d8cbc8fae06c06bf4f94a6ea Mon Sep 17 00:00:00 2001 From: kanyanat Date: Thu, 6 Nov 2025 16:26:57 +0700 Subject: [PATCH] update ios platform push notification --- App.js | 74 ++++++++++++++++++---- ios/csareactrn60/AppDelegate.mm | 21 ++++++ ios/csareactrn60/Info.plist | 2 + ios/csareactrn60/csareactrn60.entitlements | 2 +- 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/App.js b/App.js index 581a8e6..012d24a 100644 --- a/App.js +++ b/App.js @@ -36,23 +36,75 @@ class App extends Component { this._setDataFromInitState = this._setDataFromInitState.bind(this) } - componentDidMount = () => { + componentDidMount = async () => { SplashScreen.hide() Settings.initializeSDK(); + + // Test FCM token + try { + const token = await messaging().getToken(); + console.log('Current FCM Token:', token); + console.log('Token length:', token ? token.length : 0); + + // ตรวจสอบ permission status + const authStatus = await messaging().requestPermission(); + console.log('Permission status:', authStatus); + + } catch (error) { + console.log('Error getting FCM token:', error); + } + } + + // เพิ่ม method ใหม่สำหรับ setup listeners + setupNotificationListeners = () => { + // Foreground message handler + messaging().onMessage(async remoteMessage => { + console.log('Foreground notification:', remoteMessage); + }); + + // Background/Quit message handler + messaging().setBackgroundMessageHandler(async remoteMessage => { + console.log('Background notification:', remoteMessage); + }); + + // Notification opened app handler + messaging().onNotificationOpenedApp(remoteMessage => { + console.log('Notification opened app:', remoteMessage); + }); + + // Check if app was opened from notification + messaging() + .getInitialNotification() + .then(remoteMessage => { + if (remoteMessage) { + console.log('App opened from notification:', remoteMessage); + } + }); } initNotification = async () => { - await this.setPermission() - const fcmToken = await messaging().getAPNSToken(); - if (fcmToken) { - store.dispatch(appSetPushToken(fcmToken)) - const resultSendDevice = await registerDevice(fcmToken) - console.log(' re sult register_device =>', resultSendDevice) - if (resultSendDevice.ok && resultSendDevice.data.success) { - store.dispatch(appSetDevice(resultSendDevice.data.device)) - } - } + try { + await this.setPermission() + // ใช้ getToken() แทน getAPNSToken() + const fcmToken = await messaging().getToken(); + console.log('FCM Token:', fcmToken); + + if (fcmToken) { + store.dispatch(appSetPushToken(fcmToken)) + const resultSendDevice = await registerDevice(fcmToken) + console.log('register_device result =>', resultSendDevice) + if (resultSendDevice.ok && resultSendDevice.data.success) { + store.dispatch(appSetDevice(resultSendDevice.data.device)) + } + } + + // เพิ่ม notification listeners + this.setupNotificationListeners(); + + } catch (error) { + console.log('initNotification error:', error); + } } setPermission = async () => { diff --git a/ios/csareactrn60/AppDelegate.mm b/ios/csareactrn60/AppDelegate.mm index e40cfb2..3704cd3 100644 --- a/ios/csareactrn60/AppDelegate.mm +++ b/ios/csareactrn60/AppDelegate.mm @@ -8,12 +8,16 @@ #import "AppDelegate.h" #import +#import #import #import "RNSplashScreen.h" #import +@interface AppDelegate () +@end + @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions @@ -23,6 +27,10 @@ [FIRApp configure]; } + // เพิ่ม notification center delegate + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; + center.delegate = self; + RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"csareactrn60" @@ -68,4 +76,17 @@ #endif } +// เพิ่ม methods สำหรับ handle notifications +- (void)userNotificationCenter:(UNUserNotificationCenter *)center + willPresentNotification:(UNNotification *)notification + withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { + completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound); +} + +- (void)userNotificationCenter:(UNUserNotificationCenter *)center +didReceiveNotificationResponse:(UNNotificationResponse *)response + withCompletionHandler:(void (^)(void))completionHandler { + completionHandler(); +} + @end diff --git a/ios/csareactrn60/Info.plist b/ios/csareactrn60/Info.plist index f76acbf..8d55624 100644 --- a/ios/csareactrn60/Info.plist +++ b/ios/csareactrn60/Info.plist @@ -83,6 +83,8 @@ no use NSSpeechRecognitionUsageDescription no use + NSUserNotificationUsageDescription + This app needs permission to send push notifications. UIAppFonts arial.ttf diff --git a/ios/csareactrn60/csareactrn60.entitlements b/ios/csareactrn60/csareactrn60.entitlements index 80b5221..15268e6 100644 --- a/ios/csareactrn60/csareactrn60.entitlements +++ b/ios/csareactrn60/csareactrn60.entitlements @@ -3,7 +3,7 @@ aps-environment - development + production com.apple.developer.applesignin Default