Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

How to use this library when you have multiple push providers? #952

Closed
shercoder opened this issue Nov 28, 2018 · 7 comments
Closed

How to use this library when you have multiple push providers? #952

shercoder opened this issue Nov 28, 2018 · 7 comments
Labels

Comments

@shercoder
Copy link

I'm using react-native-push-notification along with Localytics push messaging. So I created a custom FirebaseMessagingService that checks if RemoteMessage is a Localytics message else it needs to fire RNPushListenerService.

I'm starting RNPushListenerService using startService but it does not look like it is starting it. Has anyone else dealt with issue like this before? and How did you handle it?

Note: There can only be one FirebaseMessagingService with intent-filter of com.google.firebase.MESSAGING_EVENT and I'm registering my custom service with that intent-filter.

public class CustomFirebaseMessagingService extends FirebaseMessagingService {
    @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 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);
        intent.setAction("com.google.firebase.MESSAGING_EVENT");
        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 think react-native-push-notification library needs to provide a custom method like the one Localytics is providing to handle these notifications when the user has multiple push providers. e.g. Localytics.handleFirebaseMessage

@yashafromrussia
Copy link

@shercoder have you figured this out?

@shercoder
Copy link
Author

@gorjuspixels Yeah I ended up forking this repo and fixing it in a feature branch. I haven't had a chance to make a PR for this repo. I'll see if I can do that this weekend but here is the draft PR if you need example buildcom#1

@shercoder
Copy link
Author

Then In your custom FirebaseMessagingService, you can do something like this:

private RNReceivedMessageHandler receivedMessageHandler = new RNReceivedMessageHandler(this);

...
...
...

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

        String from = remoteMessage.getFrom();
       
            try {
                if (from first provider) {
                    receivedMessageHandler.handleReceivedMessage(remoteMessage);
                } else {
                    // pass remote message to another provider
                }
            } catch (Exception e) {
                Log.e(e, "Failed to extract Push Message", remoteMessage.getMessageId());
            }
        
    }

@shercoder
Copy link
Author

@gorjuspixels Sorry for taking so long to get this in but if you're still looking for the solution, I have finally found some time to get the PR rolling to support this here #1128

Even if this is no longer your requirement, I would really love it if you can test the PR for me. 🙏

Again, apologies for the delay.

@uraniumreza
Copy link

@shercoder Can you please help me here If I want to pass RemoteMessage to RNFirebase? How should I do that?

@shercoder
Copy link
Author

@uraniumreza I do not use RNFirebase, so I am not familiar with their API. I'd suggest reading through their docs and if you can't find your answer then create an issue on their Github repo.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants