Reputation: 2330
I'm using Distriqt Push Notifications Extension and I can't get it working correctly if the user does not allow PNs on first run: the application ends registering the user because it states that PNs are enabled and available.
I do the following:
if (PushNotifications.isSupported()) {
registerPushNotifications();
}
private function registerPushNotifications():void {
PushNotifications.service.addEventListener(PushNotificationEvent.REGISTER_SUCCESS, onPushNotificationToken);
PushNotifications.service.register(MODEL.Configuration.GCM_SENDER_ID);
}
private function onPushNotificationToken(event:PushNotificationEvent):void {
if (PushNotifications.service.isEnabled) { registerDevice(); }
}
Does not PushNotifications.service.isEnabled
supposed to be false if the user disallows it? When does it become false? How am I supposed to handle this case scenario?
Upvotes: 0
Views: 246
Reputation: 3871
Just posting this here for anyone else who has issues with the isEnabled
flag:
var hasRequestedPermissionsOnce:Boolean = false;
// You should load hasRequestedPermissionsOnce from some persistent storage, defaulting to false
...
PushNotifications.init( APP_KEY );
if (PushNotifications.isSupported)
{
if (PushNotifications.service.isEnabled)
{
// Notifications have been enabled by the user
// You are free to register and expect a registration success
register();
}
else if (!hasRequestedPermissionsOnce)
{
// You should implement hasRequestedPermissionsOnce somewhere to check if this is the first run of the app
// If we haven't called register once yet the isEnabled flag may be false as we haven't requested permissions
// You can just register here to request permissions or use a dialog to delay the request
register();
}
else
{
// The user has disabled notifications
// Advise your user of the lack of notifications as you see fit
}
}
...
private function register():void
{
// You should save hasRequestedPermissionsOnce to a shared object, file or other persistent storage
hasRequestedPermissionsOnce = true;
PushNotifications.service.addEventListener( PushNotificationEvent.REGISTER_SUCCESS, registerSuccessHandler );
PushNotifications.service.addEventListener( PushNotificationEvent.REGISTER_FAILED, registerFailedHandler );
PushNotifications.service.register( GCM_SENDER_ID );
}
Original source here: https://gist.github.com/marchbold/fb0438cf326a44cea0cf#file-distriqt-extensions-pushnotifications-isenabled-as
Upvotes: 0
Reputation: 2330
I've found what was happening in my application:
I'm handling activate/deactivate events to enable and disable background execution: NativeApplication.nativeApplication.executeInBackground = true;
. This makes your application able to run on background, ignoring the UI which asks for user permission and it happens that PushNotifications.service.isEnabled
is true
on first run after installation.
What I've done is delaying adding activation and deactivation listeners till one of this things happen first:
PushNotifications.isEnabled == false
I hope this helps someone.
Upvotes: 0