react-native-fcm icon indicating copy to clipboard operation
react-native-fcm copied to clipboard

Device Token is null in some devices?

Open woniesong92 opened this issue 8 years ago • 26 comments

Is it possible that device_token can be null in some android devices?

woniesong92 avatar Dec 02 '16 15:12 woniesong92

u need to ask for push permission on android M

sibelius avatar Dec 02 '16 16:12 sibelius

@sibelius can you elaborate that? I thought it should never be null as long as the device can connect to firebase server.

evollu avatar Dec 03 '16 00:12 evollu

U r right, there is not to be with permissions

sibelius avatar Dec 03 '16 08:12 sibelius

@evollu it seems about half the time, the device token is missing. Both on initialize and refreshToken. Have you encountered this before?

woniesong92 avatar Dec 03 '16 13:12 woniesong92

if you are running with android emulator, can you check event log? There should be logs about firebase connection.

evollu avatar Dec 03 '16 21:12 evollu

@evollu I have the same issue here and after some researches I think that Firebase just send the device token in this particular cases:

  1. First time that the app was open on device (just the first time);
  2. Security reasons that made Firebase change the token by it self;
  3. Manually Uninstall/Install app again on device;

But in my case for example, the token was generated on first time that the app was installed, after that, never more! I tried to uninstall/install the app and nothing happends! If anyone solve this problem, please, tell me, cause I have research everywhere and can't find anything.

leandrosimoes avatar Dec 07 '16 13:12 leandrosimoes

I wonder if it is related to the device. Can you try the native sample project from firebase website and see if you still have the issue? If so, fire a bug to them

evollu avatar Dec 07 '16 14:12 evollu

@evollu After try a lot of things to works on my device, I run in my emulator, and works. After that, I run in my device again and (I don't know why) works too and (I don't know why again) still working! ¬¬

I will try the native sample project anyway and I come back here to say what happends.

Tks a lot.

leandrosimoes avatar Dec 07 '16 17:12 leandrosimoes

@woniesong92 is this solved?

sibelius avatar Dec 12 '16 13:12 sibelius

I keep having a null registration id on application first boot. Is there any news about that?

Wasseing avatar Jan 04 '17 13:01 Wasseing

can you try with Firebase native example? If you still see it, fire a bug to them

evollu avatar Jan 04 '17 13:01 evollu

Are you guys sure that you have listened for the refreshToken event? I had the same issue especially when reinstalling the app on my phone because it took longer for getting the token.

So make sure you both of them:

Getting the Firebase token

🚨 Can be unavailable on first load

FCM.getFCMToken().then(token => {
  console.log(token)
})

Listener for Firebase token

FCM.on('refreshToken', (token) => {
  console.log(token)
})

yannickoo avatar Jan 19 '17 14:01 yannickoo

This isn't solved. I haven't had the time to write up a reproduction steps but it seems to depend on the device version too. I made sure to listen to both the first load and the refreshToken.

woniesong92 avatar Jan 19 '17 22:01 woniesong92

I just figure out that in my case the problem is the company proxy server. The proxy was blocking the request to firebase and after I configure the proxy server properly to not block my device requests everything woks fine. I know this is a particular case but maybe can help someone.

I made THIS MODIFICATIONS on my forked version of this repo, this code force the refresh of token on firebase server. I really do not test yet, so if someone wants to implement this and do more tests, just feel free to do that.

leandrosimoes avatar Jan 20 '17 00:01 leandrosimoes

firebase class file

    final Intent intent = new Intent("tokenReceiver");
    final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
    intent.putExtra("token",refreshedToken);
    broadcastManager.sendBroadcast(intent);
    storeToken(refreshedToken);
    //sendRegistrationToServer(refreshedToken);

MainActivity LocalBroadcastManager.getInstance(this).registerReceiver(tokenReceiver, new IntentFilter("tokenReceiver"));

BroadcastReceiver tokenReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { token = intent.getStringExtra("token"); if(token != null) { }

	}
};

This code working fine

ArunkumarKAndanshetti avatar Jun 28 '17 11:06 ArunkumarKAndanshetti

I have the same issue with some device the token is always be null

I can not get token in some devices first time but when I back to prevues page and come back to login I get the token but in some other devices like Samsung Galaxy G5 I'll never get the token and it's always be null

hemedani avatar Jul 14 '17 06:07 hemedani

I solved this problem for my case. But I'm using Swift (iOS). After re-installing app and other actions my token was nil. I needed to wait didRefreshRegistrationToken function. So, I'm ignoring this nil with guard let token = Messaging.messaging().fcmToken else { return } and after this function func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { is getting called. Hope it helps someone

Pulichev avatar Aug 18 '17 20:08 Pulichev

Try to change the device language.It worked for me :)

herou avatar Nov 22 '17 10:11 herou

I closed the emulator and started the app again from Android Studio. Then it invoked onTokenRefresh to get the new token.

MathebulaMahlatse avatar Feb 21 '18 20:02 MathebulaMahlatse

This happens to me quite a lot I have to aggressively retry on both iOS and Android

evanjmg avatar Apr 05 '19 14:04 evanjmg

This happens even now with MAUI Android. Im facing this null value issue recently on some android devices like vivo, samsung Fold & pixel kind of devices.

FYI, I'm generating the token via FirebaseMessagingService's OnNewToken override method.

Is there any workaround ?

GnanaPriyaNamasivayam avatar Mar 14 '23 12:03 GnanaPriyaNamasivayam

This package is deprecated for a long time. You should use the react-native-firebase instead.

leandrosimoes avatar Mar 14 '23 13:03 leandrosimoes

Im using mobile app MAUI. Any suggestions for it?

GnanaPriyaNamasivayam avatar Mar 14 '23 14:03 GnanaPriyaNamasivayam

Do you mean .Net MAUI Framework?

leandrosimoes avatar Mar 14 '23 15:03 leandrosimoes

Yes

GnanaPriyaNamasivayam avatar Mar 14 '23 17:03 GnanaPriyaNamasivayam

And do you know what library, SDK, or plugin are you using to handle Firebase in the MAUI project?

leandrosimoes avatar Mar 14 '23 20:03 leandrosimoes