我提到这个问题 https://stackoverflow.com/questions/35387227/get-device-token-with-react-native获取设备令牌以便将推送通知发送到我的应用程序。我使用创建了我的应用程序create-react-native-app
。这是代码:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
AppRegistry,
Text,
View,
PushNotificationIOS
} from 'react-native';
type Props = {};
export default class Apptitude extends Component<Props> {
constructor() {
console.log('registering evt listerner in launchpad')
PushNotificationIOS.addEventLister('register', (token) => {
this.setState({
deviceToken: token
})
});
}
render() {
return (
<View>
</View>
);
}
}
PushNotificationIOS.addEventListener('registrationError', (registrationError) => {
console.lo('was error')
console.log(reason.message)
console.log(reason.code)
console.log(reason.details)
})
// yes I'm aware I've added an event listener in the constructor also. Neither of these callbacks fire
PushNotificationIOS.addEventListener('register', (token) => {
console.log('this is the token', token);
});
console.log('requesting permissions')
PushNotificationIOS.requestPermissions();
问题是register
和registrationError
事件永远不会触发。系统会提示我批准权限,下次应用程序启动时我可以使用checkPermissions()
并确认已授予权限。但如果没有设备令牌,就无法向设备发送推送通知。我究竟做错了什么?
另一件需要注意的事情是,在模拟器上onRegister
功能未触发,您必须使用真实设备。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)