DART lang的通知 - Chrome打包应用

时间:2014-08-31 20:58:45

标签: dart google-chrome-app

我想在DART构建的Chrome Packaged app中使用通知。

我的pubspec.yaml是:

dependencies:
    browser: any
    chrome: any
transformers:
    - chrome

我的main.dart是:

import 'dart:html';
import 'package:chrome/chrome_app.dart' as chrome;

void main() {
   ....
     NotificationOptions notOptions = {
       'message':  'this is a notification message'

     };

chrome.notifications.create('id1', notOptions).then((id) => print('notification created'));
....
}

它给了我一个问题:"未定义的课程' NotificationOptions'"

如果我删除"作为chrome"在导入包的行中,然后我在chrome.notifications.create(...)中收到了一个错误,那就是' chrome'未定义。

我在这里犯了什么错误,以及为Chrome通知定义选项的正确方法是什么!

1 个答案:

答案 0 :(得分:1)

如果您在导入中使用as chrome,则必须使用chrome为此包中的标识符添加前缀。

这不是有效的Dart语法

 NotificationOptions notOptions = {
   'message':  'this is a notification message'

 };

也许它应该像

import 'dart:html';
import 'package:chrome/chrome_app.dart' as chrome;

void main() {
 .....
     chrome.NotificationOptions notOptions = new chrome.NotificationOptions(type: chrome.TemplateType.BASIC,
    iconUrl:'/icon.png',title:'notification title', message:  'this is a notification message');

  chrome.notifications.create('id1', notOptions).then((id) => print('notification created'));
  ....
}

另外,manifiest.json应该包含权限,如:

  "permissions": ["notifications"],
相关问题