fix get fcm token
This commit is contained in:
parent
c4424434e8
commit
46a8f0051e
@ -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);
|
||||
|
||||
@ -85,6 +85,15 @@
|
||||
<string>no use</string>
|
||||
<key>NSUserNotificationUsageDescription</key>
|
||||
<string>This app needs permission to send push notifications.</string>
|
||||
<key>UIUserNotificationSettings</key>
|
||||
<dict>
|
||||
<key>UIUserNotificationTypeAlert</key>
|
||||
<true/>
|
||||
<key>UIUserNotificationTypeBadge</key>
|
||||
<true/>
|
||||
<key>UIUserNotificationTypeSound</key>
|
||||
<true/>
|
||||
</dict>
|
||||
<key>UIAppFonts</key>
|
||||
<array>
|
||||
<string>arial.ttf</string>
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user