0

我正在使用 Distriqt Push Notifications Extension,如果用户在首次运行时不允许 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应该是假的?什么时候变成假的?我应该如何处理这种情况?

4

2 回答 2

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

原始来源:https ://gist.github.com/marchbold/fb0438cf326a44cea0cf#file-distriqt-extensions-pushnotifications-isenabled-as

于 2016-02-21T23:26:30.590 回答
0

我发现我的应用程序中发生了什么:

我正在处理激活/停用事件以启用和禁用后台执行:NativeApplication.nativeApplication.executeInBackground = true;. 这使您的应用程序能够在后台运行,忽略要求用户许可的 UI,并且它恰好PushNotifications.service.isEnabledtrue安装后首次运行时发生。

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

  • 设备不支持推送通知PushNotifications.isEnabled == false
  • 当设备收到推送令牌时
  • 当设备接收推送令牌失败时

我希望这可以帮助别人。

于 2016-02-18T15:46:57.443 回答