2015-03-24 16:26:16 +00:00
|
|
|
/**
|
2017-05-06 03:50:47 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-07-12 12:51:57 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
2015-03-24 16:26:16 +00:00
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule PushNotificationIOSExample
|
2015-03-24 16:26:16 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-03-24 16:26:16 +00:00
|
|
|
var {
|
|
|
|
AlertIOS,
|
|
|
|
PushNotificationIOS,
|
|
|
|
StyleSheet,
|
|
|
|
Text,
|
|
|
|
TouchableHighlight,
|
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
2015-03-24 16:26:16 +00:00
|
|
|
|
2016-07-26 08:00:02 +00:00
|
|
|
class Button extends React.Component {
|
|
|
|
render() {
|
2015-03-24 16:26:16 +00:00
|
|
|
return (
|
|
|
|
<TouchableHighlight
|
|
|
|
underlayColor={'white'}
|
|
|
|
style={styles.button}
|
|
|
|
onPress={this.props.onPress}>
|
|
|
|
<Text style={styles.buttonLabel}>
|
|
|
|
{this.props.label}
|
|
|
|
</Text>
|
|
|
|
</TouchableHighlight>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-24 16:26:16 +00:00
|
|
|
|
|
|
|
class NotificationExample extends React.Component {
|
|
|
|
componentWillMount() {
|
2016-09-06 18:01:31 +00:00
|
|
|
PushNotificationIOS.addEventListener('register', this._onRegistered);
|
|
|
|
PushNotificationIOS.addEventListener('registrationError', this._onRegistrationError);
|
|
|
|
PushNotificationIOS.addEventListener('notification', this._onRemoteNotification);
|
2016-04-22 16:15:37 +00:00
|
|
|
PushNotificationIOS.addEventListener('localNotification', this._onLocalNotification);
|
2016-09-06 18:01:31 +00:00
|
|
|
|
|
|
|
PushNotificationIOS.requestPermissions();
|
2015-03-24 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2016-09-06 18:01:31 +00:00
|
|
|
PushNotificationIOS.removeEventListener('register', this._onRegistered);
|
|
|
|
PushNotificationIOS.removeEventListener('registrationError', this._onRegistrationError);
|
|
|
|
PushNotificationIOS.removeEventListener('notification', this._onRemoteNotification);
|
2016-04-22 16:15:37 +00:00
|
|
|
PushNotificationIOS.removeEventListener('localNotification', this._onLocalNotification);
|
2015-03-24 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Button
|
|
|
|
onPress={this._sendNotification}
|
|
|
|
label="Send fake notification"
|
|
|
|
/>
|
2016-04-22 16:15:37 +00:00
|
|
|
|
|
|
|
<Button
|
|
|
|
onPress={this._sendLocalNotification}
|
|
|
|
label="Send fake local notification"
|
|
|
|
/>
|
2015-03-24 16:26:16 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendNotification() {
|
|
|
|
require('RCTDeviceEventEmitter').emit('remoteNotificationReceived', {
|
2017-06-19 23:37:31 +00:00
|
|
|
remote: true,
|
2015-03-24 16:26:16 +00:00
|
|
|
aps: {
|
|
|
|
alert: 'Sample notification',
|
|
|
|
badge: '+1',
|
|
|
|
sound: 'default',
|
2017-06-19 23:37:31 +00:00
|
|
|
category: 'REACT_NATIVE',
|
|
|
|
'content-available': 1,
|
2015-03-24 16:26:16 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:15:37 +00:00
|
|
|
_sendLocalNotification() {
|
|
|
|
require('RCTDeviceEventEmitter').emit('localNotificationReceived', {
|
|
|
|
aps: {
|
|
|
|
alert: 'Sample local notification',
|
|
|
|
badge: '+1',
|
|
|
|
sound: 'default',
|
|
|
|
category: 'REACT_NATIVE'
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:01:31 +00:00
|
|
|
_onRegistered(deviceToken) {
|
|
|
|
AlertIOS.alert(
|
|
|
|
'Registered For Remote Push',
|
|
|
|
`Device Token: ${deviceToken}`,
|
|
|
|
[{
|
|
|
|
text: 'Dismiss',
|
|
|
|
onPress: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onRegistrationError(error) {
|
|
|
|
AlertIOS.alert(
|
|
|
|
'Failed To Register For Remote Push',
|
|
|
|
`Error (${error.code}): ${error.message}`,
|
|
|
|
[{
|
|
|
|
text: 'Dismiss',
|
|
|
|
onPress: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onRemoteNotification(notification) {
|
2017-06-19 23:37:31 +00:00
|
|
|
const result = `Message: ${notification.getMessage()};\n
|
|
|
|
badge: ${notification.getBadgeCount()};\n
|
|
|
|
sound: ${notification.getSound()};\n
|
|
|
|
category: ${notification.getCategory()};\n
|
|
|
|
content-available: ${notification.getContentAvailable()}.`;
|
|
|
|
|
2015-03-24 16:26:16 +00:00
|
|
|
AlertIOS.alert(
|
2016-04-22 16:15:37 +00:00
|
|
|
'Push Notification Received',
|
2017-06-19 23:37:31 +00:00
|
|
|
result,
|
2016-04-22 16:15:37 +00:00
|
|
|
[{
|
|
|
|
text: 'Dismiss',
|
|
|
|
onPress: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onLocalNotification(notification){
|
|
|
|
AlertIOS.alert(
|
|
|
|
'Local Notification Received',
|
2015-04-02 01:37:03 +00:00
|
|
|
'Alert message: ' + notification.getMessage(),
|
2015-03-24 16:26:16 +00:00
|
|
|
[{
|
|
|
|
text: 'Dismiss',
|
|
|
|
onPress: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NotificationPermissionExample extends React.Component {
|
2016-02-25 10:44:58 +00:00
|
|
|
state: any;
|
|
|
|
|
2015-03-24 16:26:16 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {permissions: null};
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Button
|
|
|
|
onPress={this._showPermissions.bind(this)}
|
|
|
|
label="Show enabled permissions"
|
|
|
|
/>
|
|
|
|
<Text>
|
|
|
|
{JSON.stringify(this.state.permissions)}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_showPermissions() {
|
|
|
|
PushNotificationIOS.checkPermissions((permissions) => {
|
|
|
|
this.setState({permissions});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
button: {
|
|
|
|
padding: 10,
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
buttonLabel: {
|
|
|
|
color: 'blue',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.title = 'PushNotificationIOS';
|
|
|
|
exports.description = 'Apple PushNotification and badge value';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Badge Number',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> {
|
2015-03-24 16:26:16 +00:00
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<Button
|
|
|
|
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
|
|
|
|
label="Set app's icon badge to 42"
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(0)}
|
|
|
|
label="Clear app's icon badge"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Push Notifications',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> {
|
2015-03-24 16:26:16 +00:00
|
|
|
return <NotificationExample />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Notifications Permissions',
|
2016-10-16 11:11:59 +00:00
|
|
|
render(): React.Element<any> {
|
2015-03-24 16:26:16 +00:00
|
|
|
return <NotificationPermissionExample />;
|
|
|
|
}
|
|
|
|
}];
|