update ios platform push notification

This commit is contained in:
kanyanat 2025-11-06 16:26:57 +07:00
parent 5447279eee
commit 86eec87b8c
4 changed files with 87 additions and 12 deletions

74
App.js
View File

@ -36,23 +36,75 @@ class App extends Component {
this._setDataFromInitState = this._setDataFromInitState.bind(this) this._setDataFromInitState = this._setDataFromInitState.bind(this)
} }
componentDidMount = () => { componentDidMount = async () => {
SplashScreen.hide() SplashScreen.hide()
Settings.initializeSDK(); 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 () => { initNotification = async () => {
await this.setPermission() try {
const fcmToken = await messaging().getAPNSToken(); await this.setPermission()
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))
}
}
// ใช้ 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 () => { setPermission = async () => {

View File

@ -8,12 +8,16 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import <React/RCTBundleURLProvider.h> #import <React/RCTBundleURLProvider.h>
#import <UserNotifications/UserNotifications.h>
#import <Firebase.h> #import <Firebase.h>
#import "RNSplashScreen.h" #import "RNSplashScreen.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h> #import <FBSDKCoreKit/FBSDKCoreKit.h>
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
@ -23,6 +27,10 @@
[FIRApp configure]; [FIRApp configure];
} }
// เพิ่ม notification center delegate
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"csareactrn60" moduleName:@"csareactrn60"
@ -68,4 +76,17 @@
#endif #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 @end

View File

@ -83,6 +83,8 @@
<string>no use</string> <string>no use</string>
<key>NSSpeechRecognitionUsageDescription</key> <key>NSSpeechRecognitionUsageDescription</key>
<string>no use</string> <string>no use</string>
<key>NSUserNotificationUsageDescription</key>
<string>This app needs permission to send push notifications.</string>
<key>UIAppFonts</key> <key>UIAppFonts</key>
<array> <array>
<string>arial.ttf</string> <string>arial.ttf</string>

View File

@ -3,7 +3,7 @@
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>aps-environment</key> <key>aps-environment</key>
<string>development</string> <string>production</string>
<key>com.apple.developer.applesignin</key> <key>com.apple.developer.applesignin</key>
<array> <array>
<string>Default</string> <string>Default</string>