evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 682 forks source link

fcm token not available on first load react native (android) #1021

Closed roeib closed 6 years ago

roeib commented 6 years ago
"react-native": "0.55.4",
"react-native-fcm": "^14.1.3",
All Android  devices

after install the apk the app dont get fcm token only after close the app and open again everything is fine.

FCM.getFCMToken().then(token => { console.log("TOKEN (getFCMToken)", token); //send to the server });

FCM.on('FCMTokenRefreshed', token => {
  console.log("tokewn", token);
      //send to the server
});

please help :)

roeib commented 6 years ago

@evollu

evollu commented 6 years ago

do you have same issue in debug mode?

roeib commented 6 years ago

@evollu on the emulator i dont have this problem

Seemapadiya commented 6 years ago

@evollu in release build getting same issues after install the apk the app dont get token it giving null and then after relaunch the app getting token is there any other thing to do or am i doing wrong any thing, because in debug build its working perfectly fine. Please help

evollu commented 6 years ago

it takes a while to get firebase token. i wonder if it could be a firebase bug

Seemapadiya commented 6 years ago

Okk but now what should I do to handle this condition , can you please help me out

evollu commented 6 years ago

listen on FCM.on('FCMTokenRefreshed', token => { and call FCM.getFcmToken on app start. The token should be there eventually. You can save the token in storage for future compare to see if it actually changed

Seemapadiya commented 6 years ago

Ok i will try it and let you know if its works or not, Thank you for help

Seemapadiya commented 6 years ago

@evollu Thank you very much, its working fine in release build.

Udbhav12 commented 6 years ago

@Seemapadiya I am facing the same issue. Could you please share code snippet?

Seemapadiya commented 6 years ago

@Udbhav12 previously i was used this method on my app start and its working fine in debug FCM.getFCMToken().then(token => { console.log("FCM >>>token>>" + token) }); For the release build issues i have to implement this method on app start and saved token in storage FCM.on('FCMTokenRefreshed', token => { console.log('FCMTokenRefreshed',token); }); if we didn't find token in FCM.on method but i have always get token in this method FCM.on('FCMTokenRefreshed', token => {})

havinhthai commented 6 years ago

@Seemapadiya Did you save parameter token in FCM.on('FCMTokenRefreshed', token => {}) and FCM.getFCMToken() to store?

Example:

FCM.on('FCMTokenRefreshed', token => {
    AsyncStorage.setItem('NOTIFICATION_TOKEN', token);
});

FCM.getFCMToken().then(token => {
   AsyncStorage.setItem('NOTIFICATION_TOKEN', token);
});
Seemapadiya commented 6 years ago

Yes direct store in to my model class

sarathbabutemburu commented 5 years ago

@evollu please help me i am not getting FCM token in release apk below is my code

FCM.on('FCMTokenRefreshed', token => {});

componentDidMount() { FCM.getFCMToken().then(token => { console.log("TOKEN (getFCMToken)", token); this.props.savePushNotificationToken(token) }); }