loading...
Cover image for Build WordPress App with React Native #27 : open specific screen with a deeplink

Build WordPress App with React Native #27 : open specific screen with a deeplink

kris profile image kris Originally published at kriss.io on ・6 min read

This series intends to show how I build app to serve content from my WordPress blog by using react native. Since, my blog is talking about react-native, the series and the articles are interconnected. We will learn how to set-up many packages that make our lives comfortable and learn how to deal with WordPress APIs. Here, the most prominent features talked about in the book are the dark theme , offline mode, infinite scroll and many more. You can discover much more in this series.this inspiration to do this tutorial series came from the React Native Mobile Templates from instamobile

In case of wanting to learn from the beginning, all the previous parts for this tutorial series are available below:

  1. Build WordPress Client App with React Native #1: Overview
  2. Build WordPress Client App with React Native #2: Setting Up Your Environment
  3. Build WordPress Client App with React Native #3: Handle Navigation with React navigation
  4. Build WordPress Client App with React Native #4: Add Font Icon
  5. Build WordPress Client App with React Native #5 : Home Screen with React native paper
  6. Build WordPress Client App with React Native #6 : Using Html renderer and Moment
  7. Build WordPress Client App with React Native #7: Add pull to refresh and Infinite scroll
  8. Build WordPress Client App with React Native #8: Implementing SinglePost Screen
  9. Build WordPress Client App with React Native #9: implement simple share
  10. Build WordPress Client App with React Native #10: Setup and save bookmark
  11. Build WordPress Client App with React Native #11: Remove and Render Bookmark
  12. Build WordPress Client App with React Native #12: Categories screen
  13. Build WordPress Client App with React Native #13: Configuring firebase in contact screen
  14. Build WordPress Client App with React Native #14 : Implementing Settings Screen
  15. Build WordPress Client App with React Native #15 : Forwarding message to inbox with Cloud function
  16. Build WordPress Client App with React Native #16 : Dark theme
  17. Build WordPress Client App with React Native #17 : Fix react-native-render-html to change theme
  18. Build WordPress Client App with React Native #18 : changing Theme
  19. Build WordPress Client App with React Native #19 : Notify user when offline
  20. Build WordPress Client App with React Native #20 : Saving data to cache
  21. Build WordPress Client App with React Native #21 : Splash Screen on iOS
  22. Build WordPress Client App with React Native #22 : Splash Screen on Android
  23. Build WordPress Client App with React Native #23 : Setup Firebase Push notification [iOS]
  24. Build WordPress Client App with React Native #24 : Setup Firebase Push Notification [Android]
  25. Build WordPress Client App with React Native #25 : Manage device token

in this previous chapter we can receive notification from Firebase on both Android and iOS for this last feature we will open a new post with a touch notification message

Listen for a notification event

create new async function name createNotificationListenersand create three listener

the first listener triggered when a particular notification has been received in foreground

this.notificationListener = firebase
      .notifications()
      .onNotification(notification => {
        const {title, body, data} = notification;
        console.log('1');
      });

second If your app is in the background, you can listen for when a notification is clicked/tapped/opened as follows:

this.notificationOpenedListener = firebase
      .notifications()
      .onNotificationOpened(notificationOpen => {
        const {title, body, data} = notificationOpen.notification;
        console.log(data.post_id);
      });

last If your app is closed, you can check if it was opened by a notification being clicked/tapped/opened as follows:

const notificationOpen = await firebase
      .notifications()
      .getInitialNotification();
    if (notificationOpen) {
      const {title, body, data} = notificationOpen.notification;
      console.log('3');
    }

then activate function on componentDidMount

componentDidMount() {
    SplashScreen.hide();
    this.checkPermission();
    this.createNotificationListeners();
  }

now we can listen to event from notification in multiple situations

Open specific screen with Deeplink

For iOS

we can set up a route for specific screen first we setup on iOS first open Appdelegate.m import package

#import <React/RCTLinkingManager.h>

then add before @end

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
 options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
 return [RCTLinkingManager application:app openURL:url options:options];
}

next goto info tab scroll to URL Type section

For Android

we need to open our Manifest file and add the app name we will want to be referencing, in our case krissnewapp.

In android/app/src/main, open AndroidManifest.xml and add the following intent filter below the last intent filter already listed, within the <activity> tag:

<intent-filter android:label="filter_react_native">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="krissnewapp" android:host="post" /> // A
</intent-filter>

Setup Deeplink in React navigation

next, define a route with react-navigation in CreateStackNavigatordefine a path in SinglePost screen and receive post_id as the parameter

const StackNavigator = createStackNavigator({
  DashboardTabNavigator: DashboardTabNavigator,
  SinglePost: {
    screen: SinglePost,
    path: 'post/:post_id',
  },
  CategorieList: CategorieList,
  Contact: Contact,
});

the last thing define route prefix

const prefix = 'krissappnew://';
  return (
    <PaperProvider theme={paper_theme}>
      <Navigation theme={nav_theme} uriPrefix={prefix} />
    </PaperProvider>
  );

now we can use deeplink in notification listener

first import linking component from react-native

import {Platform, Linking} from 'react-native';

add Linking to Notification package

this.notificationListener = firebase
      .notifications()
      .onNotification(notification => {
        const {title, body, data} = notification;
        Linking.openURL('krissappnew://post/' + data.post_id).catch(err =>
          console.error('An error occurred', err),
        );
        console.log('1');
      });

    this.notificationOpenedListener = firebase
      .notifications()
      .onNotificationOpened(notificationOpen => {
        const {title, body, data} = notificationOpen.notification;
        Linking.openURL('krissappnew://post/' + data.post_id).catch(err =>
          console.error('An error occurred', err),
        );
        console.log(data.post_id);
      });
    const notificationOpen = await firebase
      .notifications()
      .getInitialNotification();
    if (notificationOpen) {
      const {title, body, data} = notificationOpen.notification;
      Linking.openURL('krissappnew://post/' + data.post_id).catch(err =>
        console.error('An error occurred', err),
      );
      console.log('3');
    }

we receive post_id from a message and concatenate to URL while using Linking component open

Summary

here we learn a lot of technique from setup push notification on Android and iOS also Firebase then we handle device token and we conclude everything with using cloud function send message last thing we learn how to handle Deeplink in React native

The post Build WordPress Client App with React Native #27 : open specific screen with a deeplink appeared first on Kriss.

Discussion

pic
Editor guide