index.js 3.32 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';
yogevbd's avatar
yogevbd committed
9 10
import { Notifications } from '../lib/dist/index';

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({
yogevbd's avatar
yogevbd committed
25
        notifications: [...this.state.notifications, notification.link]
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({
yogevbd's avatar
yogevbd committed
33
        notifications: [...this.state.notifications, `Notification Clicked: ${notification.link}`]
yogevbd's avatar
yogevbd committed
34 35 36 37
      });
  
      completion();
    });
38 39
  }

yogevbd's avatar
yogevbd committed
40 41
  renderNotification(notification) {
    return <Text>{`${notification}`}</Text>;
Lidan Hifi's avatar
Lidan Hifi committed
42 43
  }

yogevbd's avatar
yogevbd committed
44 45
  requestPermissions() {
    Notifications.requestPermissions();
yogevbd's avatar
WIP  
yogevbd committed
46 47
  }

yogevbd's avatar
yogevbd committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  setCategories() {
    const upvoteAction = {
      activationMode: 'background',
      title: String.fromCodePoint(0x1F44D),
      identifier: 'UPVOTE_ACTION'
    };
    
    const replyAction = {
      activationMode: 'background',
      title: 'Reply',
      authenticationRequired: true,
      textInput: {
        buttonTitle: 'Reply now',
        placeholder: 'Insert message'
      },
      identifier: 'REPLY_ACTION'
    };
    
    const category = {
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction]
    };
70

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

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

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

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

yogevbd's avatar
yogevbd committed
95
  componentWillUnmount() {
yogevbd's avatar
WIP  
yogevbd committed
96 97
  }

Lidan Hifi's avatar
Lidan Hifi committed
98
  render() {
yogevbd's avatar
WIP  
yogevbd committed
99 100 101 102 103 104 105
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));

Lidan Hifi's avatar
Lidan Hifi committed
106 107
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
108
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
yogevbd's avatar
WIP  
yogevbd committed
109 110
        <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
111
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      </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
136
AppRegistry.registerComponent('NotificationsExampleApp', () => NotificationsExampleApp);