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

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

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

48 49 50 51 52 53 54 55
  async componentDidMount() {
    const initialNotification = await NotificationsIOS.getInitialNotification();
    if (initialNotification) {
      this.setState({notifications: [initialNotification.getData().link, ...this.state.notifications]});
    }

  }

Lidan Hifi's avatar
Lidan Hifi committed
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
  }

yogevbd's avatar
yogevbd committed
68 69 70 71
  onPushKitNotificationReceived(notification) {
    console.log('PushKit notification Received: ' + JSON.stringify(notification));
  }

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

78
    completion({alert: notification.getData().showAlert, sound: false, badge: false});
Lidan Hifi's avatar
Lidan Hifi committed
79 80
  }

yogevbd's avatar
WIP  
yogevbd committed
81 82
  onNotificationOpened(notification, completion, action) {
    console.log('Notification Opened: ' + JSON.stringify(notification) + JSON.stringify(action));
83 84 85
    this.setState({
      notifications: [...this.state.notifications, `Notification Clicked: ${notification.getData().link}`]
    });
yogevbd's avatar
WIP  
yogevbd committed
86
    completion();
Lidan Hifi's avatar
Lidan Hifi committed
87 88
  }

yogevbd's avatar
WIP  
yogevbd committed
89
  renderNotification(notification) {
90
    return <Text>{`${notification}`}</Text>;
yogevbd's avatar
WIP  
yogevbd committed
91 92
  }

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

Lidan Hifi's avatar
Lidan Hifi committed
101 102
    return (
      <View style={styles.container}>
yogevbd's avatar
WIP  
yogevbd committed
103
        <Button title={'Request permissions'} onPress={this.requestPermissions} testID={'requestPermissions'}/>
yogevbd's avatar
WIP  
yogevbd committed
104 105
        <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
106
        {notifications}
Lidan Hifi's avatar
Lidan Hifi committed
107 108 109 110
      </View>
    );
  }

yogevbd's avatar
WIP  
yogevbd committed
111 112 113
  requestPermissions() {
    let cat = new NotificationCategory({
      identifier: 'SOME_CATEGORY',
yogevbd's avatar
yogevbd committed
114
      actions: [upvoteAction, replyAction]
yogevbd's avatar
WIP  
yogevbd committed
115 116 117 118
    });
    NotificationsIOS.requestPermissions([cat]);
  }

yogevbd's avatar
WIP  
yogevbd committed
119 120 121 122 123 124
  sendLocalNotification() {
    NotificationsIOS.localNotification({
      body: 'Local notificiation!',
      title: 'Local Notification Title',
      sound: 'chime.aiff',
      category: 'SOME_CATEGORY',
yogevbd's avatar
yogevbd committed
125
      userInfo: { link: 'localNotificationLink' },
yogevbd's avatar
WIP  
yogevbd committed
126 127 128 129 130 131 132
    });
  }

  removeAllDeliveredNotifications() {
    NotificationsIOS.removeAllDeliveredNotifications();
  }

Lidan Hifi's avatar
Lidan Hifi committed
133
  componentWillUnmount() {
Lidan Hifi's avatar
Lidan Hifi committed
134 135
    NotificationsIOS.removeEventListener('notificationReceivedForeground', this.onNotificationReceivedForeground.bind(this));
    NotificationsIOS.removeEventListener('notificationOpened', this.onNotificationOpened.bind(this));
136 137
    NotificationsIOS.removeEventListener('remoteNotificationsRegistered', this.onPushRegistered.bind(this));
    NotificationsIOS.removeEventListener('pushKitRegistered', this.onPushKitRegistered.bind(this));
Lidan Hifi's avatar
Lidan Hifi committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  }
}

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