react-native - 使用 OneSignal.registerForPushNotifications,然后批准不注册
问题描述
我是用户 OneSignal 结合 React-Native 来设置推送通知。在我的应用程序中,用户需要登录才能接收推送通知(或授予权限)。
我遇到的问题是,当用户登录时,他们会收到我的自定义弹出窗口,询问他们是否愿意接收消息。如果他们点击是,他们的 iOS 弹出窗口显示,请求许可。
当我再次单击“是”并检查 OneSignal 用户仪表板时,查看哪些用户已注册推送通知。我看到了我的,但选择退出,这是不正确的。
所以我想知道 OneSignal 的代码中是否存在错误(已经与他们联系,但进展缓慢)或我的代码错误。
我在用;
- 反应原生 0.56
- 反应原生单信号:3.2.7
以下代码是我目前拥有的:
componentWillMount() {
OneSignal.init("{key}", {kOSSettingsKeyAutoPrompt : false});
AsyncStorage.multiGet(['firstTime', 'subscribed']).then((response) => {
this.setState({
firstTime: JSON.parse(response[0][1]) != null ? JSON.parse(response[0][1]) : true,
subscribed: JSON.parse(response[1][1]) != null ? JSON.parse(response[1][1]) : false
});
setTimeout(() => {
if (Platform.OS === 'android' && !this.state.subscribed && this.state.firstTime) {
OneSignal.setSubscription(false);
}
}, 200);
});
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('ids', this.onIds);
}
componentDidMount () {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
AsyncStorage.getItem('firstTime').then((result) => {
!this.isCancelled && this.setState({
firstTime: JSON.parse(result) != null ? JSON.parse(result) : true
})
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
setTimeout(() => {
if (!this.state.subscribed && this.state.firstTime) {
Alert.alert(
I18n.t('notifications title'),
I18n.t('notifications subtitle'),
[
{text: I18n.t('notifications no'), onPress: () => {
setTimeout(() => {
AsyncStorage.multiSet([['firstTime', JSON.stringify(false)], ['subscribed', JSON.stringify(false)]]);
}, 100)
}, style: 'cancel'},
{text: I18n.t('notifications ok'), onPress: () => {
if (Platform.OS === 'android') {
OneSignal.setSubscription(true);
}else if (Platform.OS === 'ios') {
// OneSignal.setLogLevel(6, 0);
OneSignal.registerForPushNotifications();
}
setTimeout(() => {
AsyncStorage.multiSet([['firstTime', JSON.stringify(false)], ['subscribed', JSON.stringify(true)]]);
}, 100)
}}
],
{ cancelable: false }
)
}
}, 500)
}else{
OneSignal.setSubscription(false);
}
SplashScreen.hide();
});
};
希望各位大侠帮忙。
干杯,
解决方案
啊,我找到了问题的原因。else 语句中的第二个 setSubscription(false) 取消订阅,尽管我没有。
所以删除 else 正在为我修复它。
推荐阅读
- python - 在 Python 中释放内存的最干净/最正确的方法(当 del/gc 拒绝时)
- python - 如何从 Python 中的 GLM 结果中获取标准错误?
- python-3.x - 如何检测和提取抓取的网址中的页码?
- database - Liquibase 和 Terraform 最佳实践
- java - 241. 添加括号的不同方法 - 一个我无法理解且非常有趣的错误
- cmake - 将 ESP32 项目从 C 盘移动到 H 盘。现在 Cmake 不会编译
- javascript - React-Native-Canvas 不绘制整个矩形
- java - Mockito 上的 NullPointerException 验证
- python - 这个二分搜索有什么问题?
- mysql - 根据列查找相似日期