首页 > 解决方案 > 如何在 Android 中拥有多个 FirebaseMessagingService?

问题描述

在我的项目中,我使用两个库来处理两种不同类型的推送通知:Localytics 和 react-native-push-notification。

我有一个自定义 FirebaseMessagingService 来检查它是否是 Localytics 推送,然后让 Localytics 通过他们提供的方法处理它。但如果不是 Localytics 推送,我需要将此推送数据传递给 react-native-push-notification 的 RNPushNotificationListenerService,它也是 FirebaseMessagingService。

我正在尝试启动 RNPushNotificationListenerService 但它似乎没有启动,因为它从不发送推送。我也尝试过设置断点,但没有运气。

AndroidManifest.xml

 <!-- push notifications -->

    <service android:name=".fcm.CustomFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
    <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>
    <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <!-- push notifications end -->

CustomFirebaseMessagingService.java

public class CustomFirebaseMessagingService extends FirebaseMessagingService {

public CustomFirebaseMessagingService() {
    super();
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();

    try {
        if (!Localytics.handleFirebaseMessage(data)) {
            forwardPushNotification(remoteMessage);
        }
    } catch (Exception e) {
        Timber.e(e, "Failed to extract Push Message", remoteMessage.getMessageId());
    }
}

@Override
public void onDeletedMessages() {
    super.onDeletedMessages();
}

@Override
public void onMessageSent(String msgId) {
    super.onMessageSent(msgId);
}

@Override
public void onSendError(String msgId, Exception e) {
    super.onSendError(msgId, e);
}

@Override
public void onNewToken(String token) {
    super.onNewToken(token);
    Localytics.setPushRegistrationId(token);
}

private void forwardPushNotification(RemoteMessage remoteMessage) {
    Map<String, String> data = remoteMessage.getData();
    data.put("from", remoteMessage.getFrom());
    Intent intent = new Intent(this, RNPushNotificationListenerService.class);

    Bundle extraData = new Bundle(data.size());
    for (Map.Entry<String, String> entry : data.entrySet()) {
        extraData.putString(entry.getKey(), entry.getValue());
    }
    intent.putExtras(extraData);
    startService(intent);
}

}

我感觉我没有正确启动服务?

我也知道 AndroidManifest 将忽略com.google.firebase.MESSAGING_EVENTfor的声明RNPushNotificationListenerService

我尝试在没有我的自定义 FirebaseMessagingService 的情况下让各个库正常工作,并且它们都可以正常工作。当我使用自定义 FirebaseMessagingService 进行设置时,我只需要弄清楚如何启动 RNPushNotificationListenerService。

标签: androidfirebasereact-nativefirebase-cloud-messagingreact-native-push-notification

解决方案


推荐阅读