You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have launched my application using @react-native-firebase/messaging and I'm in some tricky situation now.
My app saves fcm tokens in database to target specific users and send notifications to them. And it saves tokens into database when user opens the app, to ensure that every user has their own token (prepare for any issue such as a network error).
What is wierd, is that about 11,000 users have registered in my app, but about 3,000 users does not have their token. There is no database issues, and this randomly happens even on exactly same device (regardless of whether it is iOS or Android). I'm quite sure that there won't be as many as 3,000 people who denied permission or who deleted my app.
I wonder if anyone else had the same problem as me.
Below is my simplified codes where using messaging().getToken(). Is there any problem in my code?
(+) I was using version ^11.5.0, and I updated it into ^12.0.0 in last week.
export constPushHandler: React.FC=()=>{constappState=useRef(AppState.currentState)const[hasPermission,setHasPermission]=useState<FirebaseMessagingTypes.AuthorizationStatus>()useEffect(()=>{// Check AppState, and when app has come to the foreground, check permissionconsthandleAppStateChange=(nextAppState: AppStateStatus)=>{if(appState.current.match(/inactive|background/)&&nextAppState==='active'){messaging().hasPermission().then(authStatus=>setHasPermission(authStatus))}appState.current=nextAppState}AppState.addEventListener('change',handleAppStateChange)returnAppState.removeEventListener('change',handleAppStateChange)},[])useEffect(()=>{handleTokenPermission()},[])useEffect(()=>{if(hasPermission===messaging.AuthorizationStatus.AUTHORIZED||hasPermission===messaging.AuthorizationStatus.PROVISIONAL){saveDeviceToken()}},[hasPermission])consthandleTokenPermission=async()=>{try{// Check whether permission allowedconstauthStatus=awaitmessaging().hasPermission()constenabled=authStatus===messaging.AuthorizationStatus.AUTHORIZED||authStatus===messaging.AuthorizationStatus.PROVISIONALif(enabled){setHasPermission(authStatus)}else{// if permission already has been denied, move to settingsif(authStatus===messaging.AuthorizationStatus.DENIED){Alert.alert('check permission',undefined,[{text: 'cancel'},{text: 'settings',onPress: openSettings},])}// else, request permissionelse{constauthorized=awaitmessaging().requestPermission()setHasPermission(authorized)}}}catch(error){console.error(error)}}constsaveDeviceToken=async()=>{try{messaging().getToken().then(asyncfcmToken=>{console.log('PUSH TOKEN: ',fcmToken)awaitsaveTokenToDatabase({token: fcmToken})// This is the function I save fcm token into my database})returnmessaging().onTokenRefresh(asynctoken=>{awaitsaveTokenToDatabase({ token })})}catch(error){console.error(error)}}returnnull}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have launched my application using
@react-native-firebase/messaging
and I'm in some tricky situation now.My app saves fcm tokens in database to target specific users and send notifications to them. And it saves tokens into database when user opens the app, to ensure that every user has their own token (prepare for any issue such as a network error).
What is wierd, is that about 11,000 users have registered in my app, but about 3,000 users does not have their token. There is no database issues, and this randomly happens even on exactly same device (regardless of whether it is iOS or Android). I'm quite sure that there won't be as many as 3,000 people who denied permission or who deleted my app.
I wonder if anyone else had the same problem as me.
Below is my simplified codes where using
messaging().getToken()
. Is there any problem in my code?(+) I was using version
^11.5.0
, and I updated it into^12.0.0
in last week.(++) On other devices that successfully saved their token in database, everything works perfectly.
App.tsx
PushHandler.tsx
Beta Was this translation helpful? Give feedback.
All reactions