update ios platform push notification
This commit is contained in:
parent
5447279eee
commit
86eec87b8c
58
App.js
58
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 () => {
|
||||
try {
|
||||
await this.setPermission()
|
||||
const fcmToken = await messaging().getAPNSToken();
|
||||
|
||||
// ใช้ 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(' re sult register_device =>', resultSendDevice)
|
||||
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 () => {
|
||||
|
||||
@ -8,12 +8,16 @@
|
||||
#import "AppDelegate.h"
|
||||
|
||||
#import <React/RCTBundleURLProvider.h>
|
||||
#import <UserNotifications/UserNotifications.h>
|
||||
|
||||
#import <Firebase.h>
|
||||
#import "RNSplashScreen.h"
|
||||
|
||||
#import <FBSDKCoreKit/FBSDKCoreKit.h>
|
||||
|
||||
@interface AppDelegate () <UNUserNotificationCenterDelegate>
|
||||
@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
|
||||
|
||||
@ -83,6 +83,8 @@
|
||||
<string>no use</string>
|
||||
<key>NSSpeechRecognitionUsageDescription</key>
|
||||
<string>no use</string>
|
||||
<key>NSUserNotificationUsageDescription</key>
|
||||
<string>This app needs permission to send push notifications.</string>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>arial.ttf</string>
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>aps-environment</key>
|
||||
<string>development</string>
|
||||
<string>production</string>
|
||||
<key>com.apple.developer.applesignin</key>
|
||||
<array>
|
||||
<string>Default</string>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user