CatalystCode / react-native-azurenotificationhub

React Native module to support Azure Notification Hub push notifications on Android, iOS, and Windows.
MIT License
47 stars 75 forks source link

I want to navigate app off a notification but getInitialNotification returning null no matter what/ _onRemoteNotification is working properly #169

Open mgoldfi1 opened 4 years ago

mgoldfi1 commented 4 years ago

Report

Environment

Issues and Steps to Reproduce

  1. Generate push notification
  2. App is either minimized or terminated
  3. User clicks on notification or opens app
  4. getInitialNotification() returning null from promise no matter what

Expected Behavior

I want a way to be able to tell if the notification was clicked on so that I can either direct the navigation to the chat screen or otherwise just open the app normally. Right now, I can only get _onRemoteNotification to be hit, which is not that useful because I only want to navigate the user if they clicked on the notification, not i they got a notification while they were in the app. I can't find any use cases for this but regardless im not hitting the .catch and its returning null every time. There is a chance I'm using this incorrectly but there is barely any documentation so I guess I should ask when exactly is getInitialNotification supposed to be hit?

Actual Behavior

App opens if you click the notification, sometimes onRemoteNotification is hit sometimes it is not.

Function:

getInitialNotification() {
    console.log('hitting function');
    NotificationHub.getInitialNotification()
      .then(res => {
        console.warn(res);
//This returns null every time
        if (res) {
//Never have made it inside this block
          Alert.alert(
            'Get Initial Notification Received',
            'Alert message: ' + res.getMessage(),
            [
              {
                text: 'Dismiss',
                onPress: null,
              },
            ],
          );
        }
      })
      .catch(reason => console.warn('ERROR', reason));
//Have not gotten an error
  }

App.js/ componentDidMount : I attach a listener to appState because i want to get the notification whenever it is brought to foreground. Am I doing this correctly/logically?

  componentDidMount = async () => {
    AppState.addEventListener('change', state => {
      store.dispatch({type: 'CHANGED_STATE', state});
//When state changes I update redux to handle other processes such as data fetching.
      if (state === 'active') {
        this.getInitialNotification();
//Calling method when brought to foreground
      }
    });
    this.requestPermissions()
      .then(x => console.warn(x))
      .catch(err => console.warn(err));
  };
mgoldfi1 commented 4 years ago

To anyone encountering a similar issue:

I realized this problem only occurs in debug/release builds but not distribution builds such as testflight which was very difficult for me to find out and eventually discovered by accident. After updating APNS certs to production and pushing the build to testflight, the getInitialNotification method is being hit on app cold start through a push notification. I don't know much else yet but this is what I have found so far.