From e6e2c8349d7628ebf86767c33a85bfce4b059009 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Tue, 7 May 2019 20:15:12 +0200 Subject: [PATCH] fix(node-pushnotifications): add web support (#34444) * fix: add web support * fix: lint * fix: add web in settings * fix: remove package.json * fix: upgrade Typescript version * fix: lint --- types/node-pushnotifications/index.d.ts | 14 ++++++++--- .../node-pushnotifications-tests.ts | 23 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/types/node-pushnotifications/index.d.ts b/types/node-pushnotifications/index.d.ts index 5b9ed891f9..205c7eef4d 100644 --- a/types/node-pushnotifications/index.d.ts +++ b/types/node-pushnotifications/index.d.ts @@ -1,11 +1,14 @@ // Type definitions for node-pushnotifications 1.0 // Project: https://github.com/appfeel/node-pushnotifications // Definitions by: Menushka Weeratunga +// Julian Hundeloh // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 -// TypeScript Version: 2.1 /// +import * as webPush from 'web-push'; + export = PushNotifications; declare class PushNotifications { @@ -13,8 +16,8 @@ declare class PushNotifications { setOptions(opts: PushNotifications.Settings): void; sendWith(method: PushNotifications.PushMethod, regIds: string[], data: PushNotifications.Data, cb: PushNotifications.Callback): void; - send(registrationIds: string[], data: PushNotifications.Data, cb: PushNotifications.Callback): void; - send(registrationIds: string[], data: PushNotifications.Data): Promise; + send(registrationIds: PushNotifications.RegistrationId|PushNotifications.RegistrationId[], data: PushNotifications.Data, cb: PushNotifications.Callback): void; + send(registrationIds: PushNotifications.RegistrationId|PushNotifications.RegistrationId[], data: PushNotifications.Data): Promise; } declare namespace PushNotifications { @@ -96,6 +99,10 @@ declare namespace PushNotifications { client_secret?: string; }; }; + /** Web */ + web?: webPush.RequestOptions; + /** Always use FCM? */ + isAlwaysUseFCM?: boolean; } interface Data { /** REQUIRED */ @@ -177,4 +184,5 @@ declare namespace PushNotifications { } type PushMethod = (regIds: string[], data: Data, settings: Settings) => void; type Callback = (err: any, result: any) => void; + type RegistrationId = string | webPush.PushSubscription; } diff --git a/types/node-pushnotifications/node-pushnotifications-tests.ts b/types/node-pushnotifications/node-pushnotifications-tests.ts index 6a4d9d3335..2347e878d4 100644 --- a/types/node-pushnotifications/node-pushnotifications-tests.ts +++ b/types/node-pushnotifications/node-pushnotifications-tests.ts @@ -1,4 +1,5 @@ import PushNotifications = require('node-pushnotifications'); +import { supportedContentEncodings } from 'web-push'; const settings = { gcm: { @@ -19,6 +20,17 @@ const settings = { client_id: "null", client_secret: "null", notificationMethod: 'sendTileSquareBlock', + }, + web: { + vapidDetails: { + subject: '< \'mailto\' Address or URL >', + publicKey: '< URL Safe Base64 Encoded Public Key >', + privateKey: '< URL Safe Base64 Encoded Private Key >', + }, + gcmAPIKey: 'gcmkey', + TTL: 2419200, + contentEncoding: supportedContentEncodings.AES_128_GCM, + headers: {}, } }; const push = new PushNotifications(settings); @@ -26,6 +38,13 @@ const push = new PushNotifications(settings); const registrationIds = []; registrationIds.push('INSERT_YOUR_DEVICE_ID'); registrationIds.push('INSERT_OTHER_DEVICE_ID'); +registrationIds.push({ + endpoint: 'https://fcm.googleapis.com/fcm/send/...', + keys: { + auth: '...', + p256dh: '...' + } +}); const data = { title: 'New push notification', @@ -41,8 +60,8 @@ push.send(registrationIds, data, (err, result) => { } }); -// Or you could use it as a promise: -push.send(registrationIds, data) +// Or you could use it as a promise and send only a single notifications: +push.send(registrationIds[0], data) .then((results) => { console.log(results); })