Get Expo Push Notification data when app is closed

#1

Hi All. This is my App.js

import React from 'react';
import { AppState, View, StatusBar } from 'react-native';
import { ScreenHost } from './src/app/screen-host';
import { AppHost } from './src/debug/app-host';
import { settings } from './src/constants/settings';
import { services } from './src/services/services';
import {
  Notifications,
} from 'expo';

export default class App extends React.Component {

  state = {
    notification: {},
  };

  componentDidMount() {

    // Handle notifications that are received or selected while the app
    // is open. If the app was closed and then opened by tapping the
    // notification (rather than just tapping the app icon to open it),
    // this function will fire on the next tick after the app starts
    // with the notification data.
    this._notificationSubscription = Notifications.addListener(this._handleNotification);
  }

  _handleNotification = (notification) => {
    this.setState({notification: notification});
      alert(notification.data.conv_id);
  };

  render() {
    if (settings.useDebugLayout) {
      return (
        <AppHost>
          <AppMain />
        </AppHost>
      );
    } else {
      return (
        <AppMain />
      );
    }
  }
}

class AppMain extends React.Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <StatusBar barStyle="light-content" />
        <ScreenHost />
      </View>
    );
  }
}

This is working fine when the APP is in foreground or when APP is minimized, but when app is closed, and the notification comes, it shows the loading screen and the push notification data is lost. I am using

"expo": "^27.0.0",
"moment": "^2.22.2",
"react": "16.3.1",
"react-moment": "^0.7.0",
"moment-timezone": "^0.5.17",
"react-native": "https://github.com/expo/react-native/archive/sdk-27.0.0.tar.gz",
"react-native-swipeable": "^0.6.0",
"react-navigation": "1.5.11"
0 Likes

#2

Check out the various cases here: https://docs.expo.io/versions/latest/guides/push-notifications#notification-handling-timing

When the app is opened from being closed, it gets the data in props.exp.notification in the top-level React.Component.

1 Like

#3

What do we have to initialize in order to get this? props.exp.notification

0 Likes

#4

When I wroteconsole.log(props.exp.notification); right after export default class App extends React.Component { it said, Syntax Error. When I wrote it insidecomponentDidMount, it said props not defined.

0 Likes

closed #5

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.

0 Likes