index.ios.js 4.06 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

10
import NotificationsIOS, { NotificationAction, NotificationCategory } from 'react-native-notifications';
Lidan Hifi's avatar
Lidan Hifi committed
11

12
let upvoteAction = new NotificationAction({
13
  activationMode: 'background',
14
  title: String.fromCodePoint(0x1F44D),
15
  identifier: 'UPVOTE_ACTION'
16
}, (action, completed) => {
17
  NotificationsIOS.log('ACTION RECEIVED');
18 19 20 21 22 23
  NotificationsIOS.log(JSON.stringify(action));

  completed();
});

let replyAction = new NotificationAction({
24 25 26
  activationMode: 'background',
  title: 'Reply',
  behavior: 'textInput',
27
  authenticationRequired: true,
28
  identifier: 'REPLY_ACTION'
29
}, (action, completed) => {
30
  console.log('ACTION RECEIVED');
31 32 33 34 35
  console.log(action);

  completed();
});

Lidan Hifi's avatar
Lidan Hifi committed
36
class NotificationsExampleApp extends Component {
Lidan Hifi's avatar
Lidan Hifi committed
37 38 39

  constructor() {
    super();
yogevbd's avatar
WIP  
yogevbd committed
40 41 42 43
    this.state = {
      notifications: []
    };

44
    NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
yogevbd's avatar
WIP  
yogevbd committed
45
    NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
46

47 48
    NotificationsIOS.consumeBackgroundQueue();

49
    NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
50
    NotificationsIOS.registerPushKit();
51

Lidan Hifi's avatar
Lidan Hifi committed
52 53
    NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
54 55 56
  }

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

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

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

Lidan Hifi's avatar
Lidan Hifi committed
68
  onNotificationReceivedForeground(notification) {
69
    console.log('Notification Received Foreground: ' + JSON.stringify(notification));
yogevbd's avatar
WIP  
yogevbd committed
70 71
    this.setState({
      notifications: [...this.state.notifications, notification]
72
    });
Lidan Hifi's avatar
Lidan Hifi committed
73 74 75
  }

  onNotificationOpened(notification) {
76
    console.log('Notification Opened: ' + JSON.stringify(notification));
Lidan Hifi's avatar
Lidan Hifi committed
77 78
  }

yogevbd's avatar
WIP  
yogevbd committed
79 80 81 82
  renderNotification(notification) {
    return <Text>{`${''} | ${JSON.stringify(notification)}`}</Text>;
  }

Lidan Hifi's avatar
Lidan Hifi committed
83
  render() {
yogevbd's avatar
WIP  
yogevbd committed
84 85 86 87 88 89 90
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));

Lidan Hifi's avatar
Lidan Hifi committed
91 92
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
93 94
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
95 96 97 98
      </View>
    );
  }

yogevbd's avatar
WIP  
yogevbd committed
99 100 101 102 103 104 105 106 107
  requestPermissions() {
    let cat = new NotificationCategory({
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction],
      context: 'default'
    });
    NotificationsIOS.requestPermissions([cat]);
  }

Lidan Hifi's avatar
Lidan Hifi committed
108
  componentWillUnmount() {
Lidan Hifi's avatar
Lidan Hifi committed
109 110
    NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
111 112
    NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
113
    // NotificationsIOS.resetCategories();
Lidan Hifi's avatar
Lidan Hifi committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  }

  _onNotification(notification) {
    AlertIOS.alert(
      'Notification Received',
      'Alert message: ' + notification.getMessage(),
      [{
        text: 'Dismiss',
        onPress: null,
      }]
    );
  }
}

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