index.js 4.23 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, NotificationAction, NotificationCategory} 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.registerRemoteNotifications();
yogevbd's avatar
WIP  
yogevbd committed
43 44
  }

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

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

63 64

    const category = new NotificationCategory({
yogevbd's avatar
yogevbd committed
65 66
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction]
67
    });
68

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

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

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

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

yogevbd's avatar
yogevbd committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  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
113
  render() {
yogevbd's avatar
WIP  
yogevbd committed
114 115 116 117 118 119
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));
yogevbd's avatar
yogevbd committed
120 121 122 123 124 125
      const openedNotifications = this.state.openedNotifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderOpenedNotification(notification)}
        </View>
      ));
Lidan Hifi's avatar
Lidan Hifi committed
126 127
    return (
      <View style={styles.container}>
128 129 130
        <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
131
        {notifications}
yogevbd's avatar
yogevbd committed
132
        {openedNotifications}
Lidan Hifi's avatar
Lidan Hifi committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
      </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
157
AppRegistry.registerComponent('NotificationsExampleApp', () => NotificationsExampleApp);