index.android.js 1.96 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';

Lidan Hifi's avatar
Lidan Hifi committed
14 15 16 17 18
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
Amit Davidi's avatar
Amit Davidi committed
19
  mainText: {
Lidan Hifi's avatar
Lidan Hifi committed
20 21 22 23 24 25
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
});

Amit Davidi's avatar
Amit Davidi committed
26
class MainComponent extends Component {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  constructor(props) {
    super(props);

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

  componentWillMount() {
    NotificationsAndroid.setRegistrationTokenUpdateListener(this.onPushRegistered.bind(this));
    NotificationsAndroid.setNotificationOpenedListener(this.onNotificationOpened.bind(this));
    NotificationsAndroid.setNotificationReceivedListener(this.onNotificationReceived.bind(this));

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

  onTick() {
    this.setState({elapsed: this.state.elapsed + 1});
  }

Amit Davidi's avatar
Amit Davidi committed
49 50 51
  render() {
    return (
      <View style={styles.container}>
52 53 54 55 56 57
        <TouchableHighlight onPress={() => {console.log("Touch")}}>
          <Text style={styles.mainText}>Wix React Native Notifications</Text>
        </TouchableHighlight>
        <Text style={styles.mainText}>Last notification:</Text>
        <Text style={styles.mainText}>{this.state.lastNotification ? this.state.lastNotification.body + ` (opened at ''${this.state.notificationRxTime})` : "N/A"}</Text>
        <Text style={styles.mainText}>Time elapsed: {this.state.elapsed}</Text>
Amit Davidi's avatar
Amit Davidi committed
58 59 60
      </View>
    )
  }
61 62 63 64 65 66 67 68 69 70 71 72

  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
73 74 75
}

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