Distriqt推送通知扩展:如何知道用户是否在首次运行应用程序时不允许使用PN?

时间:2016-02-18 13:15:42

标签: push-notification air distriqt

我正在使用Distriqt推送通知扩展,如果用户在首次运行时不允许PN,则无法使其正常工作:应用程序结束注册用户,因为它声明PN已启用且可用。

我执行以下操作:

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(); }
}

如果用户不允许,PushNotifications.service.isEnabled不应该是假的吗?什么时候变得虚假?我该如何处理这种情况呢?

2 个答案:

答案 0 :(得分:0)

我发现我的申请中发生了什么:

我正在处理激活/停用事件以启用和禁用后台执行:NativeApplication.nativeApplication.executeInBackground = true;。这使您的应用程序能够在后台运行,忽略了要求用户权限的UI,并且安装后第一次运行时PushNotifications.service.isEnabled truePushNotifications.isEnabled == false

我所做的是延迟添加激活和停用监听器,直到其中一件事先发生:

  • 设备不支持推送通知<a href="#/screen1.html" class="button">To Screen 1</a>
  • 当设备收到推送令牌时
  • 当设备无法接收推送令牌时

我希望这有助于某人。

答案 1 :(得分:0)

只需将此处发布给有isEnabled标志问题的其他人:

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 );
}

原文来源:enter image description here