HMS-Core / hms-react-native-plugin

This repo contains all of React-Native HMS plugins.
https://developer.huawei.com/consumer/en/doc/overview/HMS-Core-Plugin?ha_source=hms1
Apache License 2.0
240 stars 68 forks source link

onTokenReceived never triggers - (MoEngage sdk conflict) #150

Closed ngseba closed 2 years ago

ngseba commented 2 years ago

Description

When trying to apply for a token using HmsPushInstanceId.getToken("") it works on EMUI >=10. However, when I try doing the same thing on EMUI < 10, it returns an empty string. Upon inspecting the documentation I found out that: If the response to a token request is empty or a token changes, an event listener needs to be used to receive the requested or updated token. The event listener for the new tokens is HmsPushEvent.onTokenReceived.

Expected behavior

I expect the onTokenReceived handler to work.

Implementation details

So I implemented the following:

 useEffect(() => {

        //This one works for EMUI >=10 while EMUI < 10 receives empty token
        HmsPushInstanceId.getToken("")
            .then((result) => {
                console.log('PushKit@getToken - Token ',result)
            })
            .catch((err) => {
                console.log('PushKit@getToken - Error ',err)
            });

        // Should trigger on both EMUI greater and smaller than 10, however that never happens, on neither
        const onTokenReceivedListener = HmsPushEvent.onTokenReceived((result) => {
            console.log('PushKit@onTokenReceivedListener - Token ',result)
        })

        // Never triggers
        const onTokenReceivedError = HmsPushEvent.onTokenError((error) => {
            console.log('PushKit@onTokenReceivedError - Error ',error)
        })

        return () => {
            onTokenReceivedListener.remove();
            onTokenReceivedError.remove();
        };

    },[])

It seemed that it should work, but it didn't. Since I couldn't figure out if I subscribe to the token method in an appropiate manner, I wanted to debug if the native code works, if my logs would show up there, clearly there would be a problem in my React-Native code.

Logs

Upon inspecting the native code I found out that there are services registered for those event listeners. And saw that there are logs implemented. Upon carefully inspecting the following logs:

Screenshot 2021-10-07 at 15 55 48

Here I can see that the service is successfully registered and I am interested particularly in the logs "onNewToken to host app." and "onNewToken to host app with bundle." these logs are from the base service class, but the methods that make those logs appear in the first place, should also trigger another function, the one that is responsible with passing the token to React-Native, that one has some logs in it as well (so next I'm going to look for those logs in the debugging console). Basically I was expecting the log: __"HMSSDK_HmsPushMessageService onNewToken "__ to appear as well (since that is the method that also sends the token to React-Native and for some reason that doesn't happen). And right after that, service is destroyed.

Screenshot that proves that the expected method which should send params to React-Native doesn't show any log:

Screenshot 2021-10-07 at 16 07 57

I'm not that familiar with Android lifecycle, but is the service supposed to be destroyed like that? And even if all the logs should be the same as they are here, why doesn't the onTokenReceived trigger on React-Native?

Environment

ngseba commented 2 years ago

I managed to figure it out. I also installed MoEngage SDK in order to provide push notification campaigns and inApps. So i added the following in my apps Android Manifest:

        <service
            android:name="com.moengage.hms.pushkit.MoEPushKitMessageService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Also noticed that inside the hms-push-react-native package there is an intent like this declared in AndroidManifest. So the one that I added, inside my own manifest, overriding the one from the package, reason for which logs were not triggered and the callbacks were not triggered as well from this library's service.

I'll leave this here in case someone else has a similar problem.