index.ios.js 3.33 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 19
    this.registerNotificationEvents();
  }
20

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

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

yogevbd's avatar
yogevbd committed
30 31 32 33 34 35 36
    Notifications.events().registerRemoteNotificationOpened((response, completion) => {
      this.setState({
        notifications: [...this.state.notifications, `Notification Clicked: ${response.notification.data.link}`]
      });
  
      completion();
    });
37 38
  }

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

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

yogevbd's avatar
yogevbd committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  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]
    };
69

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

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

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

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

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

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

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