可以从 flex 网络应用程序获取桌面通知吗?

Posted

技术标签:

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

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

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

【问题讨论】:

【参考方案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 课程。我正在更新我的示例代码以使用该类。

以上是关于可以从 flex 网络应用程序获取桌面通知吗?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用flex构建桌面应用程序?

我可以在 C# 桌面应用程序中创建实时推送通知吗? [关闭]

当我的应用程序在后台运行时,可以从摄像头获取通知吗?

有啥方法可以获取 iOS 上所有应用的通知吗?

iOS上推送通知渐进式网络应用程序

用于获取通知的通用 Windows 10 应用后台任务计时器