11

Here is my code, how can I navigate user to the desired screen when clicked on a notification or button in a notification.

componentWillMount() {
    OneSignal.addEventListener('received', this.onReceived);
    OneSignal.addEventListener('opened', this.onOpened);
    OneSignal.addEventListener('registered', this.onRegistered);
    OneSignal.addEventListener('ids', this.onIds);

    OneSignal.inFocusDisplaying(2);
    OneSignal.requestPermissions({
        alert: true,
        badge: true,
        sound: true
    });
}

componentWillUnmount() {
    this.isUnmounted = true;

    OneSignal.removeEventListener('received', this.onReceived);
    OneSignal.removeEventListener('opened', this.onOpened);
    OneSignal.removeEventListener('registered', this.onRegistered);
    OneSignal.removeEventListener('ids', this.onIds);
}

onReceived(notification) {
    console.log("Notification received: ", notification);
}

onOpened(openResult) { // HERE I WANT TO NAVIGATE TO ANOTHER SCREEN INSTEAD OF HOME SCREEN
    this.isNotification = true;

    let data = openResult.notification.payload.additionalData;
    let inFocus = openResult.notification.isAppInFocus;

    console.log('Message: ', openResult.notification.payload.body);
    console.log('Data: ', openResult.notification.payload.additionalData);
    console.log('isActive: ', openResult.notification.isAppInFocus);
    console.log('openResult: ', openResult);
}

onRegistered(notifData) {
    console.log("Device had been registered for push notifications!", notifData);
}

onIds(device) {
    try {
        AsyncStorage.setItem("@SC:deviceInfo", JSON.stringify(device));
    } catch (error) {
        console.log(error);
    }
}

Do anyone have knowledge about all this, React Native + OneSignal + React Navigation + Redux. Please help!

Abid
  • 117
  • 1
  • 2
  • 9

4 Answers4

7

To achieve the desired behavior you can do couple of things. You can manually check the notification and state of the router and if its necessary redirect the user to the screen or you can use the Deep Linking functionality.

To use Deep Linking you attach url parameter to your notification while sending it. To direct user to the correct screen in your app you can use react-navigation deep linking functionality.

From One Signal Documentation

url string The URL to open in the browser when a user clicks on the notification. Example: http://www.google.com

Note: iOS needs https or updated NSAppTransportSecurity in plist


From React Navigation Documentation

Deep Linking

