From f9f62e79f4c61d1019b6d765f221afe0ed17b647 Mon Sep 17 00:00:00 2001 From: selfisekai Date: Sun, 12 Apr 2020 21:34:59 +0200 Subject: [PATCH 1/3] registration without registerFCM() [optional] some developers just need a valid token to work with some APIs --- src/register/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/register/index.js b/src/register/index.js index b3d69a4..42a1f11 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -4,10 +4,13 @@ const registerFCM = require('../fcm'); module.exports = register; -async function register(senderId) { +async function register(senderId, { noFcmRegistration } = {}) { // Should be unique by app - One GCM registration/token by app/appId const appId = `wp:receiver.push.com#${uuidv4()}`; const subscription = await registerGCM(appId); + if (noFcmRegistration) { + return { gcm : subscription }; + } const result = await registerFCM({ token : subscription.token, senderId, From 4a91fc7952e48d5149b6b176b9bf3ce7bd33d71d Mon Sep 17 00:00:00 2001 From: selfisekai Date: Sun, 12 Apr 2020 21:51:39 +0200 Subject: [PATCH 2/3] 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..5f8adfa --- /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?: { + noFcmRegistration?: false | null, + }, + ): Promise; + + export function register( + senderId: string, + options: { + noFcmRegistration: true, + }, + ): 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 679f2392cdaf58f86404f9ed6bcb2a8e576948af Mon Sep 17 00:00:00 2001 From: selfisekai Date: Sun, 12 Apr 2020 21:52:12 +0200 Subject: [PATCH 3/3] minor code style fix --- src/gcm/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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));