6

In my project, I'm using two libs to handle two different types of push notifications: Localytics and react-native-push-notification.

I have a custom FirebaseMessagingService that checks if it's a Localytics push then let the Localytics handle it via their provided methods. But if it's not a Localytics push, I need to pass this push data to react-native-push-notification's RNPushNotificationListenerService which is also a FirebaseMessagingService.

I'm attempting to start the RNPushNotificationListenerService but it doesn't seem to be starting because it never sends a push. I have tried setting breakpoints too but no luck.

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);
}

}

I have a feeling I'm not starting the service correctly?

I am also aware that AndroidManifest will ignore the declaration of com.google.firebase.MESSAGING_EVENT for RNPushNotificationListenerService.

I have tried getting the individual libraries working without my custom FirebaseMessagingService and they both work correctly. I just need to figure out how to start RNPushNotificationListenerService when I have both setup with my custom FirebaseMessagingService.

SherCoder
  • 678
  • 2
  • 11
  • 26

0 Answers0