Reputation: 41
I have a simple application using Firebase for Unity, where the user dynamically subs/unsubs from topics, in which he will receive push notifications for each one of said topics.
Here's the code that handles it:
public class FBManager : MonoBehaviour {
private async void Start() {
var dependencyStatus = await Firebase.FirebaseApp.CheckAndFixDependenciesAsync();
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
}
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
}
public async void Subscribe(string topic) {
print("Subbing " + topic);
await Firebase.Messaging.FirebaseMessaging.SubscribeAsync(topic);
print("Subbed " + topic);
}
public async void Unsubscribe(string topic) {
print("unSubbing " + topic);
await Firebase.Messaging.FirebaseMessaging.UnsubscribeAsync(topic);
print("unSubbed " + topic);
}
}
The Subscribe/Unsubscribe methods are called from buttons within the application, and this code works fine during the first session of the app.
However, upon closing/minimizing and reopening the application, both async methods (SubscribeAsync/UnsubscribeAsync) never finish, leaving the app in a corrupted state where I can't ever change its subscriptions after the first time I opened it. Since the Firebase API never throws any errors/messages, I'm not really sure what to do, as the methods are simply executed and never finish.
I tried a few variations with coroutines, and using the obsolete versions of the same methods (Subscribe/Unsubscribe), both of which have the same problem (not working after reopening the app). I inspected the console output with "adb logcat", and no error was thrown by Firebase, the execution of both Subscribe/Unsubscribe are simply stopping at the first print.
Does anyone experienced such issues with the library? Firebase documentation is somewhat lackluster, and after a few days of research, I didn't find any related issues online, and the little that I found was dated from 2 or so years ago, while they were still using the deprecated SubscribeFromTopic/UnsubscribeFromTopic. As such, any help with this subject would be greatly appreciated.
Upvotes: 3
Views: 1770
Reputation: 644
For anyone having this same bug, you can check if it was fixed in an issue opened on the Firebase official github repo.
Upvotes: 0
Reputation: 41
After several attempts, I figured out what was the problem. For some reason, every time you reopen the app, you need to "refresh" your token in order to use subscribe/unsubscribe again.
By "refresh", I mean you need to delete your token, and get a new one, through the methods DeleteTokenAsync/GetTokenAsync.
Upvotes: 1