davide-scalzo / react-native-mixpanel

A React Native wrapper for Mixpanel tracking
MIT License
455 stars 195 forks source link

Android: Notifications with deeplinks doesn't open targetted view #263

Closed kiitos-smasri closed 3 years ago

kiitos-smasri commented 3 years ago

Hello Am trying to receive push notifications that contains deeplinks from Android device push notifications are well received but the issue is when clicking on the notification it does nothing I contacted mixpanel support and they guided me to a native library while am using react-native

https://github.com/mixpanel/mixpanel-android

is there any other solution?

kyleclegg commented 3 years ago

We had to add some extra handling to get this working on Android due to there being multiple push providers (FCM and Mixpanel). If you check the logs the push notification may be coming through but getting muted/ignored. Check out the following resources.

https://github.com/mixpanel/mixpanel-android/issues/586 https://medium.com/@chornenkyy.v/do-you-want-to-use-two-or-more-firebasemessagingservices-at-once-67236d1bd9e0

Our custom push handler looks like this:

public class PushProvidersHandler extends FirebaseMessagingService {
    private RNReceivedMessageHandler mMessageReceivedHandler = new RNReceivedMessageHandler(this);

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        MixpanelFCMMessagingService.addToken(s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("RNPushNotification", "PushProvidersHandler - onMessageReceived!");
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getData().containsKey("mp_message")) {
            handleMixpanel(remoteMessage);
        }
        Log.i("RNPushNotification", "PushProvidersHandler - begin sending to client");
        mMessageReceivedHandler.handleReceivedMessage(remoteMessage);
    }

    private void handleMixpanel(RemoteMessage remoteMessage) {
        Map<String,String> params = new HashMap<String,String>();
        params.put("title", remoteMessage.getData().get("mp_title"));
        params.put("message", remoteMessage.getData().get("mp_message"));
        params.put("alert", remoteMessage.getData().get("mp_message"));
        Gson gson = new Gson();
        String json = gson.toJson(params);
        remoteMessage.getData().put("data", json);

        handleMixpanelDeepLink(remoteMessage);
    }

    private void handleMixpanelDeepLink(RemoteMessage remoteMessage) {
        Gson gson = new Gson();
        String maybeOnTap = remoteMessage.getData().get("mp_ontap");
        if(maybeOnTap != null && maybeOnTap != ""){
            Type dictType = new TypeToken<Map<String, String>>() {}.getType();
            Map<String,String> onTapDict = gson.fromJson(maybeOnTap, dictType);
            remoteMessage.getData().put("deepLinkType", "mixpanelDeepLink");
            remoteMessage.getData().put("uri", onTapDict.get("uri"));
        }
    }
}

AndroidManifest includes:

<service
            android:name="com.myapp.notifications.PushProvidersHandler"
            android:exported="false">
            <intent-filter>
                 <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
</service>
kiitos-smasri commented 3 years ago

that worked thanks

kpnaveen commented 3 years ago

Hi, I am facing the issue mentioned above, deep linking not working. I tried the above code snippet but was not able to get it solved. Also, I would like to know how are you sending the deep link URI from the Mixpanel. Is it via the custom payload?