index.js 4.11 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1
import {
Lidan Hifi's avatar
Lidan Hifi committed
2 3
  AppRegistry,
  StyleSheet,
4
  View,
yogevbd's avatar
WIP  
yogevbd committed
5 6
  Text,
  Button
Lidan Hifi's avatar
Lidan Hifi committed
7
} from 'react-native';
Lidan Hifi's avatar
Lidan Hifi committed
8
import React, {Component} from 'react';
9
import {Notifications} from 'react-native-notifications';
yogevbd's avatar
yogevbd committed
10

Lidan Hifi's avatar
Lidan Hifi committed
11
class NotificationsExampleApp extends Component {
Lidan Hifi's avatar
Lidan Hifi committed
12 13
  constructor() {
    super();
yogevbd's avatar
WIP  
yogevbd committed
14
    this.state = {
yogevbd's avatar
yogevbd committed
15 16
      notifications: [],
      openedNotifications: [],
yogevbd's avatar
WIP  
yogevbd committed
17 18
    };

yogevbd's avatar
yogevbd committed
19
    this.registerNotificationEvents();
yogevbd's avatar
yogevbd committed
20
    this.setCategories();
yogevbd's avatar
yogevbd committed
21
  }
22

yogevbd's avatar
yogevbd committed
23 24 25
  registerNotificationEvents() {
    Notifications.events().registerNotificationReceived((notification, completion) => {
      this.setState({
26
        notifications: [...this.state.notifications, notification]
yogevbd's avatar
yogevbd committed
27
      });
28

yogevbd's avatar
yogevbd committed
29
      completion({alert: notification.data.showAlert, sound: false, badge: false});
yogevbd's avatar
yogevbd committed
30
    });
Lidan Hifi's avatar
Lidan Hifi committed
31

yogevbd's avatar
yogevbd committed
32
    Notifications.events().registerRemoteNotificationOpened((notification, completion) => {
yogevbd's avatar
yogevbd committed
33
      this.setState({
yogevbd's avatar
yogevbd committed
34
        openedNotifications: [...this.state.openedNotifications, notification]
yogevbd's avatar
yogevbd committed
35
      });
36

yogevbd's avatar
yogevbd committed
37 38
      completion();
    });
39 40
  }

yogevbd's avatar
yogevbd committed
41
  requestPermissions() {
42
    Notifications.ios.requestPermissions();
yogevbd's avatar
WIP  
yogevbd committed
43 44
  }

yogevbd's avatar
yogevbd committed
45 46 47 48 49 50
  setCategories() {
    const upvoteAction = {
      activationMode: 'background',
      title: String.fromCodePoint(0x1F44D),
      identifier: 'UPVOTE_ACTION'
    };
51

yogevbd's avatar
yogevbd committed
52 53 54 55 56 57 58 59 60 61
    const replyAction = {
      activationMode: 'background',
      title: 'Reply',
      authenticationRequired: true,
      textInput: {
        buttonTitle: 'Reply now',
        placeholder: 'Insert message'
      },
      identifier: 'REPLY_ACTION'
    };
62

yogevbd's avatar
yogevbd committed
63 64 65 66
    const category = {
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction]
    };
67

yogevbd's avatar
yogevbd committed
68
    Notifications.setCategories([category]);
yogevbd's avatar
yogevbd committed
69 70
  }

yogevbd's avatar
yogevbd committed
71
  sendLocalNotification() {
yogevbd's avatar
yogevbd committed
72
    Notifications.postLocalNotification({
yogevbd's avatar
yogevbd committed
73 74 75 76
      body: 'Local notificiation!',
      title: 'Local Notification Title',
      sound: 'chime.aiff',
      category: 'SOME_CATEGORY',
yogevbd's avatar
yogevbd committed
77
      link: 'localNotificationLink',
78
    });
yogevbd's avatar
yogevbd committed
79
  }
yogevbd's avatar
WIP  
yogevbd committed
80

yogevbd's avatar
yogevbd committed
81 82
  removeAllDeliveredNotifications() {
    Notifications.removeAllDeliveredNotifications();
Lidan Hifi's avatar
Lidan Hifi committed
83 84
  }

yogevbd's avatar
yogevbd committed
85 86 87
  async componentDidMount() {
    const initialNotification = await Notifications.getInitialNotification();
    if (initialNotification) {
88
      this.setState({notifications: [initialNotification, ...this.state.notifications]});
yogevbd's avatar
yogevbd committed
89
    }
Lidan Hifi's avatar
Lidan Hifi committed
90 91
  }

yogevbd's avatar
yogevbd committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  renderNotification(notification) {
    return (
      <View style={{backgroundColor: 'lightgray', margin: 10}}>
        <Text>{`Title: ${notification.title}`}</Text>
        <Text>{`Body: ${notification.body}`}</Text>
        <Text>{`Extra Link Param: ${notification.data.link}`}</Text>
      </View>
    );
  }

  renderOpenedNotification(notification) {
    return (
      <View style={{backgroundColor: 'lightgray', margin: 10}}>
        <Text>{`Title: ${notification.title}`}</Text>
        <Text>{`Body: ${notification.body}`}</Text>
        <Text>{`Notification Clicked: ${notification.data.link}`}</Text>
      </View>
    );
  }

Lidan Hifi's avatar
Lidan Hifi committed
112
  render() {
yogevbd's avatar
WIP  
yogevbd committed
113 114 115 116 117 118
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));
yogevbd's avatar
yogevbd committed
119 120 121 122 123 124
      const openedNotifications = this.state.openedNotifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderOpenedNotification(notification)}
        </View>
      ));
Lidan Hifi's avatar
Lidan Hifi committed
125 126
    return (
      <View style={styles.container}>
127 128 129
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'} />
        <Button title={'Send local notification'} onPress={this.sendLocalNotification} testID={'sendLocalNotification'} />
        <Button title={'Remove all delivered notifications'} onPress={this.removeAllDeliveredNotifications} />
yogevbd's avatar
WIP  
yogevbd committed
130
        {notifications}
yogevbd's avatar
yogevbd committed
131
        {openedNotifications}
Lidan Hifi's avatar
Lidan Hifi committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

Lidan Hifi's avatar
Lidan Hifi committed
156
AppRegistry.registerComponent('NotificationsExampleApp', () => NotificationsExampleApp);