Notification at DART lang - Chrome Packaged App

元气小坏坏 提交于 2019-12-10 10:35:35

问题


I want to use notification in Chrome Packaged app, built by DART.

my pubspec.yaml is:

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

my main.dart is:

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'));
....
}

it gave me a problem: "Undefined class 'NotificationOptions'"

if I removed the "as chrome" in the line importing the package, then I'm getting an error at chrome.notifications.create(...) tilling that 'chrome' is undefined.

what mistake I made here, and what is the correct way to define the options for chrome notifications!


回答1:


If you use as chrome in the import you have to prefix references to identifiers in this package with chrome.

This is not valid Dart syntax

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

 };

maybe it should be something like

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'));
  ....
}

also the manifiest.json should include permission, as:

  "permissions": ["notifications"],


来源:https://stackoverflow.com/questions/25596250/notification-at-dart-lang-chrome-packaged-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!