Skip to content

Fix #34 FCM registering problems and add TypeScript definitions #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ storage.json
web/
.esm-cache
test/keys.js
.history
4 changes: 3 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
12 changes: 3 additions & 9 deletions src/fcm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/gcm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
74 changes: 74 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
declare module 'push-receiver' {

export function listen(
credentials: Credentials,
notificationCallback: (notification: NotificationEnvelope) => unknown,
): Promise<Client>;

export function register(
senderId: string,
options?: {
bundleId?: string,
skipFcmRegistration?: boolean,
},
): Promise<Credentials & FcmData>;

export function register(
senderId: string,
options: {
bundleId?: string,
skipFcmRegistration?: boolean,
},
): Promise<Credentials>;

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<void>;
destroy(): void;
}

}
4 changes: 2 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down