Android Question Firebase Notifications - Token [resolved]

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

I'd like my server app (B4J) to send a message directly to a device when something happens on the server. I understand we can use FirebaseMessaging.Token to achieve this. I am doing the following:

  • In FirebaseMessaging service, in SubscribeToTopics sub, I store the token on the server db against the device UDID.
  • From the B4J app, I send a message to the topic and include the token in the 'to' parameter.
This works fine for the 'happy path' when the app is running (foreground) or at least recently used so the process is still around.

What I have found challenging is:
  • FirebaseMessaging.SubscribeToTopics does not appear to be called if the app was not running in the background. For example, the user killed the app from the recents list. This, in my observation is frequently done by casual users either because they are bored or because they perceive this way they will save battery or memory or improve performance. Whatever the reason, when the app is launched again, I do not see that SubscribeToTopics was invoked again however I see this line in the log after (FirebaseMessaging start) ...'sending message to waiting queue (CallSubDelayed - SubscribeToTopics)'.
  • What is the life of the firebase token? How do we know it has expired?
  • What is the impact of calling SubscribeToTopics? Will calling it again result in a new token being returned? In some of my tests this has happened.

I know these are quite a lot of questions in one post but I think they are all related to the subject of better understanding FirebaseMessaging.

Many Thanks,
Haris.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
For example, the user killed the app from the recents list.
On most devices this should'nt cause any issue. The process should start when a new message arrives. Unfortunately there are devices that move the app to the STOPPED state after it is being killed. In such cases there is nothing that can be done. The process will only start when the user explicitly starts the app.

What is the life of the firebase token? How do we know it has expired?
Don't make any assumptions. Whenever you find a new token you need to update the server.

Will calling it again result in a new token being returned?
No.
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Thanks for your prompt input. However...

  • I was indeed referring to the user starting the app again. I am testing on a Nexus 5X running 7.1 and killing the app stops it but I see the starter and firebasemessaging services start up again without user re-launching the app. Even if I relaunch the app, the SubscribeToTopics sub is not called, which is what surprised me.
  • I do send an update token request to the server when I get the fm_TokenRefresh sub invoked. I have, however, not seen this sub invoked so far.
  • I wonder why I have seen new tokens? Is it perhaps because I reinstalled the app?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I am testing on a Nexus 5X running 7.1 and killing the app stops it but I see the starter and firebasemessaging services start up again without user re-launching the app
This is the expected behavior. It kills the process. It doesn't move the app into a special "stopped" state.

How do you see that SubscribeToTopics is not called?
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
There is a log statement in there that should output something anyways like this: Log("setting fmtoken to: " & fm.Token). I had previously thought that initially, fm_TokenRefresh will always be invoked to provide the token but that is not the case and I have never seen it invoked.

Also, fm.Token returns a token sometimes and sometimes it doesn't. I suspect that is because this is an async operation but then the token refreshed event should get called to provide me with the actual token.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It doesn't move the app into a special "stopped" state.
Didn´t you wanted to say

It DOES move the app into a special "stopped" state.
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Didn´t you wanted to say

It DOES move the app into a special "stopped" state.
I think he is right in that it is not moving into the Stopped state as I can see the service starting itself even after I kill the app from the recent list. So if the app had moved to a stopped state, those services would not have restarted. That's what I understand anyways.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Didn´t you wanted to say

It DOES move the app into a special "stopped" state.
No. The stopped state means that the app cannot start unless the user explicitly start it. This is the state of all newly installed applications (not reinstalls).
When you kill an app with a swipe it shouldn't cause the app to move to the stopped state. It should only kill the process.

But if the Subscribe method is not called, how will the notifications be delivered to this device?
You don't need to subscribe multiple times. It only need to be done once (though it is better to call it every time the app starts to handle token changes).
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The stopped state means that the app cannot start unless the user explicitly start it.
Yes. And this happens if you Kill the app.

This is the expected behavior. It kills the process. It doesn't move the app into a special "stopped" state.
Due to this i was answering.
Killing the app will move the app into the special "Stopped" state.

And now you say NO? What did i miss?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
On most devices killing the app from the recent apps list (with a swipe) doesn't move the app to the stopped state. Only if you kill it from Settings - Application it will move it to the stopped state.
Ahh i see. I thought is would be both the same. Sorry
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Sorry for asking here, and not by the new topic - but what here was "solved" ?
I also in this situation when the token is empty after FirebaseMessaging service is re-started.

And exactly found that if no token is returned - no new push messages are arrived. I already use the timer to get token.
But it's strange - sometimes it's non-empty and all is OK, but sometimes - it's empty - and ... all is bad with pushes...
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Question is "what was resolved in this topic and how ?", as "resolved" is in the topic title.
3 questions were in the first massage about the token and subscribing.
 
Upvote 0
Top