android - 如何在 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_EVENT
for的声明RNPushNotificationListenerService
。
我尝试在没有我的自定义 FirebaseMessagingService 的情况下让各个库正常工作,并且它们都可以正常工作。当我使用自定义 FirebaseMessagingService 进行设置时,我只需要弄清楚如何启动 RNPushNotificationListenerService。
解决方案
推荐阅读
- microsoft-cognitive - 微软认知翻译文本 API
- keras - 如何使用 keras 将检查点保存到路径?
- php - 当我向查询中添加内容时,为什么对象会在非对象中发生变化
- c++ - C++ 中的 RPN 计算器(CodeBlocks 控制台)
- python - 使用opencv调整文件夹中的批量图像大小
- c# - 在 C# 中获取“#”哈希后的 URL 部分
- c# - 如何在 .net core 2.0 中压缩 Web API HttpClient 返回的响应?
- java - 如何从 JPA 中的表中选择一个月中的每条记录?
- javascript - 仅 HTML 内容更改时的浏览器后退/下一个箭头
- google-maps - 带有饼图和标签的地图