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..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; + } + +} 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,