index.ios.js 4.17 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,
yogevbd's avatar
WIP  
yogevbd committed
28 29 30 31
  textInput: {
    buttonTitle: 'Reply now',
    placeholder: 'Insert message'
  },
32
  identifier: 'REPLY_ACTION'
33
}, (action, completed) => {
34
  console.log('ACTION RECEIVED');
35 36 37 38 39
  console.log(action);

  completed();
});

Lidan Hifi's avatar
Lidan Hifi committed
40
class NotificationsExampleApp extends Component {
Lidan Hifi's avatar
Lidan Hifi committed
41 42 43

  constructor() {
    super();
yogevbd's avatar
WIP  
yogevbd committed
44 45 46 47
    this.state = {
      notifications: []
    };

48
    NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
yogevbd's avatar
WIP  
yogevbd committed
49
    NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
50

51 52
    NotificationsIOS.consumeBackgroundQueue();

53
    NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
54
    NotificationsIOS.registerPushKit();
55

Lidan Hifi's avatar
Lidan Hifi committed
56 57
    NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
58 59 60
  }

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

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

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

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

    completion({});
Lidan Hifi's avatar
Lidan Hifi committed
79 80 81
  }

  onNotificationOpened(notification) {
82
    console.log('Notification Opened: ' + JSON.stringify(notification));
Lidan Hifi's avatar
Lidan Hifi committed
83 84
  }

yogevbd's avatar
WIP  
yogevbd committed
85 86 87 88
  renderNotification(notification) {
    return <Text>{`${''} | ${JSON.stringify(notification)}`}</Text>;
  }

Lidan Hifi's avatar
Lidan Hifi committed
89
  render() {
yogevbd's avatar
WIP  
yogevbd committed
90 91 92 93 94 95 96
    const notifications = this.state.notifications.map((notification, idx) =>
      (
        <View key={`notification_${idx}`}>
          {this.renderNotification(notification)}
        </View>
      ));

Lidan Hifi's avatar
Lidan Hifi committed
97 98
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
99 100
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
101 102 103 104
      </View>
    );
  }

yogevbd's avatar
WIP  
yogevbd committed
105 106 107 108 109 110 111 112 113
  requestPermissions() {
    let cat = new NotificationCategory({
      identifier: 'SOME_CATEGORY',
      actions: [upvoteAction, replyAction],
      context: 'default'
    });
    NotificationsIOS.requestPermissions([cat]);
  }

Lidan Hifi's avatar
Lidan Hifi committed
114
  componentWillUnmount() {
Lidan Hifi's avatar
Lidan Hifi committed
115 116
    NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
117 118
    NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
119
    // NotificationsIOS.resetCategories();
Lidan Hifi's avatar
Lidan Hifi committed
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 147 148 149 150 151 152
  }

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