In this guide we will set up our app to handle external URIs. Let's start with the SimpleApp that we created in the getting started guide. In this example, we want a URI like mychat://chat/Taylor to open our app and link straight into Taylor's chat page.

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
bennygenel
  • 23,896
  • 6
  • 65
  • 78
  • I have made an application using the react native and onesignal. I am able to receive the notifications from onesignal, but if app is not running and push notification is received then clicking on the push notification doesn't navigate to the desired screen. Please suggest me a solution for this problem. How this can be solved? I am using the route-flux for routing in my app. This is not an issue when app is running and notification is received. – Mukesh Kumar Oct 13 '17 at 09:48
  • @CrazyDeveloper Deep linking feature is not implemented in react-native-router-flux yet. To be able to use abow technique you need to implement [react-native Linking API](https://facebook.github.io/react-native/docs/linking.html) – bennygenel Oct 13 '17 at 09:56
  • Thanks for your response. So app is not running in background and when I click on the notification then app gets opened. How did app come to know that it is being opened by on clicking the push notification. Thanks. – Mukesh Kumar Oct 13 '17 at 10:38
  • 1
    With OneSignal you can send custom URL to open when notification activated like `mychat://chat/Taylor`. You can listen for the url and parse the parameters and then redirect user to the correct screen. – bennygenel Oct 13 '17 at 10:39
  • thanks for pointing me to the solution. I was not aware about the Linking that it can listen in react native. This is really cool stuff. – Mukesh Kumar Oct 13 '17 at 10:53
  • I have a question, I'm developing two apps first of the user app and second to providers, in user app, I made some function to send order and saved into real-time database firebase t and in the second app I just retrieve the data from DB with a listener to get the new order as a real-time without refreshing the app, SO I want to when I send an order from user app to firebase I want to display a notify in the second app? How to handle these – DevAS May 14 '19 at 05:31
  • @DevAS you can use [Onesignal API](https://documentation.onesignal.com/reference) or [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging) – bennygenel May 14 '19 at 14:09
  • Hmm I think you don't understand me very well, the idea is I need to send a notification when I want to send data to the database and at the same time I want to receive notifications in another app – DevAS May 14 '19 at 14:41
  • @DevAS I understood you quite well. You need to use [Database triggers](https://firebase.google.com/docs/functions/database-events) and trigger a function that sends a notification to the related other app users. Read the documentations I sent you and be familiar with the tools you are using. I'm sure you can figure it out. – bennygenel May 14 '19 at 14:49
5

You can dispatch a NavigationAction or perform a navigate action when onOpened is fired. Following snippet should work:

componentWillMount() {
    OneSignal.inFocusDisplaying(0);
    OneSignal.removeEventListener('opened', this.onOpened.bind(this));
    OneSignal.addEventListener('opened', this.onOpened.bind(this));
  }

onOpened(openResult) {
    let data = openResult.notification.payload.additionalData;
    // ScreenName is the name of the screen you defined in StackNavigator
    this.props.navigation.navigate('ScreenName', data)
}
Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74
  • Thanks for this example, how do you *send* this info, is there a special use case, or do you simply send it along with everything else when you dispatch the info to OneSignal? – Sara Inés Calderón Nov 01 '18 at 19:01
  • "how do you send this info" - didn't understand which info are you talking about?! – Mostafiz Rahman Nov 01 '18 at 20:23
  • thanks for responding! i meant, how do you _send_ the route info, is it like this: `{route: 'HOME'}` when you dispatch the push notif to OneSignal or do you have to call it `additionalData` when you send it? by the time you get to `onOpened` -- how did that route info get there? – Sara Inés Calderón Nov 02 '18 at 15:22
  • 1
    You can send data along with the navigation route, I've updated my answer. Did it solve your query? – Mostafiz Rahman Nov 03 '18 at 04:10
  • Thanks for the update! I appreciate it. My question is more related to _sending_ the PN rather than _receiving_ it, but you have been very helpful -- thanks so much! From what you posted looks like when you _send_ the PN you have to define `additionalData: {'ScreenName': 'HOME'}`. – Sara Inés Calderón Nov 04 '18 at 16:37
  • 1
    Right you are. Push are sent from the server and additional data is sent as payload. – Mostafiz Rahman Nov 04 '18 at 17:20
  • I have a question, I'm developing two apps first of the user app and second to providers, in user app, I made some function to send order and saved into real-time database firebase t and in the second app I just retrieve the data from DB with a listener to get the new order as a real-time without refreshing the app, SO I want to when I send an order from user app to firebase I want to display a notify in the second app? How to handle these – DevAS May 14 '19 at 05:32
  • Check OneSigal p2p notifications: https://documentation.onesignal.com/docs/react-native-sdk#section-post-notification-peer-to-peer-notifications- – Mostafiz Rahman May 15 '19 at 06:47
1

In search for the solution I landed on this question and I think most of the answers are now old. So, in case anyone looking for the solution can try this.

OneSignal.setNotificationOpenedHandler((notificationResponse) => {
      const { notification } = notificationResponse;
      if (notification) {
        const { additionalData = null } = notification;
        if (additionalData) {
          const { type } = additionalData;
          navigateToScreen(type);
        }
      }
    });
 const navigateToScreen = (type) => {
    switch (type) {
      case "post":
      case "track":
        navigation.navigate("SinglePost");
        return;
      default:
        return;
    }
  };
sakshya73
  • 5,570
  • 5
  • 22
  • 41
0

In case someone else comes with a similar problem to mine I want to add onto what @Mostafiz Rahman said. The app I was working on had a bunch of nested stacks and tabs (react-navigation v1) inside of a drawer, and if Stack1 was backgrounded and the notification was for Stack2 I couldn't get them to jump around.

I ended up putting the logic as described by Mr. Rahman in every one of the stacks' first screens -- 1st screen of Stack1, 1st screen of Stack2, etc -- and that did it!

Sara Inés Calderón
  • 1,070
  • 12
  • 22
  • I have a question, I'm developing two apps first of the user app and second to providers, in user app, I made some function to send order and saved into real-time database firebase t and in the second app I just retrieve the data from DB with a listener to get the new order as a real-time without refreshing the app, SO I want to when I send an order from user app to firebase I want to display a notify in the second app? How to handle these – DevAS May 14 '19 at 05:32