index.android.js 2.78 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 9
  View,
  TouchableHighlight
Lidan Hifi's avatar
Lidan Hifi committed
10 11
} from 'react-native';

12 13
import {NotificationsAndroid, PendingNotifications} from './notifications';

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
let mainScreen;

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

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

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

NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered);
NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened);
NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived);

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

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

  constructor(props) {
    super(props);

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

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

69
  componentDidMount() {
70
    console.log('ReactScreen', 'componentDidMount');
71
    setInterval(this.onTick.bind(this), 1000);
72 73 74
    PendingNotifications.getInitialNotification()
      .then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: notification.getData()});})
      .catch((err) => console.error("getInitialNotifiation failed", err));
75 76
  }

77 78 79 80
  componentWillUnmount() {
    console.log('ReactScreen', 'componentWillUnmount');
  }

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

Amit Davidi's avatar
Amit Davidi committed
85 86 87
  render() {
    return (
      <View style={styles.container}>
88 89 90 91
        <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
92 93 94
      </View>
    )
  }
95 96 97 98 99 100 101 102 103 104 105 106

  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
107 108 109
}

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