From ca873ea24d78f17351bbae2bd7b77451d6ba5113 Mon Sep 17 00:00:00 2001 From: Patrick Remy Date: Fri, 23 Jul 2021 09:21:53 +0200 Subject: [PATCH 1/6] :hammer: Add bundleId option on register --- src/register/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/register/index.js b/src/register/index.js index b3d69a4..754ea07 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -4,9 +4,9 @@ const registerFCM = require('../fcm'); module.exports = register; -async function register(senderId) { +async function register(senderId, { bundleId } = { bundleId : 'receiver.push.com' }) { // Should be unique by app - One GCM registration/token by app/appId - const appId = `wp:receiver.push.com#${uuidv4()}`; + const appId = `wp:${bundleId}#${uuidv4()}`; const subscription = await registerGCM(appId); const result = await registerFCM({ token : subscription.token, From ba034f6340057d25ef4eac6ac7e902dff9fd4db7 Mon Sep 17 00:00:00 2001 From: Patrick Remy Date: Fri, 23 Jul 2021 09:26:17 +0200 Subject: [PATCH 2/6] :hammer: Fix linting issues --- src/client.js | 4 +++- src/gcm/index.js | 4 ++-- src/parser.js | 4 ++-- src/register/index.js | 5 ++++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/client.js b/src/client.js index 22fbea5..2016722 100644 --- a/src/client.js +++ b/src/client.js @@ -136,14 +136,16 @@ module.exports = class Client extends EventEmitter { } _onSocketClose() { - this.emit('disconnect') + this.emit('disconnect'); this._retry(); } + // eslint-disable-next-line no-unused-vars _onSocketError(error) { // ignore, the close handler takes care of retry } + // eslint-disable-next-line no-unused-vars _onParserError(error) { this._retry(); } diff --git a/src/gcm/index.js b/src/gcm/index.js index 9260129..940082f 100644 --- a/src/gcm/index.js +++ b/src/gcm/index.js @@ -8,8 +8,8 @@ const { toBase64 } = require('../utils/base64'); // Hack to fix PHONE_REGISTRATION_ERROR #17 when bundled with webpack // https://github.com/dcodeIO/protobuf.js#browserify-integration -protobuf.util.Long = Long -protobuf.configure() +protobuf.util.Long = Long; +protobuf.configure(); const serverKey = toBase64(Buffer.from(fcmKey)); diff --git a/src/parser.js b/src/parser.js index 41c4443..ffce4f2 100644 --- a/src/parser.js +++ b/src/parser.js @@ -204,7 +204,7 @@ module.exports = class Parser extends EventEmitter { // Messages with no content are valid; just use the default protobuf for // that tag. if (this._messageSize === 0) { - this.emit('message', {tag: this._messageTag, object: {}}); + this.emit('message', { tag : this._messageTag, object : {} }); this._getNextMessage(); return; } @@ -230,7 +230,7 @@ module.exports = class Parser extends EventEmitter { bytes : Buffer, }); - this.emit('message', {tag: this._messageTag, object: object}); + this.emit('message', { tag : this._messageTag, object : object }); if (this._messageTag === kLoginResponseTag) { if (this._handshakeComplete) { diff --git a/src/register/index.js b/src/register/index.js index 754ea07..c46fa38 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -4,7 +4,10 @@ const registerFCM = require('../fcm'); module.exports = register; -async function register(senderId, { bundleId } = { bundleId : 'receiver.push.com' }) { +async function register( + senderId, + { bundleId } = { bundleId : 'receiver.push.com' } +) { // Should be unique by app - One GCM registration/token by app/appId const appId = `wp:${bundleId}#${uuidv4()}`; const subscription = await registerGCM(appId); From b5aefc33af52e9da576917f52387f3eb778c3404 Mon Sep 17 00:00:00 2001 From: Patrick Remy Date: Fri, 23 Jul 2021 09:46:20 +0200 Subject: [PATCH 3/6] :bug: Fix #34, escape endpoint form data param --- src/fcm/index.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/fcm/index.js b/src/fcm/index.js index 408c097..7cf6440 100644 --- a/src/fcm/index.js +++ b/src/fcm/index.js @@ -17,15 +17,9 @@ async function registerFCM({ senderId, token }) { }, form : { authorized_entity : senderId, - endpoint : `${FCM_ENDPOINT}/${token}`, - encryption_key : keys.publicKey - .replace(/=/g, '') - .replace(/\+/g, '-') - .replace(/\//g, '_'), - encryption_auth : keys.authSecret - .replace(/=/g, '') - .replace(/\+/g, '-') - .replace(/\//g, '_'), + endpoint : escape(`${FCM_ENDPOINT}/${token}`), + encryption_key : keys.publicKey, + encryption_auth : keys.authSecret, }, }); return { From a267683d8a74eed470c209bb5b0d9f31a4532cc4 Mon Sep 17 00:00:00 2001 From: Patrick Remy Date: Fri, 23 Jul 2021 09:47:08 +0200 Subject: [PATCH 4/6] :hammer: Git ignore IDE history files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8316bb8..d6c4f11 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ storage.json web/ .esm-cache test/keys.js +.history From 1237bde8c145dce397ed5bc0ea135479c1c76003 Mon Sep 17 00:00:00 2001 From: selfisekai Date: Sun, 12 Apr 2020 21:51:39 +0200 Subject: [PATCH 5/6] :hammer: Add typescript type declarations --- src/index.d.ts | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/index.d.ts diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..7facb99 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,72 @@ +declare module 'push-receiver' { + + export function listen( + credentials: Credentials, + notificationCallback: (notification: NotificationEnvelope) => unknown, + ): Promise; + + export function register( + senderId: string, + options?: { + bundleId?: string, + }, + ): Promise; + + export function register( + senderId: string, + options: { + bundleId?: string, + }, + ): Promise; + + export interface Credentials { + keys: Keys; + gcm: GcmData; + persistentIds: PersistentId[]; + } + + export interface Keys { + privateKey: string; + publicKey: string; + authSecret: string; + } + + export interface GcmData { + androidId: string; + token: string; + securityToken: string; + } + + // TODO: replace this with actual data + export type FcmData = any; + + export type PersistentId = string; + + export interface NotificationEnvelope { + notification: NotificationContent; + persistentId: PersistentId; + } + + // table 2b. - https://firebase.google.com/docs/cloud-messaging/http-server-ref + export interface NotificationContent { + title?: string; + body?: string; + android_channel_id?: string; + icon?: string; + sound?: string; + tag?: string; + color?: string; + click_action?: string; + body_loc_key?: string; + body_loc_args?: string; // JSON array as string + title_loc_key?: string; + title_loc_args?: string; // JSON array as string + } + + declare class Client { + on(event: 'ON_NOTIFICATION_RECEIVED', listener: (notification: NotificationEnvelope) => void): this; + connect(): Promise; + destroy(): void; + } + +} From 271e0b7d062957f59d302266a418a13fb57bb3c1 Mon Sep 17 00:00:00 2001 From: selfisekai Date: Sun, 12 Apr 2020 21:34:59 +0200 Subject: [PATCH 6/6] :tada: Add skipFcmRegistration option Some developers just need a valid gcm token to work with some APIs. --- src/index.d.ts | 2 ++ src/register/index.js | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/index.d.ts b/src/index.d.ts index 7facb99..9f50455 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -9,6 +9,7 @@ declare module 'push-receiver' { senderId: string, options?: { bundleId?: string, + skipFcmRegistration?: boolean, }, ): Promise; @@ -16,6 +17,7 @@ declare module 'push-receiver' { senderId: string, options: { bundleId?: string, + skipFcmRegistration?: boolean, }, ): Promise; diff --git a/src/register/index.js b/src/register/index.js index c46fa38..c114198 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -6,11 +6,17 @@ module.exports = register; async function register( senderId, - { bundleId } = { bundleId : 'receiver.push.com' } + { bundleId, skipFcmRegistration } = { + bundleId : 'receiver.push.com', + skipFcmRegistration : false, + } ) { // Should be unique by app - One GCM registration/token by app/appId const appId = `wp:${bundleId}#${uuidv4()}`; const subscription = await registerGCM(appId); + if (skipFcmRegistration) { + return { gcm : subscription }; + } const result = await registerFCM({ token : subscription.token, senderId,