【问题标题】:It is possible to get desktop notification from flex web app?可以从 flex 网络应用程序获取桌面通知吗?
【发布时间】:2017-06-20 10:45:30
【问题描述】:

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

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

【问题讨论】:

    标签: actionscript-3 apache-flex actionscript flex4 flex3


    【解决方案1】:

    是的,通过外部接口。下面是一个非常简单的 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 类here。这是使用该类的示例代码:

    <?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

    【讨论】:

    • 谢谢@gigawatts。您的回答对找到解决方案最有帮助。我对您的代码进行了一些更改,最后,现在我使用 ExternalInterface 获得了所需的输出,并且给定的链接也变得有用link
    • 太棒了!我最终在github.com/monkeypunch3/flexcapacitor/blob/master/MainLibrary/… 上做了一个 AS3 课程。我正在更新我的示例代码以使用该类。
    猜你喜欢
    • 2012-06-24
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多