index.ios.js 4.35 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 21
  activationMode: 'background',
  title: 'Reply',
  behavior: 'textInput',
22
  authenticationRequired: true,
yogevbd's avatar
WIP  
yogevbd committed
23 24 25 26
  textInput: {
    buttonTitle: 'Reply now',
    placeholder: 'Insert message'
  },
27
  identifier: 'REPLY_ACTION'
28 29
});

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

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

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

41 42
    NotificationsIOS.consumeBackgroundQueue();

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

Lidan Hifi's avatar
Lidan Hifi committed
46 47
    NotificationsIOS.addEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.addEventListener('notificationOpened', this.onNotificationOpened.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
WIP  
yogevbd committed
62
  onNotificationReceivedForeground(notification, completion) {
63
    console.log('Notification Received Foreground: ' + JSON.stringify(notification));
yogevbd's avatar
WIP  
yogevbd committed
64 65
    this.setState({
      notifications: [...this.state.notifications, notification]
66
    });
yogevbd's avatar
WIP  
yogevbd committed
67

yogevbd's avatar
WIP  
yogevbd committed
68
    completion({alert: true, sound: false, badge: false});
Lidan Hifi's avatar
Lidan Hifi committed
69 70
  }

yogevbd's avatar
WIP  
yogevbd committed
71 72 73
  onNotificationOpened(notification, completion, action) {
    console.log('Notification Opened: ' + JSON.stringify(notification) + JSON.stringify(action));
    completion();
Lidan Hifi's avatar
Lidan Hifi committed
74 75
  }

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

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

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

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

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

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