I am working on Flex and I want to know if it is possible to get desktop notification from flex web app?
Please note that from Flex Web App not Flex Desktop App
Yes, through ExternalInterface. Here is a very simple example code in an AS file:
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!");
I've put together an AS3 class here. Here is the example code using that 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>
More info https://developer.mozilla.org/en-US/docs/Web/API/notification