我正在开发Flex,我想知道是否可以从 flex web app获取桌面通知?
请注意,从 Flex Web App 而非 Flex桌面应用
答案 0 :(得分:3)
是的,通过ExternalInterface。这是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