react-native - 无法获取用户 ID(PlayerID)。未定义(ReactNative、OneSignal)
问题描述
通过我的应用程序,我将通过 OneSignal 发送通知。但我无法从 OneSignal 获取用户 ID。我从 GET 请求中读取了这个用户 ID,并将其保存在数据库中。在我通过 PHP 发送通知之后。
如果我总是不确定,我怎么能得到这个用户 ID?
export default class App extends Component {
constructor(props) {
super(props);
this.WEBVIEW_REF = React.createRef();
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
OneSignal.setLogLevel(6, 0);
OneSignal.setAppId("fdb89158-4072-4964-b490-6ba70fb6b5fd");
OneSignal.promptForPushNotificationsWithUserResponse(response => {
});
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
let notification = notificationReceivedEvent.getNotification();
console.log("notification: ", notification);
const data = notification.additionalData
console.log("additionalData: ", notification.additionalData);
notificationReceivedEvent.complete(notification);
});
OneSignal.setNotificationOpenedHandler(notification => {
});
OneSignal.addPermissionObserver(event => {
console.log("OneSignal: permission changed:", event);
});
OneSignal.addSubscriptionObserver(event => {
console.log("OneSignal: subscription changed to userId:", event.to.userId);
});
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}
handleBackButton = ()=>{
this.WEBVIEW_REF.current.goBack();
return true;
}
onNavigationStateChange(navState) {
this.setState({
canGoBack: navState.canGoBack
});
}
render() {
const deviceState = OneSignal.getDeviceState();
return (
<WebView
source={{ uri: 'https://www.google.com/search?q='+ deviceState.userId}}
style={{ marginTop: 35 }}
ref={this.WEBVIEW_REF}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
/>
);
}
}
解决方案
getDeviceState
返回一个异步的,因此请确保您正在等待它。
推荐阅读
- javascript - 暂停条带订阅/延长剩余期限
- ruby-on-rails - Distribute_reads gem 是否支持 Amazon Aurora 的负载平衡副本?
- c# - jQuery 添加的 ASP.NET Core MVC 链接不起作用
- android-studio - Flutter 如何启动(创建项目时出错)
- sql - 在 Google Data Studio 或 SQL 中避免数据聚合中的“无穷大”
- python - 无论是否已在其他地方安装了副本,我都可以在 Windows 上本地安装 Python 吗?
- php - 未在 Ajax 请求上设置第三方 cookie
- azure - Azure AutoML Experiment 仍运行受阻算法
- python - Python 看不到机器包
- php - Laravel Google 回调 url 给出错误(“未找到”)