iOS Question FCM Token

Cristian Achim

Member
Licensed User
Longtime User
Hi,

The FCM Token (GetToken) on iOS changes every time the app starts and SubscribeToTopics runs. I want to use it to send push messages on the user token, and sometimes, the application must be restarted twice to record the new Token in database and to receive new push notifications. On Android, it works very well. Why does it change every time? Is it normal?

I used code from PushClients.zip https://www.b4x.com/android/forum/threads/b4x-firebase-push-notifications-2023.148715/

B4i v8.51 - iPhone 12 - iOS 18.0.1

Thank you!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tested with this code:
B4X:
Private Sub GetToken As String
    Dim FIRMessaging As NativeObject
    FIRMessaging = FIRMessaging.Initialize("FIRMessaging").GetField("messaging")
    Dim token As NativeObject = FIRMessaging.GetField("FCMToken")
    If token.IsInitialized = False Then Return "" Else Return token.AsString
End Sub

The token seems to be the same every run.
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User

Hi Erel,

I used this code when I created the post. The APNS token it's the same every run but FCM token is new every time.

I run CallSubDelayed2(FirebaseMessaging, "SubscribeToTopics", Array("general")) in B4XMainPage and Application_PushToken on Main.

Thank you!
 
Last edited:
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
I reproduced with the example you gave and the same thing happens



and every time I received other token

 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
@Erel, have you some suggestions?
Thank you!
Please don't make such posts.

Assuming that you aren't doing something wrong, such as running different apps with the same package name, then it might be a bug in Firebase. The token is handled by Firebase.

With that said, your code should handle this case properly. Send the new token to your server and update it.
Another option is to switch to using unique topics as an alternative.
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
Please don't make such posts.
Sorry!
Assuming that you aren't doing something wrong, such as running different apps with the same package name, then it might be a bug in Firebase. The token is handled by Firebase.
It does not apply.
Another option is to switch to using unique topics as an alternative.
I already did this but after about two days I have to reinstall the application to receive again push messages.

Thank you for your support!
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
Hi,

I discovered where the problem was. Every time when App.RegisterForRemoteNotifications / Application_PushToken is running, it generates a new token. I modified it to run only until the initial FCM token is generated and I see that it is ok.
 
Last edited:
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Hi,

Every time when App.RegisterForRemoteNotifications / Application_PushToken is running, it generates a new token.

Don't understand what you mean by " Every time when App.RegisterForRemoteNotifications / Application_PushToken is running"

App.RegisterForRemoteNotifications is only called once, in "B4XPage_Created"

so it should only run once and never generate a new token.
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
Every time when open a new instance of app (after it was closed with swipe or was not in background) B4XPage_Created run and generate a new token. To avoid this I use this:

B4X:
If GetToken <> Main.Sett.GetString("fcm_token") Then
    Main.Sett.Put("fcm_token", GetToken)
    Main.App.RegisterForRemoteNotifications
End If

and Main.App.RegisterForRemoteNotifications will only run if token is different from the previous token, otherwise not.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User

Do you think this has something to do with the issue I’m experiencing here?
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
Do you think this has something to do with the issue I’m experiencing here?

No. I had the same problem mentioned by you here on both Android and iOS when I used sending notifications to topic. Now I use sending to FCM token and it works perfectly, including on iOS after I changed the code mentioned above.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Thanks for your reply. I'm not sure to understand what you mean.
The FCM token is unique. If you register that in your db, how can you manage when you want to send notification to a single user or to a group?
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
Thanks for your reply. I'm not sure to understand what you mean.
The FCM token is unique. If you register that in your db, how can you manage when you want to send notification to a single user or to a group?
I save in the database every FCMtoken generated by each device on which a specific user is connected. One push message are sent from the application to the respective user, one by one on each token. I don't use group messages.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I still need to grasp the core of the issue: the system is designed to send messages to Topics, and each device can subscribe to one or more Topics. Do you think a bug has arisen that sometimes causes it not to work, or is there perhaps something in the program that needs adjusting?
 
Upvote 0

Cristian Achim

Member
Licensed User
Longtime User
You can search on Google if there are problems with FCM. Now it works well in my app, but I don't use topics, I send notifications to the tokens.
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
I am using topics - and code based on the original example posted by Erel. Once in a while, the notifications stop, and like you, I have to restart the application. Just recently, I added a "mute" checkbox" to the App so that the user had the option of muting the notifications or unmuting them. The mute works by unsubscribing or resubscribing.
I have since then found out that when the notifications suddenly stop, I can restart them by checking and unchecking the mute checkbox. So apparently, what may be happening is that after a while, for some reason you may get unsubscribed ......
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I'm not concerned about the token or if the token changes, because every time the app is launched, a RegisterForRemoteNotifications and a SubscribeToTopic (as per the tutorial) are executed, and the topic never changes because it's a unique code linked to the device. Everything has always worked fine, both on Android and iOS. Only recently, some clients have started experiencing notifications that stop working, and uninstalling and reinstalling the app resolves the issue. I've read this thread carefully, but I can't find a clear logic, not even in the method you've used. Why call RegisterForRemoteNotifications only if the token has changed and not always and in any case?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…