2014-09-01 128 views
1

我想请教一下在DART正常的通知,这是不同的比Chrome封装应用讨论here通知 - HTML /浏览器/ WebKit的/桌面

我创建了下面的代码,浏览器[DARTIUM ]如预期那样要求权限,但两种情况下的响应(允许/拒绝)都在控制台中显示为“默认”,并且通知未出现。

{

void main() { 
    Notification.requestPermission().then((String permission) { 
    print(permission); // ==> This is always = "default" for both Allow and Deny 
    if (permission == "default") { 
     print('permission granted'); 
     var notification = new Notification('hello'); 
    } 
else print('sorry no permission!'); 
}); 
} 

任何想法!感谢

回答

1

感谢布赖恩和冈特,我做了我自己的通知,由this

我做了所使用的相同,CSS文件的启发,并取得了自定义元素,notification.dart

part of myApp; 

class NotificationElement extends HtmlElement { 
// Define the custom element tag 
static final tag = 'x-Notification'; 
factory NotificationElement() => new Element.tag(tag); 
// Create the element and define its stylesheet 

NotificationElement.created() : super.created(){ 
    LinkElement styleSheet = new LinkElement()..rel = "stylesheet"..type="text/css"..href="./style/toastr.css"; 
     document.head.append(styleSheet); 
    } 

var notificationContainer = new Element.html('<div></div>') 
          ..id='notification-container'; 
var notificationBody = new Element.html('<div></div>') 
        ..classes.add('notification'); 
var notificationButton = new ButtonElement() 
        ..classes.add('notification-close-button') 
        ..text='x'; 

var notificationTitle = new Element.html('<div></div>')..classes.add('notification-title'); 
var notificationMsg = new Element.html('<div></div>')..classes.add('notification-message'); 
var notificationMsgLabel = new LabelElement(); 

Element launchElement(type,location,Title,Msg){ 
notificationButton.onClick.listen((e) => notificationContainer.nodes.remove(notificationBody)); 
notificationContainer..classes.add('notification-'+location); 
notificationBody..classes.add('notification-'+type); 
notificationTitle.text=Title; 
notificationMsg.innerHtml='<label>'+Msg+'</label>'; 
     notificationBody.nodes..add(notificationButton)..add(notificationTitle)..add(notificationMsg);       
notificationContainer.nodes..add(notificationBody); 
return (notificationContainer); 
} 

}

Element mynotification = querySelector('#notification-element'); 

void CreatefonixNotification(type,location,Title,Msg){ 
    var notifyMe = new Element.tag('x-notification'); 
    notifyMe = notifyMe.launchElement(type,location,Title,Msg); 
    mynotification.nodes.add(notifyMe); 
} 
在index.dart

,我注册的元素:

document.registerElement(NotificationElement.tag, NotificationElement); 

和index.html中,我加入这个div:

<div id='notification-element'></div> 

,并在应用程序的任何地方,我可以把它想:

CreateNotification('error','top-left','Error:','sorry we have some issue!!.') 
2

,因为它是目前书面绝不会显示一个许可两种方式的代码 - “默认”是指允许尚未允许或拒绝。在显示通知之前,您应该检查permission == "granted"

请记住,没有与通知杰出的错误,并检查当前权限,请参阅https://code.google.com/p/dart/issues/detail?id=20585,尽管这可能不会影响到你。

+0

不管我点击,它出了结果“默认”,所以如果(...)将有权限默认或!默认 – 2014-09-02 18:51:46

+0

您能够发布您的代码更完整的例子吗? – 2014-09-02 19:12:44

+0

没有什么实际的特殊,我想下面的代码只是一旦运行给一个通知,我很少关注的问题发言,thabks – 2014-09-04 23:50:05