Description:
Hello I'm getting an error saying that Onesignal is not yet defined when I'm setting it up. Do we need to wait before calling OneSignal.getPermissionSubscriptionState?
I have the following code below
useEffect(() => {
OneSignal.setAppId("MY_APP_KEY", {
kOSSettingsKeyAutoPrompt: false,
kOSSettingsKeyInAppLaunchURL: false,
kOSSettingsKeyInFocusDisplayOption: 2,
});
OneSignal.setLogLevel(6, 0);
OneSignal.setRequiresUserPrivacyConsent(true);
OneSignal.getPermissionSubscriptionState((status) => {
if (status.hasPrompted == false) {
console.log('enters')
OneSignal.addTrigger("prompt_ios", "true");
}
});
});
I'm also getting
_reactNativeOnesignal.default.getPermissionSubscriptionState is not a function. (In '_reactNativeOnesignal.default.getPermissionSubscriptionState(function
when trying to call it AFTER i set app ID etc.
EDIT:
After checking the source in the node_modules it indeed looks like "getPermissionSubscriptionState" is missing
@NiklasMencke I ended up not using getPermissionSubscriptionState.
I think doc here is outdated, especially the part about calling getPermissionSubscriptionState https://documentation.onesignal.com/docs/ios-push-opt-in-prompt
I am using class ,only at App screen as wrapper instead of using hooks , cause the documentation havent explain clearly
import React from 'react';
import {AuthStack} from './src/navigation/auth/index';
import {NavigationContainer} from '@react-navigation/native';
import {Provider }from 'react-redux'
import {store} from './src/redux/store'
import OneSignal from 'react-native-onesignal'
import {Alert} from 'react-native'
//One signal new method since they relesed the SDK new version and havent relese complete documentation as example
export default class App extends React.Component{
constructor(props) {
super(props);
this.state = {
name: props.name,
isSubscribed: false,
requiresPrivacyConsent: false,
isLocationShared: false,
inputValue: "",
consoleValue: ""
}
}
async componentDidMount() {
/* O N E S I G N A L S E T U P */
OneSignal.setAppId("AppId");
OneSignal.setLogLevel(6, 0);
OneSignal.setRequiresUserPrivacyConsent(this.state.requiresPrivacyConsent);
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 state = await OneSignal.getDeviceState();
this.setState({
name : state.emailAddress,
isSubscribed : state.isSubscribed
});
}
componentWillUnmount() {
OneSignal.clearHandlers();
}
OSLog = (message, optionalArg) => {
if (optionalArg) {
message = message + JSON.stringify(optionalArg);
}
console.log(message.notificationId);
let consoleValue;
if (this.state.consoleValue) {
consoleValue = this.state.consoleValue+"\n"+message
} else {
consoleValue = message;
}
this.setState({ consoleValue });
}
render() {
return (
<>
<Provider store={store}>
<NavigationContainer>
<AuthStack/>
</NavigationContainer>
</Provider>
</>
);
};
}
And work
Howdy,
Thanks for reporting this. It is indeed outdated.
In this case, you would want to use getDeviceState to get device level information. You can then use that to trigger the IAM.
Cheers
@arwysyah ,
We haven't added documentation regarding hooks which is a request made here. Please continue tracking it there.
Most helpful comment
I am using class ,only at App screen as wrapper instead of using hooks , cause the documentation havent explain clearly
And work