index.android.js 4.51 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
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
13

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

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

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

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

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

Lidan Hifi's avatar
Lidan Hifi committed
40 41 42 43 44
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
45
  titleText: {
46
    fontSize: 24,
47 48 49 50 51
    textAlign: 'center',
    margin: 10,
  },
  bodyText: {
    fontSize: 18,
Lidan Hifi's avatar
Lidan Hifi committed
52 53 54
    textAlign: 'center',
    margin: 10,
  },
55 56 57 58 59 60 61 62 63 64 65 66 67
  mainButtonText: {
    fontSize: 25,
    fontStyle: 'italic',
    fontWeight: 'bold',
    textAlign: 'center',
    margin: 10,
  },
  plainButtonText: {
    fontSize: 18,
    fontStyle: 'italic',
    textAlign: 'center',
    margin: 10,
  },
Lidan Hifi's avatar
Lidan Hifi committed
68 69
});

Amit Davidi's avatar
Amit Davidi committed
70
class MainComponent extends Component {
71 72 73 74

  constructor(props) {
    super(props);

75 76 77
    this.onPostNotification = this.onPostNotification.bind(this);
    this.onCancelNotification = this.onCancelNotification.bind(this);

78 79 80
    this.state = {
      elapsed: 0,
      lastNotification: undefined
81
    };
82

83 84
    console.log('ReactScreen', 'ReactScreen');
    mainScreen = this;
85 86

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

89
  componentDidMount() {
90
    console.log('ReactScreen', 'componentDidMount');
91
    PendingNotifications.getInitialNotification()
92
      .then((notification) => {console.log("getInitialNotification:", notification); this.setState({initialNotification: (notification ? notification.getData() : undefined)});})
93
      .catch((err) => console.error("getInitialNotifiation failed", err));
94 95
  }

96 97 98 99
  componentWillUnmount() {
    console.log('ReactScreen', 'componentWillUnmount');
  }

100 101 102 103
  onTick() {
    this.setState({elapsed: this.state.elapsed + 1});
  }

104 105 106 107 108 109 110 111 112 113 114
  onPostNotification() {
    this.lastNotificationId = NotificationsAndroid.localNotification({title: "Local notification", body: "This notification was generated by the app!"});
  }

  onCancelNotification() {
    if (this.lastNotificationId) {
      NotificationsAndroid.cancelLocalNotification(this.lastNotificationId);
      this.lastNotificationId = undefined;
    }
  }

Amit Davidi's avatar
Amit Davidi committed
115 116 117
  render() {
    return (
      <View style={styles.container}>
118 119 120 121
        <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>
122 123 124 125 126 127 128
        <Text>{"\n\n"}</Text>
        <TouchableHighlight onPress={() => this.onPostNotification()}>
          <Text style={styles.mainButtonText}>Try Me!</Text>
        </TouchableHighlight>
        <TouchableHighlight onPress={() => this.onCancelNotification()}>
          <Text style={styles.plainButtonText}>Undo last</Text>
        </TouchableHighlight>
129 130 131
        <TouchableHighlight onPress={() => this.onCheckPermissions()}>
          <Text style={styles.plainButtonText}>Check permissions</Text>
        </TouchableHighlight>
Amit Davidi's avatar
Amit Davidi committed
132 133 134
      </View>
    )
  }
135

136 137 138 139 140 141 142 143 144
  async onCheckPermissions() {
    const hasPermissions = await NotificationsAndroid.isRegisteredForRemoteNotifications();
    if (hasPermissions) {
      alert('Yay! You have permissions');
    } else {
      alert('Boo! You don\'t have permissions');
    }
  }

145 146 147 148 149 150 151 152 153 154 155
  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
156 157 158
}

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