Distriqt Push Notification Extension: How to know if user has not allowed PNs on first application run?

248 Views Asked by At

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?

2

There are 2 best solutions below

1
On

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:

  • The device does not support push notifications PushNotifications.isEnabled == false
  • When the device receive a push token
  • When the device fails receiving a push token

I hope this helps someone.

6
On

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