2016-01-05 11 views
6

私はChromeアプリケーションを構築しています。アプリは活字体(Angular2)と書かれています。活字体とChrome通知

私はプッシュ通知をしたいと思います。ここでは、コードがあります:

import {Injectable} from 'angular2/core'; 

@Injectable() 
export class NotificationService { 
    constructor() { 
     if(Notification.permission !== 'granted') { 
      Notification.requestPermission(); 
     } 
    } 
} 

ゴクゴクアプリをビルドすると、それは言う:

src/scripts/stream/notification.service.ts(6) Cannot find name 'Notification'. 

私は内部の私のクラスをラップしてみました:

/* tslint:disable */ 
... the code 
/* tslint:enable */ 

しかし、それは何も変わりません。

tslint.jsonファイルで、これがグローバル変数であることをTypescriptに伝える方法はありますか?あなたがChromeのタイピングの定義が欠落しているように見えます

"globals": { 
    "Notification": false 
} 
+0

あなたはタイプ定義が見つからないようです。あなたのプロジェクトにchrome.d.tsが含まれていますか? – toskv

+0

私はtypescriptとtslintを使い慣れました。私はchrome.d.tsが何であるか分かりません! – Maxime

+0

ここにファイルがあります:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/chrome/chrome.d.tsどうすればいいですか? – Maxime

答えて

8

ここではオプションのカップル:

1.
は、ファイルの先頭に以下の行を追加します。

declare var Notification: any; 

これは、トランスバータを通過させますが、TypeScriptの機能の多くを提供しません。

2.
定義タイプ(chrome.d.ts)をダウンロードします。

TSDが最も人気のある定義マネージャーのようです。
Typings別の有望な代替です。

2

:それはそのようなものになるだろうjshintで

tsdツールを使用してインストールできます。

まず、tsdをインストールする必要があります。

$ npm install tsd -g 

次に、Chromeのタイプ定義をプロジェクトにインストールするために使用できます。

$ tsd install chrome 

あなたはTSD here詳細を見つけることができます。

注:ライブラリ用に1つのtsdファイルしか定義されていないことを確認してください。

+0

私は、ありがとう!もう一つのこと:gitから入力フォルダを除外すべきかどうか? – Maxime

+0

プロジェクトのルートにすべての依存関係を持つtsd.jsonファイルがあるはずですので、除外しても問題ありません。 – toskv

+0

https://github.com/maxime1992/real-debrid-streamあなたはプロジェクトと私が持っているファイルを見ることができます。それは動作しません。私はちょうど2つのコマンドを実行した、私はここに何かが不足していると思う。 – Maxime

2

通知API用のd.tsファイルが見つかりませんでしたが、ちょっとしたトリックが見つかりました。

if("Notification" in window){ 
    let _Notification = window["Notification"]; 
} 

_NotificationはNotificationと同じプロパティを持ち、同じものを使用できます。これは、活字体のいずれかのエラーを回避します。

2

一般的なタイピングファイルは、GitHub TypeScript issueから入手できます。

新しい入力定義ファイル(notification.d.ts)を作成し、次のコードを追加します。

type NotificationPermission = "default" | "denied" | "granted"; 

type NotificationDirection = "auto" | "ltr" | "rtl"; 

interface NotificationPermissionCallback { 
    (permission: NotificationPermission): void; 
} 

interface NotificationOptions { 
    dir?: NotificationDirection; 
    lang?: string; 
    body?: string; 
    tag?: string; 
    image?: string; 
    icon?: string; 
    badge?: string; 
    sound?: string; 
    vibrate?: number | number[], 
    timestamp?: number, 
    renotify?: boolean; 
    silent?: boolean; 
    requireInteraction?: boolean; 
    data?: any; 
    actions?: NotificationAction[] 
} 

interface NotificationAction { 
    action: string; 
    title: string; 
    icon?: string; 
} 

declare class Notification extends EventTarget { 
    constructor(title: string, options?: NotificationOptions); 

    static readonly permission: NotificationPermission; 
    static requestPermission(): Promise<NotificationPermission>; 
    static requestPermission(deprecatedCallback: (permission: NotificationPermission) => void): void; 

    static readonly maxActions: number; 

    onclick: EventListenerOrEventListenerObject; 
    onerror: EventListenerOrEventListenerObject; 

    close(): void; 

    readonly title: string; 
    readonly dir: NotificationDirection; 
    readonly lang: string; 
    readonly body: string; 
    readonly tag: string; 
    readonly image: string; 
    readonly icon: string; 
    readonly badge: string; 
    readonly sound: string; 
    readonly vibrate: number[]; 
    readonly timestamp: number; 
    readonly renotify: boolean; 
    readonly silent: boolean; 
    readonly requireInteraction: boolean; 
    readonly data: any; 
    readonly actions: NotificationAction[] 
} 

入力ファイルがプロジェクト(tsconfig.json)に含まれていることを確認してください。

"typeRoots": [ 
     "typings/notification.d.ts" 
    ] 

通知にアクセスできるようになりました。

関連する問題