index.android.js 2.94 KB
Newer Older
Amit Davidi's avatar
Amit Davidi committed
1
'use strict';
Lidan Hifi's avatar
Lidan Hifi committed
2

Amit Davidi's avatar
Amit Davidi committed
3
import React, {Component} from 'react';
Lidan Hifi's avatar
Lidan Hifi committed
4
import {
Lidan Hifi's avatar
Lidan Hifi committed
5 6 7
  AppRegistry,
  StyleSheet,
  Text,
8
  View
Lidan Hifi's avatar
Lidan Hifi committed
9 10
} from 'react-native';

11
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
let mainScreen;

function onPushRegistered() {
  if (mainScreen) {
    mainScreen.onPushRegistered();
  }
}

function onNotificationOpened(notification) {
  if (mainScreen) {
    mainScreen.onNotificationOpened(notification)
  }
}

function onNotificationReceived(notification) {
  if (mainScreen) {
    mainScreen.onNotificationReceived(notification)
  }
}

33 34
// It's highly recommended to keep listeners registration at global scope rather than at screen-scope seeing that
// component mount and unmount lifecycle tend to be asymmetric!
35 36 37 38
NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered);
NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened);
NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived);

Lidan Hifi's avatar
Lidan Hifi committed
39 40 41 42 43
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
44 45 46 47 48 49 50
  titleText: {
    fontSize: 22,
    textAlign: 'center',
    margin: 10,
  },
  bodyText: {
    fontSize: 18,
Lidan Hifi's avatar
Lidan Hifi committed
51 52 53 54 55
    textAlign: 'center',
    margin: 10,
  },
});

Amit Davidi's avatar
Amit Davidi committed
56
class MainComponent extends Component {
57 58 59 60 61 62 63

  constructor(props) {
    super(props);

    this.state = {
      elapsed: 0,
      lastNotification: undefined
64
    };
65

66 67
    console.log('ReactScreen', 'ReactScreen');
    mainScreen = this;
68 69

    setInterval(this.onTick.bind(this), 1000);
70
  }
71

72
  componentDidMount() {
73
    console.log('ReactScreen', 'componentDidMount');
74 75 76
    PendingNotifications.getInitialNotification()
      .then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: notification.getData()});})
      .catch((err) => console.error("getInitialNotifiation failed", err));
77 78
  }

79 80 81 82
  componentWillUnmount() {
    console.log('ReactScreen', 'componentWillUnmount');
  }

83 84 85 86
  onTick() {
    this.setState({elapsed: this.state.elapsed + 1});
  }

Amit Davidi's avatar
Amit Davidi committed
87 88 89
  render() {
    return (
      <View style={styles.container}>
90 91 92 93
        <Text style={styles.titleText}>Wix React Native Notifications</Text>
        <Text style={styles.bodyText}>{this.state.initialNotification ? 'Opened from notification' : ''}</Text>
        <Text style={styles.bodyText}>Last notification: {this.state.lastNotification ? '\n'+this.state.lastNotification.body + ` (opened at ''${this.state.notificationRxTime})` : "N/A"}</Text>
        <Text style={styles.bodyText}>Time elapsed: {this.state.elapsed}</Text>
Amit Davidi's avatar
Amit Davidi committed
94 95 96
      </View>
    )
  }
97 98 99 100 101 102 103 104 105 106 107 108

  onPushRegistered() {
  }

  onNotificationOpened(notification) {
    console.log("onNotificationOpened: ", notification);
    this.setState({lastNotification: notification.getData(), notificationRxTime: this.state.elapsed});
  }

  onNotificationReceived(notification) {
    console.log("onNotificationReceived: ", notification);
  }
Amit Davidi's avatar
Amit Davidi committed
109 110 111
}

AppRegistry.registerComponent('WixRNNotifications', () => MainComponent);