Open kukat opened 1 year ago
still couldn't get onNewToken
to work, workaround
final token = await Push.instance.token;
Hey @kukat, this probably happens because your app receives the APNs token before your that widget builds, and that time, listener in that widget hasn't been set up yet? You can either:
Push.instance.token
(not a bad idea / hack / workaround, that's exactly what it's there for :) )Push.instance.onNewToken.listen
to higher up in the widget tree so that it is built when the app first starts, or into a service that initializes when the app starts.Push.instance.onNewToken.listen
is useful to check if it has changed, so you can update your servers of the new token.
Let me know if that makes sense. Sorry for the late reply, I've been a bit busy 🙏
Ahh, @kukat I just looked at this again and found in the docs:
// To be informed that the device's token has been updated by the operating system
// You should update your servers with this token
Push.instance.onNewToken.listen((token) {
print("Just got a new FCM registration token: ${token}");
});
This doesn't include the first one. Not very intuitive, but this means whenever that is called, you should call your backend APIs to update the token. Otherwise this function would be called everytime the app is started.
onNewToken
:onNewToken
was not called. This would involve persisting the token or flag that it is the first time the token was received.onNewToken
. Whenever:What do you think?
Turns out Android does approach 1, so I will use the same to be consistent.
the APNs device token is printed even before the
initState
method.debug console:
and
Push.instance.onNewToken.listen
is never get executed.main.dart: