index.ios.js 4.54 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 17 18
});

let replyAction = new NotificationAction({
19 20
  activationMode: 'background',
  title: 'Reply',
21
  authenticationRequired: true,
yogevbd's avatar
WIP  
yogevbd committed
22 23 24 25
  textInput: {
    buttonTitle: 'Reply now',
    placeholder: 'Insert message'
  },
26
  identifier: 'REPLY_ACTION'
27 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: []
    };

37
    NotificationsIOS.addEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
yogevbd's avatar
WIP  
yogevbd committed
38
    NotificationsIOS.addEventListener('remoteNotificationsRegistrationFailed', this.onPushRegisteredFailed.bind(this));
39

40 41
    NotificationsIOS.consumeBackgroundQueue();

42
    NotificationsIOS.addEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
43
    NotificationsIOS.registerPushKit();
44

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

  onPushRegistered(deviceToken) {
51
    console.log('Device Token Received: ' + deviceToken);
Lidan Hifi's avatar
Lidan Hifi committed
52 53
  }

yogevbd's avatar
WIP  
yogevbd committed
54 55 56 57
  onPushRegisteredFailed(error) {
    console.log('Remote notifiction registration failed: ' + error);
  }

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

yogevbd's avatar
yogevbd committed
62 63 64 65
  onPushKitNotificationReceived(notification) {
    console.log('PushKit notification Received: ' + JSON.stringify(notification));
  }

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

yogevbd's avatar
WIP  
yogevbd committed
72
    completion({alert: true, sound: false, badge: false});
Lidan Hifi's avatar
Lidan Hifi committed
73 74
  }

yogevbd's avatar
WIP  
yogevbd committed
75 76 77
  onNotificationOpened(notification, completion, action) {
    console.log('Notification Opened: ' + JSON.stringify(notification) + JSON.stringify(action));
    completion();
Lidan Hifi's avatar
Lidan Hifi committed
78 79
  }

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

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

Lidan Hifi's avatar
Lidan Hifi committed
92 93
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
94
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
yogevbd's avatar
WIP  
yogevbd committed
95 96
        <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
97
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
98 99 100 101
      </View>
    );
  }

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

yogevbd's avatar
WIP  
yogevbd committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123
  sendLocalNotification() {
    NotificationsIOS.localNotification({
      body: 'Local notificiation!',
      title: 'Local Notification Title',
      sound: 'chime.aiff',
      category: 'SOME_CATEGORY',
      userInfo: { }
    });
  }

  removeAllDeliveredNotifications() {
    NotificationsIOS.removeAllDeliveredNotifications();
  }

Lidan Hifi's avatar
Lidan Hifi committed
124
  componentWillUnmount() {
Lidan Hifi's avatar
Lidan Hifi committed
125 126
    NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
127 128
    NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  }
}

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