為你的應用程序處理推送通知,包括權(quán)限的處理和圖標標記數(shù)量。
為了啟動和運行,在Apple上配置您的通知 和服務器端系統(tǒng)。為了得到一個想法,這里是 解析指南。
static setApplicationIconBadgeNumber(number: number)
在主屏幕上為應用程序的圖標設置標記數(shù)量
static getApplicationIconBadgeNumber(callback: Function)
在主屏幕上為應用程序的圖標獲取當前的標記數(shù)量
static addEventListener(type: string, handler: Function)
當應用程序在前臺或者后臺運行的時候,為了遠程通知鏈接一個監(jiān)聽器。
處理程序?qū)砸粋€ PushNotificationIOS 的實例的形式被調(diào)用
static requestPermissions()
從iOS上請求所有的通知權(quán)限,提示用戶對話框
static checkPermissions(callback: Function)
查看當前正被啟用的推送權(quán)限。Callback 函數(shù)將被一個
permission 的對象調(diào)用:
static removeEventListener(type: string, handler: Function)
刪除事件監(jiān)聽器。為了防止內(nèi)存泄露,該操作在 componentWillUnmount 里完成。
static popInitialNotification()
如果應用程序從一個通知被冷發(fā)射,那么一個原始通知將變成可用狀態(tài)。
popInitialNotification 的第一個調(diào)用者將獲取最初的通知對象,或者為 null。后續(xù)的調(diào)用將返回 null。
constructor(nativeNotif)
你自己可能永遠都不需要 instansiate PushNotificationIOS。你只需要監(jiān)聽 notification 事件并且調(diào)用 popInitialNotification就足夠了。
getMessage()
getAlert 的一個別名,該函數(shù)是為了獲取通知的主要消息字符串
getSound()
從 aps 對象中獲取聲音字符串
getAlert()
從 aps 對象中獲取通知的主要消息字符串
getBadgeCount()
從 aps 對象中獲取標記數(shù)量
getData()
在通知上獲取數(shù)據(jù)對象
'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 />;
}
}];