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

13
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
14

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

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

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

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

35
// 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
36
// component mount and unmount lifecycle tends to be asymmetric!
37 38 39 40
NotificationsAndroid.setRegistrationTokenUpdateListener(onPushRegistered);
NotificationsAndroid.setNotificationOpenedListener(onNotificationOpened);
NotificationsAndroid.setNotificationReceivedListener(onNotificationReceived);

Lidan Hifi's avatar
Lidan Hifi committed
41 42 43 44 45
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
46
  titleText: {
47
    fontSize: 24,
48 49 50 51 52
    textAlign: 'center',
    margin: 10,
  },
  bodyText: {
    fontSize: 18,
Lidan Hifi's avatar
Lidan Hifi committed
53 54 55
    textAlign: 'center',
    margin: 10,
  },
56 57 58 59 60 61 62 63 64 65 66 67 68
  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
69 70
});

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

  constructor(props) {
    super(props);

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

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

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

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

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

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

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

105 106 107 108 109 110 111 112 113 114 115
  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
116 117 118
  render() {
    return (
      <View style={styles.container}>
119 120 121 122
        <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>
123 124 125 126 127 128 129
        <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>
130 131 132
        <TouchableHighlight onPress={() => this.onCheckPermissions()}>
          <Text style={styles.plainButtonText}>Check permissions</Text>
        </TouchableHighlight>
133
        <Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'}/>
Amit Davidi's avatar
Amit Davidi committed
134 135 136
      </View>
    )
  }
137

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

147 148 149 150 151 152 153 154
  sendLocalNotification() {
    NotificationsAndroid.localNotification({
      title: "Local notification",
      body: "This notification was generated by the app!",
      extra: "data"
    });
  }

155 156 157 158 159 160 161 162 163 164 165
  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
166 167 168
}

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