index.android.js 2.3 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',
  },
19 20 21 22 23 24 25
  titleText: {
    fontSize: 22,
    textAlign: 'center',
    margin: 10,
  },
  bodyText: {
    fontSize: 18,
Lidan Hifi's avatar
Lidan Hifi committed
26 27 28 29 30
    textAlign: 'center',
    margin: 10,
  },
});

Amit Davidi's avatar
Amit Davidi committed
31
class MainComponent extends Component {
32 33 34 35 36 37 38 39 40 41 42 43 44 45

  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));
46
  }
47

48
  componentDidMount() {
49
    setInterval(this.onTick.bind(this), 1000);
50 51 52
    PendingNotifications.getInitialNotification()
      .then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: notification.getData()});})
      .catch((err) => console.error("getInitialNotifiation failed", err));
53 54 55 56 57 58
  }

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

Amit Davidi's avatar
Amit Davidi committed
59 60 61
  render() {
    return (
      <View style={styles.container}>
62 63 64 65
        <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
66 67 68
      </View>
    )
  }
69 70 71 72 73 74 75 76 77 78 79 80

  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
81 82 83
}

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