index.ios.js 5.1 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';
Lidan Hifi's avatar
Lidan Hifi committed
9

yogevbd's avatar
yogevbd committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import { Notifications } from '../lib/dist/index';

// let upvoteAction = new NotificationAction({
//   activationMode: 'background',
//   title: String.fromCodePoint(0x1F44D),
//   identifier: 'UPVOTE_ACTION'
// });

// let replyAction = new NotificationAction({
//   activationMode: 'background',
//   title: 'Reply',
//   authenticationRequired: true,
//   textInput: {
//     buttonTitle: 'Reply now',
//     placeholder: 'Insert message'
//   },
//   identifier: 'REPLY_ACTION'
// });
28

Lidan Hifi's avatar
Lidan Hifi committed
29
class NotificationsExampleApp extends Component {
Lidan Hifi's avatar
Lidan Hifi committed
30 31 32

  constructor() {
    super();
yogevbd's avatar
WIP  
yogevbd committed
33 34 35 36
    this.state = {
      notifications: []
    };

yogevbd's avatar
yogevbd committed
37 38 39 40 41
    Notifications.events().registerNotificationsReceived((notification) => {
      alert(JSON.stringify(notification));
    })
    // NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    // NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
42

yogevbd's avatar
yogevbd committed
43 44
    // NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
    // NotificationsIOS.registerPushKit();
45

yogevbd's avatar
yogevbd committed
46 47 48
    // NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    // NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
    // NotificationsIOS.addEventListener('pushKitNotificationReceived', this.onPushKitNotificationReceived.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
49 50
  }

51
  async componentDidMount() {
yogevbd's avatar
yogevbd committed
52
    const initialNotification = await Notifications.getInitialNotification();
53 54 55 56 57
    if (initialNotification) {
      this.setState({notifications: [initialNotification.getData().link, ...this.state.notifications]});
    }
  }

Lidan Hifi's avatar
Lidan Hifi committed
58
  onPushRegistered(deviceToken) {
59
    console.log('Device Token Received: ' + deviceToken);
Lidan Hifi's avatar
Lidan Hifi committed
60 61
  }

yogevbd's avatar
WIP  
yogevbd committed
62 63 64 65
  onPushRegisteredFailed(error) {
    console.log('Remote notifiction registration failed: ' + error);
  }

66
  onPushKitRegistered(deviceToken) {
67
    console.log('PushKit Token Received: ' + deviceToken);
68 69
  }

yogevbd's avatar
yogevbd committed
70 71 72 73
  onPushKitNotificationReceived(notification) {
    console.log('PushKit notification Received: ' + JSON.stringify(notification));
  }

yogevbd's avatar
WIP  
yogevbd committed
74
  onNotificationReceivedForeground(notification, completion) {
75
    console.log('Notification Received Foreground with title: ' + JSON.stringify(notification));
yogevbd's avatar
WIP  
yogevbd committed
76
    this.setState({
77
      notifications: [...this.state.notifications, notification.getData().link]
78
    });
yogevbd's avatar
WIP  
yogevbd committed
79

80
    completion({alert: notification.getData().showAlert, sound: false, badge: false});
Lidan Hifi's avatar
Lidan Hifi committed
81 82
  }

yogevbd's avatar
WIP  
yogevbd committed
83 84
  onNotificationOpened(notification, completion, action) {
    console.log('Notification Opened: ' + JSON.stringify(notification) + JSON.stringify(action));
85 86 87
    this.setState({
      notifications: [...this.state.notifications, `Notification Clicked: ${notification.getData().link}`]
    });
yogevbd's avatar
WIP  
yogevbd committed
88
    completion();
Lidan Hifi's avatar
Lidan Hifi committed
89 90
  }

yogevbd's avatar
WIP  
yogevbd committed
91
  renderNotification(notification) {
92
    return <Text>{`${notification}`}</Text>;
yogevbd's avatar
WIP  
yogevbd committed
93 94
  }

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

Lidan Hifi's avatar
Lidan Hifi committed
103 104
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
105
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
yogevbd's avatar
WIP  
yogevbd committed
106 107
        <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
108
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
109 110 111 112
      </View>
    );
  }

yogevbd's avatar
WIP  
yogevbd committed
113
  requestPermissions() {
yogevbd's avatar
yogevbd committed
114 115 116 117 118
    // let cat = new NotificationCategory({
    //   identifier: 'SOME_CATEGORY',
    //   actions: [upvoteAction, replyAction]
    // });
    Notifications.requestPermissions(/*[cat]*/);
yogevbd's avatar
WIP  
yogevbd committed
119 120
  }

yogevbd's avatar
WIP  
yogevbd committed
121
  sendLocalNotification() {
yogevbd's avatar
yogevbd committed
122
    Notifications.localNotification({
yogevbd's avatar
WIP  
yogevbd committed
123 124 125 126
      body: 'Local notificiation!',
      title: 'Local Notification Title',
      sound: 'chime.aiff',
      category: 'SOME_CATEGORY',
yogevbd's avatar
yogevbd committed
127
      userInfo: { link: 'localNotificationLink' },
yogevbd's avatar
WIP  
yogevbd committed
128 129 130 131
    });
  }

  removeAllDeliveredNotifications() {
yogevbd's avatar
yogevbd committed
132
    // NotificationsIOS.removeAllDeliveredNotifications();
yogevbd's avatar
WIP  
yogevbd committed
133 134
  }

Lidan Hifi's avatar
Lidan Hifi committed
135
  componentWillUnmount() {
yogevbd's avatar
yogevbd committed
136 137 138 139
    // NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    // NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
    // NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    // NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  }
}

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
162
AppRegistry.registerComponent('NotificationsExampleApp', () => NotificationsExampleApp);