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
245 stars 68 forks source link

[react-native-hms-push] can't read notification content #164

Closed anyamiletic closed 2 years ago

anyamiletic commented 2 years ago

Description We have successfully integrated HMS Core into our app, and enabled the Push Kit. We are able to use HmsPushInstanceId.getToken to fetch the device token, and through AppGallery Connect, send a notification to the device. However, upon clicking the notification from the NC, we are not able to read the notification parameters (none of our callbacks are getting triggered). These are the listeners we have set up:

  /** Handle push notification when app is in quit state (closed) */
    HmsPushMessaging.getInitialNotification()
      .then(remoteMessage => {
        console.log('initial notification message received: ', remoteMessage)
        return handleNotification(remoteMessage)
      })
      .catch((err) => {
        log.error(() => ['Error during getInitialNotification', err])
      })

/** Handle push notification when app is in background */
    HmsPushEvent.onNotificationOpenedApp((remoteMessage) => {
      console.log('foreground message received: ', remoteMessage)
      handleNotification(remoteMessage)
    })

Expected behavior We expect that one of these listeners will fire when the notification is clicked on

Current behavior The app opens, but nothing is written out in the console

Logs on app reload the HmsPushMessaging.getInitialNotification fires:

initial notification message received:  {"result": {}, "resultCode": "0"}

Environment

Other We couldn't find an example in the docs about handling just notifications (not data notifications, or topics) so we are not sure if these are the right handlers to use. When the app is in the foreground, background or killed, it is always the same case - no logs in the console, the listeners are not getting triggered. We have added the following in our AndroidManifest.xml:

<uses-permission android:name="android.permission.WAKE_LOCK" />
...

  <application>
    ....
    <service
      android:name=".DemoHmsMessageService"
      android:exported="false">
      <intent-filter>
        <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
      </intent-filter>
    </service>
  </application>

  <queries>
    <intent>
      <action android:name="com.huawei.hms.core.aidlservice" />
    </intent>
  </queries>
anyamiletic commented 2 years ago

We realised that by specifying a Custom Intent URI Page, the callbacks will fire as expected. So there was no change in the front-end code, only that every notification sent had to have the custom URI. Apparently this is a known issue, but i will still leave this issue open seeing as this is still buggy behaviour.

ozcanozgur commented 2 years ago

Hi @anyamiletic ,

We have tested the notification listeners with demo application. According to our recent tests, there are no buggy behaviour with version: 6.3.0.302.

We recommend you to try the latest updated version of React Native Push Kit. You should be careful with that, the onNotificationOpenedApp listener is called from index.js when application at killed state. Otherwise the getInitialNotification method is not a listener. You can try to call this metod manually after notification received to get received notification informations.

AppRegistry.registerComponent(appName, () => App);

HmsPushEvent.onNotificationOpenedApp((result) => {
  console.log("onNotificationOpenedApp", result);
  ToastAndroid.show(JSON.stringify(result, "\n", 4), ToastAndroid.SHORT);
});

If the issue persist, contact us here. Thank you for your interest.