0

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?

Lasneyx
  • 2,110
  • 2
  • 17
  • 21
  • I've considered using a timeout, delaying registration, but it only delays the registration for an specific time; if the user does not hit Allow or Do not allow during this time, it won't work either. – Lasneyx Feb 18 '16 at 15:07

2 Answers2

0

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.

Lasneyx
  • 2,110
  • 2
  • 17
  • 21
  • Hi, we have some example code to illustrate the best usage of this here: https://gist.github.com/marchbold/fb0438cf326a44cea0cf#file-distriqt-extensions-pushnotifications-isenabled-as – Michael Feb 21 '16 at 23:24
0

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

Michael
  • 3,776
  • 1
  • 16
  • 27
  • I've used that code to check if it was the first time the application run, but I've stated in the answer I had to use another approach because I have got enabled execute in background and `isEnabled` returns true the first time, ignoring what the user has set (the truth is that the user hasn't set anything yet). – Lasneyx Feb 22 '16 at 10:32
  • Really? Changing the execute flag shouldn't affect the registration for remote notifications. So after you set that flag isEnabled reports true and if you comment it out it returns false? – Michael Feb 23 '16 at 01:15
  • Maybe I'm missing something but when `PushNotifications.service.register( GCM_SENDER_ID );` is called, a popup appears requesting permission for Push Notifications and sometimes, isEnabled reports true, even though the user clicked No. What happens behind the scenes is that `register` tries to register the device for push notifications, and when `registerSuccessHandler` gets triggered, `isEnabled` is equals `true`, but the user clicked No. I supposed that you used `hasRequestedPermissionsOnce` to avoid `isEnabled` being false the first time, but it is true, what is far more strange. – Lasneyx Feb 23 '16 at 11:38
  • What version of iOS are you testing on? – Michael Feb 23 '16 at 11:43
  • I'm testing on iOS 9.1. Is there any reported bug on that version? – Lasneyx Feb 23 '16 at 12:26
  • It might be related with a testing device: while reinstalling several times the application (with an ad hoc mobile provisioning) the error happens occasionally. Restarting the device seems to make it work properly, although I'm not 100% sure if it's this what's happening. – Lasneyx Feb 25 '16 at 11:52