是否可以从 Flex Web 应用程序获取桌面通知?

It is possible to get desktop notification from flex web app?

我正在开发 Flex,我想知道是否可以从 flex 网络应用程序获取桌面通知?

请注意,来自 Flex Web App 而不是 Flex Desktop App

是的,通过外部接口。这是 AS 文件中的一个非常简单的示例代码:

function callNotification(message:String):void {
    // throw errors if our JS has errors
    ExternalInterface.marshallExceptions = true;

    var string:String = <xml><![CDATA[
        function(message) {

          // Let's check if the browser supports notifications
          if (!("Notification" in window)) {
             alert("This browser does not support desktop notification");
             return false;
          }

          // Let's check whether notification permissions have already been granted
          else if (Notification.permission === "granted") {
             // If it's okay let's create a notification
             var notification = new Notification(message);
             return true;
          }

          // Otherwise, we need to ask the user for permission
          else if (Notification.permission !== 'denied') {
            Notification.requestPermission(function (permission) {
              // If the user accepts, let's create a notification
              if (permission === "granted") {
                var notification = new Notification(message);
                return true;
              }
              return false;
            });
          }

          // At last, if the user has denied notifications, and you 
          // want to be respectful there is no need to bother them any more.
          return false;
        }
    ]]></xml>
    var success:Boolean = ExternalInterface.call(string, message);
}

callNotification("Hello world!");

我整理了一个 AS3 class here。这是使用 class 的示例代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               minWidth="155" minHeight="100">

    <fx:Script>
        <![CDATA[
            import com.flexcapacitor.utils.WebNotification;
            protected function button1_clickHandler(event:MouseEvent):void
            {
                var message:String = messageInput.text;
                WebNotification.showNotification(message);
            }

            protected function button2_clickHandler(event:MouseEvent):void
            {
                var supported:Boolean = WebNotification.isSupported();
                resultsInput.text = supported ? "yes" : "no";
            }

            protected function button3_clickHandler(event:MouseEvent):void
            {
                var supported:Boolean = WebNotification.isPermissionGranted();
                resultsInput.text = supported ? "yes" : "no";
            }

            protected function button4_clickHandler(event:MouseEvent):void
            {
                var permission:String = WebNotification.permission;
                resultsInput.text = permission;
            }

            protected function button5_clickHandler(event:MouseEvent):void
            {
                var results:String = WebNotification.requestPermission();
                if (results=="false") {
                    resultsInput.text = "Not supported";
                }
            }

        ]]>
    </fx:Script>

    <s:VGroup horizontalCenter="0" verticalCenter="0" horizontalAlign="center">

        <s:HGroup>
            <s:Button label="Create notification" click="button1_clickHandler(event)"/>
            <s:TextInput id="messageInput" text="Hello world"/>
        </s:HGroup>

        <s:HGroup horizontalCenter="0" verticalCenter="0" >
            <s:Button label="Is supported" click="button2_clickHandler(event)"/>
            <s:Button label="Is Permission Granted" click="button3_clickHandler(event)"/>
            <s:Button label="Permission status" click="button4_clickHandler(event)"/>
            <s:Button label="Request permission" click="button5_clickHandler(event)"/>
            <s:TextInput id="resultsInput"/>
        </s:HGroup>

    </s:VGroup>  

</s:Application>

更多信息 https://developer.mozilla.org/en-US/docs/Web/API/notification