react-native - 在 React Native 中使用 Expo 的一个信号推送通知
问题描述
未处理的承诺拒绝:TypeError:null 不是对象(评估'RNOneSignal.setAppId')]
该错误每次都会发生。我现在应该做什么?它适用于 React-native-cli 项目。它在 Expo-cli 上给出错误
import * as React from "react";
import Home from "./Home"
import OneSignal from "react-native-onesignal";
class App extends React.Component {
async componentDidMount() {
/* O N E S I G N A L S E T U P */
OneSignal.setAppId("bb92b317-9038-4a4c-a52a-e0f3f9b3f163");
// OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
OneSignal.setLogLevel(6, 0);
OneSignal.setRequiresUserPrivacyConsent(false);
OneSignal.promptForPushNotificationsWithUserResponse(response => {
this.OSLog("Prompt response:", response);
});
/* O N E S I G N A L H A N D L E R S */
OneSignal.setNotificationWillShowInForegroundHandler(notifReceivedEvent => {
this.OSLog("OneSignal: notification will show in foreground:", notifReceivedEvent);
let notif = notifReceivedEvent.getNotification();
const button1 = {
text: "Cancel",
onPress: () => { notifReceivedEvent.complete(); },
style: "cancel"
};
const button2 = { text: "Complete", onPress: () => { notifReceivedEvent.complete(notif); } };
Alert.alert("Complete notification?", "Test", [button1, button2], { cancelable: true });
});
OneSignal.setNotificationOpenedHandler(notification => {
this.OSLog("OneSignal: notification opened:", notification);
});
OneSignal.setInAppMessageClickHandler(event => {
this.OSLog("OneSignal IAM clicked:", event);
});
OneSignal.addEmailSubscriptionObserver((event) => {
this.OSLog("OneSignal: email subscription changed: ", event);
});
OneSignal.addSubscriptionObserver(event => {
this.OSLog("OneSignal: subscription changed:", event);
this.setState({ isSubscribed: event.to.isSubscribed })
});
OneSignal.addPermissionObserver(event => {
this.OSLog("OneSignal: permission changed:", event);
});
const deviceState = await OneSignal.getDeviceState();
this.setState({
isSubscribed: deviceState.isSubscribed
});
}
render() {
return (
<Home />
);
}
}
export default App;
解决方案
推荐阅读
- php - cookie设置但不显示在另一个页面php
- python - 如何使用 django 基于类的通用视图添加最喜欢的文章标记系统?
- python-3.x - 在 tkinter 库中看不到 GUI
- jquery-isotope - 如何使用同位素过滤器和自举手风琴?
- emulation - 拥有多个调色板的 GameBoy 有什么意义?
- tensorflow - 张量流中张量的 _keras_shape 和 _shape 有什么区别?
- java - 如何在对话框中知道另一个对话框在android中的java类中使用这个对话框关闭了?
- excel - 如何使用vba从访问excel中检索数据?
- c# - 将流利的类似 LINQ 的查询链接在一起
- ruby-on-rails - 电子商务商店的模型