Skip to content
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

typescript type declarations, option to register without registerFCM() #35

Open
wants to merge 3 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
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
72 changes: 72 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
declare module 'push-receiver' {

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

export function register(
senderId: string,
options?: {
noFcmRegistration?: false | null,
},
): Promise<Credentials & FcmData>;

export function register(
senderId: string,
options: {
noFcmRegistration: true,
},
): 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;
}

}
5 changes: 4 additions & 1 deletion src/register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down