mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
[ReactNative] Open Source PushNotifications and move Badge Number methods and permission into it
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* 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.
|
||||
*
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var React = require('react-native');
|
||||
var {
|
||||
AppState,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TouchableHighlight,
|
||||
View,
|
||||
} = React;
|
||||
|
||||
var Button = React.createClass({
|
||||
render: function() {
|
||||
return (
|
||||
<TouchableHighlight
|
||||
underlayColor={'white'}
|
||||
style={styles.button}
|
||||
onPress={this.props.onPress}>
|
||||
<Text style={styles.buttonLabel}>
|
||||
{this.props.label}
|
||||
</Text>
|
||||
</TouchableHighlight>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
var styles = StyleSheet.create({
|
||||
button: {
|
||||
padding: 10,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
},
|
||||
buttonLabel: {
|
||||
color: 'blue',
|
||||
},
|
||||
});
|
||||
|
||||
exports.title = 'AppState';
|
||||
exports.description = 'App background status and badge value';
|
||||
exports.examples = [
|
||||
{
|
||||
title: 'Set Badge Number',
|
||||
render: function() {
|
||||
return (
|
||||
<View>
|
||||
<Button
|
||||
onPress={() => AppState.setApplicationIconBadgeNumber(42)}
|
||||
label="Set app's icon badge to 42"
|
||||
/>
|
||||
<Button
|
||||
onPress={() => AppState.setApplicationIconBadgeNumber(0)}
|
||||
label="Clear app's icon badge"
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
},
|
||||
}];
|
||||
152
Examples/UIExplorer/PushNotificationIOSExample.js
Normal file
152
Examples/UIExplorer/PushNotificationIOSExample.js
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* 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.
|
||||
*
|
||||
* @flow
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var React = require('react-native');
|
||||
var {
|
||||
AlertIOS,
|
||||
PushNotificationIOS,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TouchableHighlight,
|
||||
View,
|
||||
} = React;
|
||||
|
||||
var Button = React.createClass({
|
||||
render: function() {
|
||||
return (
|
||||
<TouchableHighlight
|
||||
underlayColor={'white'}
|
||||
style={styles.button}
|
||||
onPress={this.props.onPress}>
|
||||
<Text style={styles.buttonLabel}>
|
||||
{this.props.label}
|
||||
</Text>
|
||||
</TouchableHighlight>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
class NotificationExample extends React.Component {
|
||||
componentWillMount() {
|
||||
PushNotificationIOS.addEventListener('notification', this._onNotification);
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
PushNotificationIOS.removeEventListener('notification', this._onNotification);
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<View>
|
||||
<Button
|
||||
onPress={this._sendNotification}
|
||||
label="Send fake notification"
|
||||
/>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
_sendNotification() {
|
||||
require('RCTDeviceEventEmitter').emit('remoteNotificationReceived', {
|
||||
aps: {
|
||||
alert: 'Sample notification',
|
||||
badge: '+1',
|
||||
sound: 'default',
|
||||
category: 'REACT_NATIVE'
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
_onNotification(notification) {
|
||||
AlertIOS.alert(
|
||||
'Notification Received',
|
||||
`Alert message: ${notification.getMessage()}`,
|
||||
[{
|
||||
text: 'Dismiss',
|
||||
onPress: null,
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class NotificationPermissionExample extends React.Component {
|
||||
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',
|
||||
render(): React.Component {
|
||||
PushNotificationIOS.requestPermissions();
|
||||
|
||||
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',
|
||||
render(): React.Component {
|
||||
return <NotificationExample />;
|
||||
}
|
||||
},
|
||||
{
|
||||
title: 'Notifications Permissions',
|
||||
render(): React.Component {
|
||||
return <NotificationPermissionExample />;
|
||||
}
|
||||
}];
|
||||
@@ -21,6 +21,7 @@
|
||||
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
|
||||
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
|
||||
147CED4C1AB3532B00DA3E4C /* libRCTActionSheet.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 147CED4B1AB34F8C00DA3E4C /* libRCTActionSheet.a */; };
|
||||
14DC67F41AB71881001358AB /* libRCTPushNotification.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14DC67F11AB71876001358AB /* libRCTPushNotification.a */; };
|
||||
D85B829E1AB6D5D7003F4FE2 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D85B829C1AB6D5CE003F4FE2 /* libRCTVibration.a */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
@@ -88,6 +89,13 @@
|
||||
remoteGlobalIDString = 134814201AA4EA6300B7C361;
|
||||
remoteInfo = RCTActionSheet;
|
||||
};
|
||||
14DC67F01AB71876001358AB /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 14DC67E71AB71876001358AB /* RCTPushNotification.xcodeproj */;
|
||||
proxyType = 2;
|
||||
remoteGlobalIDString = 134814201AA4EA6300B7C361;
|
||||
remoteInfo = RCTPushNotification;
|
||||
};
|
||||
D85B829B1AB6D5CE003F4FE2 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = D85B82911AB6D5CE003F4FE2 /* RCTVibration.xcodeproj */;
|
||||
@@ -116,6 +124,7 @@
|
||||
13B07FB51A68108700A75B9A /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = UIExplorer/Images.xcassets; sourceTree = "<group>"; };
|
||||
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = UIExplorer/Info.plist; sourceTree = "<group>"; };
|
||||
13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = UIExplorer/main.m; sourceTree = "<group>"; };
|
||||
14DC67E71AB71876001358AB /* RCTPushNotification.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTPushNotification.xcodeproj; path = ../../Libraries/PushNotificationIOS/RCTPushNotification.xcodeproj; sourceTree = "<group>"; };
|
||||
14E0EEC81AB118F7000DECC3 /* RCTActionSheet.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTActionSheet.xcodeproj; path = ../../Libraries/ActionSheetIOS/RCTActionSheet.xcodeproj; sourceTree = "<group>"; };
|
||||
D85B82911AB6D5CE003F4FE2 /* RCTVibration.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTVibration.xcodeproj; path = ../../Libraries/Vibration/RCTVibration.xcodeproj; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
@@ -135,6 +144,7 @@
|
||||
00D2771C1AB8C55500DC1E48 /* libicucore.dylib in Frameworks */,
|
||||
00D2771A1AB8C3E100DC1E48 /* libRCTWebSocketDebugger.a in Frameworks */,
|
||||
D85B829E1AB6D5D7003F4FE2 /* libRCTVibration.a in Frameworks */,
|
||||
14DC67F41AB71881001358AB /* libRCTPushNotification.a in Frameworks */,
|
||||
147CED4C1AB3532B00DA3E4C /* libRCTActionSheet.a in Frameworks */,
|
||||
134454601AAFCABD003F0779 /* libRCTAdSupport.a in Frameworks */,
|
||||
134A8A2A1AACED7A00945AAE /* libRCTGeolocation.a in Frameworks */,
|
||||
@@ -177,6 +187,7 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
D85B82911AB6D5CE003F4FE2 /* RCTVibration.xcodeproj */,
|
||||
14DC67E71AB71876001358AB /* RCTPushNotification.xcodeproj */,
|
||||
14E0EEC81AB118F7000DECC3 /* RCTActionSheet.xcodeproj */,
|
||||
13417FFA1AA91531003F314A /* ReactKit.xcodeproj */,
|
||||
134454551AAFCAAE003F0779 /* RCTAdSupport.xcodeproj */,
|
||||
@@ -259,6 +270,14 @@
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
14DC67E81AB71876001358AB /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
14DC67F11AB71876001358AB /* libRCTPushNotification.a */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
83CBB9F61A601CBA00E9B192 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -371,6 +390,10 @@
|
||||
ProductGroup = 134180271AA91779003F314A /* Products */;
|
||||
ProjectRef = 134180261AA91779003F314A /* RCTNetwork.xcodeproj */;
|
||||
},
|
||||
{
|
||||
ProductGroup = 14DC67E81AB71876001358AB /* Products */;
|
||||
ProjectRef = 14DC67E71AB71876001358AB /* RCTPushNotification.xcodeproj */;
|
||||
},
|
||||
{
|
||||
ProductGroup = 13417FEB1AA914B8003F314A /* Products */;
|
||||
ProjectRef = 13417FEA1AA914B8003F314A /* RCTText.xcodeproj */;
|
||||
@@ -453,6 +476,13 @@
|
||||
remoteRef = 147CED4A1AB34F8C00DA3E4C /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
14DC67F11AB71876001358AB /* libRCTPushNotification.a */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = archive.ar;
|
||||
path = libRCTPushNotification.a;
|
||||
remoteRef = 14DC67F01AB71876001358AB /* PBXContainerItemProxy */;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
D85B829C1AB6D5CE003F4FE2 /* libRCTVibration.a */ = {
|
||||
isa = PBXReferenceProxy;
|
||||
fileType = archive.ar;
|
||||
|
||||
@@ -48,7 +48,6 @@ var APIS = [
|
||||
require('./ActionSheetIOSExample'),
|
||||
require('./AdSupportIOSExample'),
|
||||
require('./AlertIOSExample'),
|
||||
require('./AppStateExample'),
|
||||
require('./AppStateIOSExample'),
|
||||
require('./AsyncStorageExample'),
|
||||
require('./CameraRollExample.ios'),
|
||||
@@ -56,6 +55,7 @@ var APIS = [
|
||||
require('./LayoutExample'),
|
||||
require('./NetInfoExample'),
|
||||
require('./PointerEventsExample'),
|
||||
require('./PushNotificationIOSExample'),
|
||||
require('./StatusBarIOSExample'),
|
||||
require('./TimerExample'),
|
||||
require('./VibrationIOSExample'),
|
||||
|
||||
Reference in New Issue
Block a user