使用OneSignal.registerForPushNotifications,则同意不注册
Using OneSignal.registerForPushNotifications, then approve not registering
我是 OneSignal 用户,结合 React-Native 来设置推送通知。在我的应用程序中,用户需要登录才能接收推送通知(或授予权限)。
我遇到的问题是,当用户登录时,他们会看到我的自定义弹出窗口,询问他们是否愿意接收消息。如果他们单击“是”,他们会 iOS 弹出窗口显示,请求许可。
当我再次单击“是”并检查 OneSignal 用户仪表板时,查看哪些用户注册了推送通知。我看到了我的,但选择退出,这是不正确的。
所以我在想是不是OneSignal的代码有bug(已经和他们联系了,但是速度很慢)或者我的代码有问题
我正在使用;
- React-native 0.56
- react-native-onesignal: 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 就是为我修复它。
我是 OneSignal 用户,结合 React-Native 来设置推送通知。在我的应用程序中,用户需要登录才能接收推送通知(或授予权限)。
我遇到的问题是,当用户登录时,他们会看到我的自定义弹出窗口,询问他们是否愿意接收消息。如果他们单击“是”,他们会 iOS 弹出窗口显示,请求许可。
当我再次单击“是”并检查 OneSignal 用户仪表板时,查看哪些用户注册了推送通知。我看到了我的,但选择退出,这是不正确的。
所以我在想是不是OneSignal的代码有bug(已经和他们联系了,但是速度很慢)或者我的代码有问题
我正在使用;
- React-native 0.56
- react-native-onesignal: 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 就是为我修复它。