TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Please add Notification.requestPermission()

Open kk80805588 opened this issue 11 years ago • 19 comments

https://developer.mozilla.org/zh-CN/docs/Web/API/notification/Using_Web_Notifications unf _n c9y b 1_n 3phz h

kk80805588 avatar May 11 '15 07:05 kk80805588

We try not to put non-standardized things in lib.d.ts.

You can always include a .d.ts file yourself that includes definitions for these APIs.

RyanCavanaugh avatar May 11 '15 17:05 RyanCavanaugh

Isn't the Notification API actually a standard? https://notifications.spec.whatwg.org/

Since the Notification API is part of Web Workers, I'd expect to have it defined in one of the standard libs (e.g. with the new TS 1.9 --lib option).

heruan avatar Jun 14 '16 14:06 heruan

This was closed over 13 months ago, when Notification was from standard.

This can be addressed by contributing to the TS-Lib-Generator.

kitsonk avatar Jun 14 '16 15:06 kitsonk

Thank you @kitsonk I had a look to the TS-Lib-Generator to contribute but I couldn't guess the correct approach to add the Notification type in the addedTypes.json file (as the contribution guidelines state), since the interface has static/readonly properties not covered by the build script.

This is the needed declaration:

interface NotificationOptions {
    dir?: DOMString;
    lang?: DOMString;
    body?: DOMString;
    tag?: DOMString;
    icon?: USVString;
    data?: any;
    vibrate?: number[];
    renotify?: boolean;
    silent?: boolean;
    sound?: USVString;
    noscreen?: boolean;
    sticky?: boolean;
}

interface Notification {
    readonly title: DOMString;
    readonly dir: DOMString;
    readonly lang: DOMString;
    readonly body: DOMString;
    readonly tag: DOMString;
    readonly icon: USVString;
    readonly data: any;
    readonly silent: boolean;
    readonly timestamp: DOMTimeStamp;
    readonly noscreen: boolean;
    readonly renotify: boolean;
    readonly sound: USVString;
    readonly sticky: boolean;
    readonly vibrate: number[];
    onclick: Function;
    onerror: Function;
    close(): void;
}

declare var Notification: {
    prototype: Notification;
    readonly permission: DOMString;
    new(title: string, options?: NotificationOptions): Notification;
    requestPermission(): Promise<DOMString>;
}

heruan avatar Jun 14 '16 15:06 heruan

I am a little bit surprised that it hasn't come over yet, though I haven't looked at master. It is in Edge 14, so the IDLs for Edge should reflect that, which should generate it without overrides... I don't know enough, about the specifics, but do new APIs automagically appear when added to Edge's IDLs and what version of the IDLs are used when building (is it only released)?

kitsonk avatar Jun 14 '16 19:06 kitsonk

Ah, I see, the browser IDLs are currently 4 months old... which means it is likely they are "Edge 13" ones. @zhengbli how often/when do you update them?

kitsonk avatar Jun 14 '16 19:06 kitsonk

We used to take the Edge spec files along with official windows 10 releases, therefore the last update was the same as TH2. However, it makes more sense now to do a more frequent update to avoid wasted efforts from the community, we are taking to the Edge team trying to make a monthly update happen. Also, NotificationOptions is indeed available in the latest Edge build. It should be covered next update, which should be soon.

zhengbli avatar Jun 15 '16 23:06 zhengbli

@zhengbli Is there any update on this issue?

FranklinWhale avatar Dec 17 '16 05:12 FranklinWhale

By the way, I have made an updated defintion for Notification:

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: NotificationPermission): void;

	static readonly maxActions: number;

	onclick: EventListenerOrEventListenerObject;
	onerror: EventListenerOrEventListenerObject;

	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[]
}

FranklinWhale avatar Dec 17 '16 06:12 FranklinWhale

Great work! I improved it a bit: the 'close' method on the Notification class was missing, and the definition for the deprecated requestPermission method wasn't entirely correct.

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[]
}

marcovdb avatar Dec 27 '16 09:12 marcovdb

Should be added in the next release. See https://github.com/Microsoft/TSJS-lib-generator/blob/cd60588b72a9188e89346b3c440a76508b4c0e76/baselines/dom.generated.d.ts#L8360-L8381

mhegazy avatar Feb 01 '17 17:02 mhegazy

How do you watch for a action click?

mwent-cray avatar Jul 07 '17 16:07 mwent-cray

@mwent-cray StackOverflow is the place for questions.

wonea avatar Jul 09 '17 13:07 wonea

@marcovdb @mhegazy is Notification definitions still in TS 2.4? Because I can't seem to find it there :-|

https://github.com/Microsoft/TypeScript/blob/master/src/lib/dom.generated.d.ts

Shavindra avatar Aug 03 '17 18:08 Shavindra

Yes. It's in a different file.

https://github.com/Microsoft/TypeScript/blob/master/src/lib/webworker.generated.d.ts

marcovdb avatar Aug 03 '17 19:08 marcovdb

@marcovdb is there a reason why the interface is different from code mentioned in your last post in thread?

Shavindra avatar Aug 03 '17 20:08 Shavindra

Sorry, no idea. You'd have to ask the person that actually contributed the definitions. That would be @zhengbli.

marcovdb avatar Aug 03 '17 20:08 marcovdb

@zhengbli why were all but 5 of the NotificationOptions omitted? 😞

ialexryan avatar Nov 15 '17 02:11 ialexryan

@ialexryan please file a new issue and give us some context about the issue, and i would be happy to look into it.

mhegazy avatar Nov 20 '17 20:11 mhegazy