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 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/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 { 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/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..9f50455 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,74 @@ +declare module 'push-receiver' { + + export function listen( + credentials: Credentials, + notificationCallback: (notification: NotificationEnvelope) => unknown, + ): Promise; + + export function register( + senderId: string, + options?: { + bundleId?: string, + skipFcmRegistration?: boolean, + }, + ): Promise; + + export function register( + senderId: string, + options: { + bundleId?: string, + skipFcmRegistration?: boolean, + }, + ): 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; + } + +} 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 b3d69a4..c114198 100644 --- a/src/register/index.js +++ b/src/register/index.js @@ -4,10 +4,19 @@ const registerFCM = require('../fcm'); module.exports = register; -async function register(senderId) { +async function register( + senderId, + { bundleId, skipFcmRegistration } = { + bundleId : 'receiver.push.com', + skipFcmRegistration : false, + } +) { // 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); + if (skipFcmRegistration) { + return { gcm : subscription }; + } const result = await registerFCM({ token : subscription.token, senderId,