Notifications (troubles after building and running natively)

#1

Hi!

First off, absolutely love the work you’ve done with Expo! Helped me a lot along the way!

Now, I have have an issue I haven’t been able to figure out or found enough information about. It has to do with push notifications and the “DeviceNotRegistered” error. I’ll explain two scenarios I’ve used to handle notification tokens for the user and then explain what I’ve experienced.

Scenario 1

A) On app launch, the app asks for notification permissions. If “granted”, generates token and saves to my backend and associates it with the user. It also saves the token to the SecureStore.
B) The app first checks the SecureStore for a push token. If present, doesn’t generate a new token but continues (the token is already associated with the user in my backend)

I implemented the above solution to not generate tokens on each app launch and not making “unnecessary” calls to my backend for saving it. However, as I read your documentation further I noticed that the tokens can differ if the application is reinstalled, but the SecureStore token value persist so there could most probably become a mismatch here so that the token in SecureStore is no longer a valid one and a new token should have been created anew.

Scenario 2
On app launch, the app checks permissions and if granted generates token and sends it to my backend for saving. This is now happening on each application launch to ensure a “fresh” token is always held in my backend. Nothing gets handled trough the SecureStore as a mentioned in “Scenario 1”.

Conclusion / problem
I have used the expo client on iPhone without any problems with the push token. However, we recently started builds with expo for trying out the APK natively on Android phones. And they are having troubles with their tokens not being valid. I’ve checked the token generated for these devices and they are the same when running the app trough Expo client and when running the app from the APK built.

I’ve followed the guide on https://docs.expo.io/versions/latest/guides/push-notifications.html (awesome guides btw!) and all works as intended as far as asking permissions and generating tokens, but the tokens have been “corrupted” somehow.

So I am looking for some best practice and if I’m doing something wrong in either of the scenarios a described above. Since my only conclusion as to why this has started happening is because some “mismatch” between tokens when running the native APK on Android or running the application through expo client. This may be a “non issue” on app release further but I can’t know for sure.

The questions I’ve been asking myself is:

  1. Can I “over use” the call to “getExpoPushTokenAsync()”? In scneario 1 this wasn’t the case but now I use the method on every app launch to ensure fresh and valid token.
  2. Are there any difference in how and what token gets generated when running through the client and running native apk?
  3. How can I ensure a valid token is saved to my backend on application launch, I use the application on my phones in development now in three different ways: a) exp start --lan b) Through account QR (builds on my expo account) c) Downloading the APK and installing it on Android phone.

I’m posting because I’ve tried to find these answers here and on other places but can’t find this particular problem. Happy to be pointed in the right direction, have a missed it.

Thank you!

/Johan

#2

To stop regenerating new tokens when the app starts you can push your deviceId to your backend and if it tells you that it has been saved because it didn’t exist yet, then you have to generate a new token because the deviceId should be different when the app is reinstalled.

#3

Thank you for your suggestion! With this deviceId being unique a could also save to SecureStore and check value there along with saving the token there, that way I don’t need to make the unnescessary backend calls. However, I read the documentation about the deviceID and it says it’s unique in the way you described but also depending on the expo client. Is it then related to running through the expo client or will this approach work just as well when running the application stand-alone?

#4

And also, I’m interested in how the generated tokens get “connected” to the project. I wonder if there could be some unexpected problems if you build the application in the three different ways I described above;

  1. Local (exp start --lan)
  2. Running from Expo project QR code (exp build:ios / exp build:android)
  3. Installing APK on device

Should all they above cases use different app.json specifying different names/ slugs or whatever to not mix things up?

#5

This is related to: https://github.com/expo/expo/issues/1568

#6

Hi! Sorry about this issue. We’ve been working hard on a fix but it unfortunately required a lot of changes on our end. We just put up a guide on using FCM for push notifications which should fix the problem: https://docs.expo.io/versions/latest/guides/using-fcm. Please let us know if you run into problems with the new system and sorry for the trouble.

#7

Hi! No worries, thank you for providing a workaround! I’ll try it out as soon as possible.

closed #8

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.