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
This commit is contained in:
Julian Hundeloh
2019-05-07 11:15:12 -07:00
committed by Jesse Trinity
parent 2eb83051a5
commit e6e2c8349d
2 changed files with 32 additions and 5 deletions
+11 -3
View File
@@ -1,11 +1,14 @@
// Type definitions for node-pushnotifications 1.0
// Project: https://github.com/appfeel/node-pushnotifications
// Definitions by: Menushka Weeratunga <https://github.com/menushka>
// Julian Hundeloh <https://github.com/jaulz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 2.1
/// <reference types="node" />
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<any>;
send(registrationIds: PushNotifications.RegistrationId|PushNotifications.RegistrationId[], data: PushNotifications.Data, cb: PushNotifications.Callback): void;
send(registrationIds: PushNotifications.RegistrationId|PushNotifications.RegistrationId[], data: PushNotifications.Data): Promise<any>;
}
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;
}
@@ -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);
})