我会尝试实施react-native-push-notification:
这是我的代码:
index.android.js:
import React from 'react';
import { AppRegistry } from 'react-native';
import App from './app';
AppRegistry.registerComponent('PushNotificationHowTo', () => App);
/app/index.js
import React, { Component } from 'react';
import { View, Text, StyleSheet, Picker, AppState, Platform } from 'react-native';
import PushController from './PushController';
import PushNotification from 'react-native-push-notification';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
picker: {
width: 100,
},
});
export default class App extends Component {
constructor(props) {
super(props);
this.handleAppStateChange = this.handleAppStateChange.bind(this);
this.state = {
seconds: 5,
};
}
componentDidMount() {
AppState.addEventListener('change', this.handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
}
handleAppStateChange(appState) {
if (appState === 'background') {
let date = new Date(Date.now() + (this.state.seconds * 1000));
if (Platform.OS === 'ios') {
date = date.toISOString();
}
PushNotification.localNotificationSchedule({
message: "My Notification Message",
date,
});
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Choose your notification time in seconds.
</Text>
<Picker
style={styles.picker}
selectedValue={this.state.seconds}
onValueChange={(seconds) => this.setState({ seconds })}
>
<Picker.Item label="5" value={5} />
<Picker.Item label="10" value={10} />
<Picker.Item label="15" value={15} />
</Picker>
<PushController />
</View>
);
}
}
/app/PushController.js
import React, { Component } from 'react';
import PushNotification from 'react-native-push-notification';
export default class PushController extends Component {
componentDidMount() {
PushNotification.configure({
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
},
});
}
render() {
return null;
}
}
执行我在问题下面的代码:
元素类型无效:期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:object。
如何解决该问题以及如何实施推送通知?