在我的项目中,我使用两个库来处理两种不同类型的推送通知: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
的{{1}}声明。
我尝试使各个库在没有我的自定义FirebaseMessagingService的情况下工作,并且它们都能正常工作。当我同时使用自定义FirebaseMessagingService进行设置时,我只需要弄清楚如何启动RNPushNotificationListenerService。