OneSignal-Flutter-SDK
OneSignal-Flutter-SDK copied to clipboard
[question]: Why does the method OneSignal.initialize() not return a Future?
How can we help?
In the first step of my code, I initialize OneSignal with the function OneSignal.initialize(). In the second step, I want to use the OneSignal.Notifications.permission field to query whether the system permission are still available. However, as the initialize() function is not asynchronous, the field OneSignal.Notifications.permission returns an incorrect result. The same happens for other fields such as OneSignal.User.pushSubscription.optedIn. If I wait 200ms after initialization, the fields return the correct results.
So shouldn't the initialize function be asynchronous and wait for all fields to be initialized? Is this possible in a future release?
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
Yes same question
Hi @MarcelGalster and @affan3699 thanks for reaching out, and apologies for the delay.
The initialize method does not return a Future so this is indeed a limitation when you are trying to retrieve other data immediately. Usually this information will be unavailable on a new install only. Subsequent app opens should read accurate data.
To workaround this, I recommend using the observers to be notified of changes:
OneSignal.Notifications.addPermissionObserver(observer);
OneSignal.User.pushSubscription.addObserver((state) {
OSPushSubscriptionChangedState current = state.current;
OSPushSubscriptionChangedState previous = state.previous;
if (state.current.optedIn) {
/// Respond to new state
}
});
OneSignal.User.addObserver((state) {
var userState = state.jsonRepresentation();
print('OneSignal user changed: $userState');
});