fix get fcm token
This commit is contained in:
parent
de23d2277c
commit
c4424434e8
110
App.js
110
App.js
@ -41,19 +41,8 @@ class App extends Component {
|
|||||||
SplashScreen.hide()
|
SplashScreen.hide()
|
||||||
Settings.initializeSDK();
|
Settings.initializeSDK();
|
||||||
|
|
||||||
// Test FCM token
|
// ไม่ต้องขอ FCM token ที่นี่ เพราะจะทำใน initNotification แล้ว
|
||||||
try {
|
console.log('App initialized');
|
||||||
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
|
// เพิ่ม method ใหม่สำหรับ setup listeners
|
||||||
@ -110,29 +99,59 @@ class App extends Component {
|
|||||||
|
|
||||||
initNotification = async () => {
|
initNotification = async () => {
|
||||||
try {
|
try {
|
||||||
await this.setPermission()
|
console.log('Starting notification initialization...');
|
||||||
|
|
||||||
|
// ขอ permission ก่อน
|
||||||
|
await this.setPermission();
|
||||||
|
|
||||||
|
// รอให้ APNs token พร้อมสำหรับ iOS
|
||||||
if (Platform.OS === 'ios') {
|
if (Platform.OS === 'ios') {
|
||||||
let apnsToken = await messaging().getAPNSToken();
|
console.log('Waiting for APNs token on iOS...');
|
||||||
|
let apnsToken = null;
|
||||||
let retry = 0;
|
let retry = 0;
|
||||||
while (!apnsToken && retry < 10) {
|
const maxRetries = 20; // เพิ่มจำนวน retry
|
||||||
console.log('Waiting for APNs token...');
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
while (!apnsToken && retry < maxRetries) {
|
||||||
apnsToken = await messaging().getAPNSToken();
|
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);
|
||||||
|
}
|
||||||
retry++;
|
retry++;
|
||||||
}
|
}
|
||||||
console.log('APNs Token:', apnsToken);
|
|
||||||
|
if (apnsToken) {
|
||||||
if (!apnsToken) {
|
console.log('APNs Token received:', apnsToken);
|
||||||
console.log('Failed to get APNs token after retries');
|
} else {
|
||||||
// Decide if we want to return here or try anyway.
|
console.log('Failed to get APNs token after retries, continuing anyway...');
|
||||||
// Usually if APNs is missing, getToken will fail.
|
// ลองต่อไปแม้ไม่ได้ APNs token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ใช้ getToken() แทน getAPNSToken()
|
// ขอ FCM token หลังจากที่ APNs token พร้อมแล้ว
|
||||||
const fcmToken = await messaging().getToken();
|
let fcmToken = null;
|
||||||
console.log('FCM Token:', fcmToken);
|
let fcmRetry = 0;
|
||||||
|
const maxFcmRetries = 5;
|
||||||
|
|
||||||
|
while (!fcmToken && fcmRetry < maxFcmRetries) {
|
||||||
|
try {
|
||||||
|
fcmToken = await messaging().getToken();
|
||||||
|
if (fcmToken) {
|
||||||
|
console.log('FCM Token received:', 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fcmRetry++;
|
||||||
|
}
|
||||||
|
|
||||||
if (fcmToken) {
|
if (fcmToken) {
|
||||||
store.dispatch(appSetPushToken(fcmToken))
|
store.dispatch(appSetPushToken(fcmToken))
|
||||||
@ -141,6 +160,8 @@ class App extends Component {
|
|||||||
if (resultSendDevice.ok && resultSendDevice.data.success) {
|
if (resultSendDevice.ok && resultSendDevice.data.success) {
|
||||||
store.dispatch(appSetDevice(resultSendDevice.data.device))
|
store.dispatch(appSetDevice(resultSendDevice.data.device))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Failed to get FCM token after all retries');
|
||||||
}
|
}
|
||||||
|
|
||||||
// เพิ่ม notification listeners
|
// เพิ่ม notification listeners
|
||||||
@ -153,14 +174,33 @@ class App extends Component {
|
|||||||
|
|
||||||
setPermission = async () => {
|
setPermission = async () => {
|
||||||
try {
|
try {
|
||||||
await messaging().registerDeviceForRemoteMessages();
|
console.log('Setting up permissions...');
|
||||||
const authStatus = await messaging().requestPermission();
|
|
||||||
const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL;
|
// ขอ permission ก่อน
|
||||||
if (!enabled) {
|
const authStatus = await messaging().requestPermission({
|
||||||
await messaging().requestPermission()
|
alert: true,
|
||||||
|
sound: true,
|
||||||
|
badge: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Permission status:', authStatus);
|
||||||
|
|
||||||
|
const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
|
||||||
|
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
|
||||||
|
|
||||||
|
if (enabled) {
|
||||||
|
console.log('Notification permission granted, registering device...');
|
||||||
|
await messaging().registerDeviceForRemoteMessages();
|
||||||
|
|
||||||
|
// สำหรับ iOS ให้รอสักครู่หลัง register
|
||||||
|
if (Platform.OS === 'ios') {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log('Notification permission denied');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('error', error)
|
console.log('setPermission error:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -56,21 +56,28 @@ class LoginScreen extends Component {
|
|||||||
console.log('true')
|
console.log('true')
|
||||||
try {
|
try {
|
||||||
// ใช้ NotificationService
|
// ใช้ NotificationService
|
||||||
|
console.log('Initializing notifications for login...');
|
||||||
const isPermissionGranted = await NotificationService.initialize();
|
const isPermissionGranted = await NotificationService.initialize();
|
||||||
|
|
||||||
if (isPermissionGranted) {
|
if (isPermissionGranted) {
|
||||||
|
console.log('Getting notification tokens...');
|
||||||
const tokens = await NotificationService.getTokens();
|
const tokens = await NotificationService.getTokens();
|
||||||
fcmToken = tokens.fcmToken;
|
fcmToken = tokens.fcmToken;
|
||||||
|
|
||||||
console.log('FCM Token:', fcmToken);
|
if (fcmToken) {
|
||||||
|
console.log('Login FCM Token received:', fcmToken);
|
||||||
|
} else {
|
||||||
|
console.log('No FCM token received during login');
|
||||||
|
}
|
||||||
|
|
||||||
if (tokens.apnsToken) {
|
if (tokens.apnsToken) {
|
||||||
console.log('APNs Token:', tokens.apnsToken);
|
console.log('Login APNs Token received:', tokens.apnsToken);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log('Notification permission denied.');
|
console.log('Notification permission denied during login.');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get notification tokens:', error);
|
console.error('Failed to get notification tokens during login:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -43,15 +43,12 @@ class NotificationService {
|
|||||||
try {
|
try {
|
||||||
const tokens = {};
|
const tokens = {};
|
||||||
|
|
||||||
// Get FCM token
|
// Get APNs token first for iOS
|
||||||
tokens.fcmToken = await messaging().getToken();
|
|
||||||
console.log('FCM Token:', tokens.fcmToken);
|
|
||||||
|
|
||||||
// Get APNs token for iOS
|
|
||||||
if (Platform.OS === 'ios') {
|
if (Platform.OS === 'ios') {
|
||||||
|
console.log('Getting APNs token first...');
|
||||||
let apnsToken = null;
|
let apnsToken = null;
|
||||||
let retry = 0;
|
let retry = 0;
|
||||||
const maxRetries = 15;
|
const maxRetries = 20;
|
||||||
|
|
||||||
while (!apnsToken && retry < maxRetries) {
|
while (!apnsToken && retry < maxRetries) {
|
||||||
try {
|
try {
|
||||||
@ -68,12 +65,36 @@ class NotificationService {
|
|||||||
|
|
||||||
if (apnsToken) {
|
if (apnsToken) {
|
||||||
tokens.apnsToken = apnsToken;
|
tokens.apnsToken = apnsToken;
|
||||||
console.log('APNs Token:', apnsToken);
|
console.log('APNs Token received:', apnsToken);
|
||||||
} else {
|
} else {
|
||||||
console.log('Failed to get APNs token after retries');
|
console.log('Failed to get APNs token after retries');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get FCM token after APNs token is ready
|
||||||
|
let fcmRetry = 0;
|
||||||
|
const maxFcmRetries = 5;
|
||||||
|
|
||||||
|
while (!tokens.fcmToken && fcmRetry < maxFcmRetries) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fcmRetry++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!tokens.fcmToken) {
|
||||||
|
console.log('Failed to get FCM token after all retries');
|
||||||
|
}
|
||||||
|
|
||||||
return tokens;
|
return tokens;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get tokens:', error);
|
console.error('Failed to get tokens:', error);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user