fix get fcm token
This commit is contained in:
parent
c4424434e8
commit
46a8f0051e
@ -36,6 +36,9 @@
|
|||||||
// เพิ่ม notification center delegate
|
// เพิ่ม notification center delegate
|
||||||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
||||||
center.delegate = self;
|
center.delegate = self;
|
||||||
|
|
||||||
|
// ขอ permission สำหรับ push notification
|
||||||
|
[application registerForRemoteNotifications];
|
||||||
|
|
||||||
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
|
||||||
@ -109,11 +112,16 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
||||||
|
NSLog(@"APNs device token received: %@", deviceToken);
|
||||||
// ส่ง token ให้ทั้ง Firebase และ Notifee
|
// ส่ง token ให้ทั้ง Firebase และ Notifee
|
||||||
[FIRMessaging messaging].APNSToken = deviceToken;
|
[FIRMessaging messaging].APNSToken = deviceToken;
|
||||||
[RNNotifee didRegisterForRemoteNotificationsWithDeviceToken: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 {
|
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
||||||
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
|
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
|
||||||
completionHandler(UIBackgroundFetchResultNoData);
|
completionHandler(UIBackgroundFetchResultNoData);
|
||||||
|
|||||||
@ -85,6 +85,15 @@
|
|||||||
<string>no use</string>
|
<string>no use</string>
|
||||||
<key>NSUserNotificationUsageDescription</key>
|
<key>NSUserNotificationUsageDescription</key>
|
||||||
<string>This app needs permission to send push notifications.</string>
|
<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>
|
<key>UIAppFonts</key>
|
||||||
<array>
|
<array>
|
||||||
<string>arial.ttf</string>
|
<string>arial.ttf</string>
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import { locale, t } from '../../utils/i18n'
|
|||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import messaging from "@react-native-firebase/messaging";
|
import messaging from "@react-native-firebase/messaging";
|
||||||
import NotificationService from '../../utils/NotificationService';
|
import NotificationService from '../../utils/NotificationService';
|
||||||
|
import { testTokens } from '../../utils/TokenTest';
|
||||||
|
|
||||||
class LoginScreen extends Component {
|
class LoginScreen extends Component {
|
||||||
|
|
||||||
@ -38,6 +39,9 @@ class LoginScreen extends Component {
|
|||||||
await messaging().deleteToken()
|
await messaging().deleteToken()
|
||||||
console.log('deletetoken');
|
console.log('deletetoken');
|
||||||
|
|
||||||
|
// Test tokens
|
||||||
|
await testTokens();
|
||||||
|
|
||||||
// Initialize notification service
|
// Initialize notification service
|
||||||
await NotificationService.initialize();
|
await NotificationService.initialize();
|
||||||
await NotificationService.createNotificationChannel();
|
await NotificationService.createNotificationChannel();
|
||||||
|
|||||||
@ -43,56 +43,43 @@ class NotificationService {
|
|||||||
try {
|
try {
|
||||||
const tokens = {};
|
const tokens = {};
|
||||||
|
|
||||||
// Get APNs token first for iOS
|
|
||||||
if (Platform.OS === 'ios') {
|
if (Platform.OS === 'ios') {
|
||||||
console.log('Getting APNs token first...');
|
console.log('Getting APNs token first...');
|
||||||
let apnsToken = null;
|
|
||||||
let retry = 0;
|
|
||||||
const maxRetries = 20;
|
|
||||||
|
|
||||||
while (!apnsToken && retry < maxRetries) {
|
// รอให้ระบบพร้อมก่อน
|
||||||
try {
|
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||||
apnsToken = await messaging().getAPNSToken();
|
|
||||||
if (!apnsToken) {
|
// ลองใช้วิธีอื่นในการดึง APNs token
|
||||||
console.log(`Waiting for APNs token... (${retry + 1}/${maxRetries})`);
|
try {
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
const apnsToken = await messaging().getAPNSToken();
|
||||||
}
|
if (apnsToken) {
|
||||||
} catch (error) {
|
tokens.apnsToken = apnsToken;
|
||||||
console.log('Error getting APNs token:', error);
|
console.log('APNs Token received:', apnsToken);
|
||||||
|
} else {
|
||||||
|
console.log('APNs token is null, continuing without it');
|
||||||
}
|
}
|
||||||
retry++;
|
} catch (apnsError) {
|
||||||
}
|
console.log('APNs token error:', apnsError.message);
|
||||||
|
|
||||||
if (apnsToken) {
|
|
||||||
tokens.apnsToken = apnsToken;
|
|
||||||
console.log('APNs Token received:', apnsToken);
|
|
||||||
} else {
|
|
||||||
console.log('Failed to get APNs token after retries');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get FCM token after APNs token is ready
|
// ลอง FCM token โดยไม่รอ APNs
|
||||||
let fcmRetry = 0;
|
console.log('Getting FCM token...');
|
||||||
const maxFcmRetries = 5;
|
try {
|
||||||
|
tokens.fcmToken = await messaging().getToken();
|
||||||
while (!tokens.fcmToken && fcmRetry < maxFcmRetries) {
|
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 {
|
try {
|
||||||
tokens.fcmToken = await messaging().getToken();
|
tokens.fcmToken = await messaging().getToken();
|
||||||
if (tokens.fcmToken) {
|
console.log('FCM Token received on retry:', tokens.fcmToken);
|
||||||
console.log('FCM Token received:', tokens.fcmToken);
|
} catch (retryError) {
|
||||||
break;
|
console.log('FCM token retry failed:', retryError.message);
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`FCM token error (${fcmRetry + 1}/${maxFcmRetries}):`, error.message);
|
|
||||||
if (fcmRetry < maxFcmRetries - 1) {
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fcmRetry++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tokens.fcmToken) {
|
|
||||||
console.log('Failed to get FCM token after all retries');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user