index.ios.js 3.84 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 45
    NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));

46 47
    NotificationsIOS.consumeBackgroundQueue();

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

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

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

59
  onPushKitRegistered(deviceToken) {
60
    console.log('PushKit Token Received: ' + deviceToken);
61 62
  }

Lidan Hifi's avatar
Lidan Hifi committed
63
  onNotificationReceivedForeground(notification) {
64
    console.log('Notification Received Foreground: ' + JSON.stringify(notification));
yogevbd's avatar
WIP  
yogevbd committed
65 66
    this.setState({
      notifications: [...this.state.notifications, notification]
67
    });
Lidan Hifi's avatar
Lidan Hifi committed
68 69 70
  }

  onNotificationOpened(notification) {
71
    console.log('Notification Opened: ' + JSON.stringify(notification));
Lidan Hifi's avatar
Lidan Hifi committed
72 73
  }

yogevbd's avatar
WIP  
yogevbd committed
74 75 76 77
  renderNotification(notification) {
    return <Text>{`${''} | ${JSON.stringify(notification)}`}</Text>;
  }

Lidan Hifi's avatar
Lidan Hifi committed
78
  render() {
yogevbd's avatar
WIP  
yogevbd committed
79 80 81 82 83 84 85
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));

Lidan Hifi's avatar
Lidan Hifi committed
86 87
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
88 89
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
90 91 92 93
      </View>
    );
  }

yogevbd's avatar
WIP  
yogevbd committed
94 95 96 97 98 99 100 101 102
  requestPermissions() {
    let cat = new NotificationCategory({
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction],
      context: 'default'
    });
    NotificationsIOS.requestPermissions([cat]);
  }

Lidan Hifi's avatar
Lidan Hifi committed
103
  componentWillUnmount() {
Lidan Hifi's avatar
Lidan Hifi committed
104 105
    NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
106 107
    NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
108
    // NotificationsIOS.resetCategories();
Lidan Hifi's avatar
Lidan Hifi committed
109 110 111 112 113 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
  }

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