index.js 3.48 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 15 16 17
    this.state = {
      notifications: []
    };

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

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

yogevbd's avatar
yogevbd committed
28 29
      completion({alert: true, sound: false, badge: false});
    });
Lidan Hifi's avatar
Lidan Hifi committed
30

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

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

yogevbd's avatar
yogevbd committed
40
  renderNotification(notification) {
41 42 43 44 45 46 47
    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>
    );
Lidan Hifi's avatar
Lidan Hifi committed
48 49
  }

yogevbd's avatar
yogevbd committed
50
  requestPermissions() {
51
    Notifications.ios.requestPermissions();
yogevbd's avatar
WIP  
yogevbd committed
52 53
  }

yogevbd's avatar
yogevbd committed
54 55 56 57 58 59
  setCategories() {
    const upvoteAction = {
      activationMode: 'background',
      title: String.fromCodePoint(0x1F44D),
      identifier: 'UPVOTE_ACTION'
    };
60

yogevbd's avatar
yogevbd committed
61 62 63 64 65 66 67 68 69 70
    const replyAction = {
      activationMode: 'background',
      title: 'Reply',
      authenticationRequired: true,
      textInput: {
        buttonTitle: 'Reply now',
        placeholder: 'Insert message'
      },
      identifier: 'REPLY_ACTION'
    };
71

yogevbd's avatar
yogevbd committed
72 73 74 75
    const category = {
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction]
    };
76

yogevbd's avatar
yogevbd committed
77
    Notifications.setCategories([category]);
yogevbd's avatar
yogevbd committed
78 79
  }

yogevbd's avatar
yogevbd committed
80
  sendLocalNotification() {
yogevbd's avatar
yogevbd committed
81
    Notifications.postLocalNotification({
yogevbd's avatar
yogevbd committed
82 83 84 85
      body: 'Local notificiation!',
      title: 'Local Notification Title',
      sound: 'chime.aiff',
      category: 'SOME_CATEGORY',
yogevbd's avatar
yogevbd committed
86
      link: 'localNotificationLink',
87
    });
yogevbd's avatar
yogevbd committed
88
  }
yogevbd's avatar
WIP  
yogevbd committed
89

yogevbd's avatar
yogevbd committed
90 91
  removeAllDeliveredNotifications() {
    Notifications.removeAllDeliveredNotifications();
Lidan Hifi's avatar
Lidan Hifi committed
92 93
  }

yogevbd's avatar
yogevbd committed
94 95 96
  async componentDidMount() {
    const initialNotification = await Notifications.getInitialNotification();
    if (initialNotification) {
97
      this.setState({notifications: [initialNotification, ...this.state.notifications]});
yogevbd's avatar
yogevbd committed
98
    }
Lidan Hifi's avatar
Lidan Hifi committed
99 100 101
  }

  render() {
yogevbd's avatar
WIP  
yogevbd committed
102 103 104 105 106 107 108
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));

Lidan Hifi's avatar
Lidan Hifi committed
109 110
    return (
      <View style={styles.container}>
111 112 113
        <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
114
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      </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
139
AppRegistry.registerComponent('NotificationsExampleApp', () => NotificationsExampleApp);