If your application uses Pushwoosh alongside another push provider or your own custom Firebase Cloud Messaging (FCM) implementation, you may encounter issues with device registration, such as devices appearing with an "Unknown Platform" or push tokens becoming invalid. This happens because multiple FirebaseMessagingService implementations in a single app can conflict with each other.
To resolve this, you must create a single, consolidated FirebaseMessagingService that correctly routes push tokens and messages to all necessary services.
Step 1: Create a Custom Messaging Service
Create a new class in your application that extends com.google.firebase.messaging.FirebaseMessagingService.
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.pushwoosh.firebase.PushwooshFcmHelper;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
// ...
}
Step 2: Handle New Tokens
Override the onNewToken() method in your custom service. Inside this method, pass the generated token to the Pushwoosh SDK. If you have another service that needs the token, call its registration method here as well.
@Override
public void onNewToken(String token) {
super.onNewToken(token);
// Register token with Pushwoosh
PushwooshFcmHelper.onNewToken(this, token);
// TODO: Register the token with your other push service here
// e.g., myOtherService.registerToken(token);
}
Step 3: Handle Incoming Messages
Override the onMessageReceived() method. In this method, first, allow the Pushwoosh SDK to process the incoming message. The PushwooshFcmHelper.onMessageReceived() method will return true if the message was intended for Pushwoosh and was handled. If it returns false, you can then pass the message to your other service's logic.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if the message is from Pushwoosh and handle it
boolean isPushwooshMessage = PushwooshFcmHelper.onMessageReceived(this, remoteMessage);
// If not a Pushwoosh message, pass it to your other service
if (!isPushwooshMessage) {
// TODO: Handle the message with your other push service's logic
// e.g., myOtherService.handleMessage(remoteMessage);
}
}
Step 4: Update AndroidManifest.xml
Finally, register your new custom service in your AndroidManifest.xml and ensure that no other FirebaseMessagingService is declared. This prevents the system from getting confused about which service should handle FCM events.
<application ...>
<service
android:name=".MyFirebaseMessagingService" // Use the path to your custom service
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Comments
0 comments
Please sign in to leave a comment.