diff --git a/ios/csareactrn60/AppDelegate.mm b/ios/csareactrn60/AppDelegate.mm index 13c9dec..c5ef37a 100644 --- a/ios/csareactrn60/AppDelegate.mm +++ b/ios/csareactrn60/AppDelegate.mm @@ -36,6 +36,9 @@ // เพิ่ม notification center delegate UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; + + // ขอ permission สำหรับ push notification + [application registerForRemoteNotifications]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge @@ -109,11 +112,16 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response } - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { + NSLog(@"APNs device token received: %@", deviceToken); // ส่ง token ให้ทั้ง Firebase และ Notifee [FIRMessaging messaging].APNSToken = deviceToken; [RNNotifee didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } +- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { + NSLog(@"Failed to register for remote notifications: %@", error); +} + - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; completionHandler(UIBackgroundFetchResultNoData); diff --git a/ios/csareactrn60/Info.plist b/ios/csareactrn60/Info.plist index 48a5bb3..43f612b 100644 --- a/ios/csareactrn60/Info.plist +++ b/ios/csareactrn60/Info.plist @@ -85,6 +85,15 @@ no use NSUserNotificationUsageDescription This app needs permission to send push notifications. + UIUserNotificationSettings + + UIUserNotificationTypeAlert + + UIUserNotificationTypeBadge + + UIUserNotificationTypeSound + + UIAppFonts arial.ttf diff --git a/src/screens/login/Login.js b/src/screens/login/Login.js index 2778b72..15ad8ed 100644 --- a/src/screens/login/Login.js +++ b/src/screens/login/Login.js @@ -17,6 +17,7 @@ import { locale, t } from '../../utils/i18n' import moment from "moment"; import messaging from "@react-native-firebase/messaging"; import NotificationService from '../../utils/NotificationService'; +import { testTokens } from '../../utils/TokenTest'; class LoginScreen extends Component { @@ -38,6 +39,9 @@ class LoginScreen extends Component { await messaging().deleteToken() console.log('deletetoken'); + // Test tokens + await testTokens(); + // Initialize notification service await NotificationService.initialize(); await NotificationService.createNotificationChannel(); diff --git a/src/utils/NotificationService.js b/src/utils/NotificationService.js index 97116c6..fe95906 100644 --- a/src/utils/NotificationService.js +++ b/src/utils/NotificationService.js @@ -43,56 +43,43 @@ class NotificationService { try { const tokens = {}; - // Get APNs token first for iOS if (Platform.OS === 'ios') { console.log('Getting APNs token first...'); - let apnsToken = null; - let retry = 0; - const maxRetries = 20; - while (!apnsToken && retry < maxRetries) { - try { - apnsToken = await messaging().getAPNSToken(); - if (!apnsToken) { - console.log(`Waiting for APNs token... (${retry + 1}/${maxRetries})`); - await new Promise(resolve => setTimeout(resolve, 2000)); - } - } catch (error) { - console.log('Error getting APNs token:', error); + // รอให้ระบบพร้อมก่อน + await new Promise(resolve => setTimeout(resolve, 3000)); + + // ลองใช้วิธีอื่นในการดึง APNs token + try { + const apnsToken = await messaging().getAPNSToken(); + if (apnsToken) { + tokens.apnsToken = apnsToken; + console.log('APNs Token received:', apnsToken); + } else { + console.log('APNs token is null, continuing without it'); } - retry++; - } - - if (apnsToken) { - tokens.apnsToken = apnsToken; - console.log('APNs Token received:', apnsToken); - } else { - console.log('Failed to get APNs token after retries'); + } catch (apnsError) { + console.log('APNs token error:', apnsError.message); } } - // Get FCM token after APNs token is ready - let fcmRetry = 0; - const maxFcmRetries = 5; - - while (!tokens.fcmToken && fcmRetry < maxFcmRetries) { + // ลอง FCM token โดยไม่รอ APNs + console.log('Getting FCM token...'); + try { + tokens.fcmToken = await messaging().getToken(); + console.log('FCM Token received:', tokens.fcmToken); + } catch (fcmError) { + console.log('FCM token error:', fcmError.message); + + // ถ้า FCM ล้มเหลว ลองอีกครั้งหลังจากรอ + console.log('Retrying FCM token after delay...'); + await new Promise(resolve => setTimeout(resolve, 5000)); try { tokens.fcmToken = await messaging().getToken(); - if (tokens.fcmToken) { - console.log('FCM Token received:', tokens.fcmToken); - break; - } - } catch (error) { - console.log(`FCM token error (${fcmRetry + 1}/${maxFcmRetries}):`, error.message); - if (fcmRetry < maxFcmRetries - 1) { - await new Promise(resolve => setTimeout(resolve, 3000)); - } + console.log('FCM Token received on retry:', tokens.fcmToken); + } catch (retryError) { + console.log('FCM token retry failed:', retryError.message); } - fcmRetry++; - } - - if (!tokens.fcmToken) { - console.log('Failed to get FCM token after all retries'); } return tokens;