When you use multiple SDKs that rely on Firebase Cloud Messaging (FCM) in your Unity Android app (such as the Firebase SDK itself, or analytics services like Appsflyer), you need to manage the FCM token and message handling carefully to avoid conflicts. A common issue is the FCM token being regenerated on every app launch.
The solution involves two main steps:
1. Manually controlling the FCM token registration with Pushwoosh.
2. Creating a "router" service to ensure all SDKs receive incoming push messages.
1. Manually Registering the FCM Token
Instead of using the standard Pushwoosh.Instance.RegisterForPushNotifications() method, which automatically manages the token lifecycle, you should obtain the token yourself using your primary FCM provider (e.g., the Firebase SDK) and then pass it to Pushwoosh.
Use the Pushwoosh.Instance.RegisterExistingToken(string token) method. This gives your application full control over the token.
Example Implementation:
// In your app's initialization logic
// 1. Subscribe to Firebase token update events
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
// 2. Get the initial token from Firebase
Firebase.Messaging.FirebaseMessaging.GetTokenAsync().ContinueWithOnMainThread(task => {
if (task.IsCompleted && !task.IsFaulted && !task.IsCanceled) {
string fcmToken = task.Result;
UnityEngine.Debug.Log("Obtained FCM Token: " + fcmToken);
// 3. Pass the existing token to Pushwoosh
Pushwoosh.Instance.RegisterExistingToken(fcmToken);
}
});
// Handler for when Firebase issues a new token
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs args) {
UnityEngine.Debug.Log("New FCM Token received: " + args.Token);
// 4. Pass the new token to Pushwoosh to keep it updated
Pushwoosh.Instance.RegisterExistingToken(args.Token);
}
Important Considerations:
* Lifecycle Management: When using RegisterExistingToken, your application is fully responsible for the push token's lifecycle. The Pushwoosh SDK will not automatically request a new token or handle its refresh. You must listen for token updates from Firebase (using the TokenReceived event) and pass the new token to Pushwoosh whenever it changes.
* Network Errors: The registration call to Pushwoosh can fail due to device network issues (e.g., Unable to resolve host). It is recommended to implement your own retry logic to handle potential registration failures and ensure the token is successfully sent to Pushwoosh.
2. Routing Incoming Push Messages
When a push message arrives, the Android system delivers it to only one FirebaseMessagingService. To ensure all your SDKs can process the message, you must create a single router service that forwards the message to each SDK as needed.
For detailed instructions on creating this router service, please see our guide: Android: Using Multiple FirebaseMessagingService Implementations.
Comments
0 comments
Please sign in to leave a comment.