Skip to content

Commit 8aedf92

Browse files
committed
Polished index.d.ts based on Arthur's feedback
1 parent 5b719a8 commit 8aedf92

File tree

8 files changed

+125
-35
lines changed

8 files changed

+125
-35
lines changed

packages/firebase/index.d.ts

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6950,7 +6950,7 @@ declare namespace firebase.messaging {
69506950
*/
69516951
interface Messaging {
69526952
/**
6953-
* Deletes the only registration token associated with this messaging instance and unsubscribes
6953+
* Deletes the registration token associated with this messaging instance and unsubscribes
69546954
* this messaging instance from the push subscription.
69556955
*
69566956
* @return The promise resolves when the token has been successfully deleted.
@@ -6970,19 +6970,26 @@ declare namespace firebase.messaging {
69706970
deleteToken(token: string): Promise<boolean>;
69716971

69726972
/**
6973-
* Subscribes the user to push notifications. Returns an FCM registration
6973+
* Subscribes the messaging instance to push notifications. Returns an FCM registration
69746974
* token that can be used to send push messages to the user.
69756975
*
69766976
* If notification permission isn't already granted, this method asks the
69776977
* user for permission. The returned promise rejects if the user does not
69786978
* allow the app to show notifications.
69796979
*
6980-
* @param options.vapidKey the public server key provided to push services. It is used to authenticate
6981-
* the push subscribers to receive push messages only from sending servers that holds the corresponding private key. If it is not provided, a default VAPID key will be be used. Note that some push services (Chrome Push Service) require a non-default VAPID key. Therefore, it is recommended to to generate and import a VAPID key for your project with {@link https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_with_fcm Configure Web Credentials with FCM}. Also See {@link https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol The Web Push Protocol} for details on web push services.}
6980+
* @param options.vapidKey the public server key provided to push services. It is used to
6981+
* authenticate the push subscribers to receive push messages only from sending servers that
6982+
* holds the corresponding private key. If it is not provided, a default VAPID key will be used.
6983+
* Note that some push services (Chrome Push Service) require a non-default VAPID key.
6984+
* Therefore, it is recommended to generate and import a VAPID key for your project with
6985+
* {@link https://firebase.google.com/docs/cloud-messaging/js/client#configure_web_credentials_with_fcm Configure Web Credentials with FCM}.
6986+
* See {@link https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol The Web Push Protocol}
6987+
* for details on web push services.}
69826988
*
69836989
* @param options.serviceWorkerRegistration the service worker registration for receiving push messaging.
69846990
* If it is not provided explicitly, you need to have a `firebase-messaging-sw.js` at your root location.
6985-
* See {@link https://firebase.google.com/docs/cloud-messaging/js/client#retrieve-the-current-registration-token Retrieve the current registration token} for more details.
6991+
* See {@link https://firebase.google.com/docs/cloud-messaging/js/client#retrieve-the-current-registration-token Retrieve the current registration token}
6992+
* for more details.
69866993
*
69876994
* @return The promise resolves with the FCM token string.
69886995
*
@@ -7027,7 +7034,8 @@ declare namespace firebase.messaging {
70277034
): firebase.Unsubscribe;
70287035

70297036
/**
7030-
* Called when a message is received while the app is in the background. An app is considered as a background app if no active window is displayed.
7037+
* Called when a message is received while the app is in the background. An app is considered
7038+
* as a background app if no active window is displayed.
70317039
*
70327040
* @param
70337041
* nextOrObserver This function, or observer object with `next` defined,
@@ -7115,7 +7123,10 @@ declare namespace firebase.messaging {
71157123
}
71167124

71177125
/**
7118-
* Message payload that contains the notification payload that is represented with {@link firebase.Messaging.NotificationPayload} and the data payload that contains an arbitrary number of key-value pairs sent by developers through the {@link https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification Send API}
7126+
* Message payload that contains the notification payload that is represented with
7127+
* {@link firebase.Messaging.NotificationPayload} and the data payload that contains an
7128+
* arbitrary number of key-value pairs sent by developers through
7129+
* the {@link https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification Send API}
71197130
*/
71207131
export interface MessagePayload {
71217132
/**
@@ -7126,21 +7137,38 @@ declare namespace firebase.messaging {
71267137
/**
71277138
* Arbitrary key/value pairs.
71287139
*/
7129-
71307140
data?: { [key: string]: string };
71317141

71327142
/**
71337143
* See {@link firebase.Messaging.FcmOptions}.
71347144
*/
7135-
71367145
fcmOptions?: FcmOptions;
7146+
7147+
/**
7148+
* The sender of this message.
7149+
*/
7150+
from: string;
7151+
7152+
/**
7153+
* The collapse key of this message. See more {@link https://firebase.google.com/docs/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages}.
7154+
*/
7155+
collapseKey: string;
71377156
}
71387157

71397158
/**
71407159
* Options for features provided by the FCM SDK for Web. See more {@link https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions}.
71417160
*/
71427161
export interface FcmOptions {
7162+
/**
7163+
* The link to open when the user clicks on the notification. For all URL values, HTTPS is required.
7164+
* For example, by setting this value to your app's URL, a notification click click event will
7165+
* put your app in focus for the user.
7166+
*/
71437167
link?: string;
7168+
7169+
/**
7170+
* Label associated with the message's analytics data.
7171+
*/
71447172
analyticsLabel?: string;
71457173
}
71467174

packages/messaging/src/controllers/sw-controller.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,20 @@ const DISPLAY_MESSAGE: MessagePayloadInternal = {
6060
},
6161
fcmOptions: {
6262
link: 'https://example.org'
63-
}
63+
},
64+
from: 'from',
65+
// eslint-disable-next-line camelcase
66+
collapse_key: 'collapse'
6467
};
6568

6669
// internal message payload (parsed directly from the push event) that contains and only contains data payload.
6770
const DATA_MESSAGE: MessagePayloadInternal = {
6871
data: {
6972
key: 'value'
70-
}
73+
},
74+
from: 'from',
75+
// eslint-disable-next-line camelcase
76+
collapse_key: 'collapse'
7177
};
7278

7379
describe('SwController', () => {

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ export class SwController implements FirebaseMessaging, FirebaseService {
105105
// Calling this from an old SW can cause all kinds of trouble.
106106
async getToken(): Promise<string> {
107107
if (!this.vapidKey) {
108-
// Call getToken using the current VAPID key if there already is a token. This is needed because usePublicVapidKey was not available in SW. It will be removed when vapidKey becomes a parameter of getToken, or when getToken is removed from SW.
108+
// Call getToken using the current VAPID key if there already is a token. This is needed
109+
// because usePublicVapidKey was not available in SW. It will be removed when vapidKey becomes
110+
// a parameter of getToken, or when getToken is removed from SW.
109111
const tokenDetails = await dbGet(this.firebaseDependencies);
110112
this.vapidKey =
111113
tokenDetails?.subscriptionOptions?.vapidKey ?? DEFAULT_VAPID_KEY;
@@ -156,7 +158,9 @@ export class SwController implements FirebaseMessaging, FirebaseService {
156158
* A handler for push events that shows notifications based on the content of the payload.
157159
*
158160
* The payload must be a JSON-encoded Object with a `notification` key. The
159-
* value of the `notification` property will be used as the NotificationOptions object passed to showNotification. Additionally, the `title` property of the notification object will be used as the title.
161+
* value of the `notification` property will be used as the NotificationOptions object passed to
162+
* showNotification. Additionally, the `title` property of the notification object will be used as
163+
* the title.
160164
*
161165
* If there is no notification data in the payload then no notification will be shown.
162166
*/
@@ -176,7 +180,7 @@ export class SwController implements FirebaseMessaging, FirebaseService {
176180
return sendMessagePayloadInternalToWindows(clientList, internalPayload);
177181
}
178182

179-
// background handling: display and pass to onBackgroundMessage
183+
// background handling: display and pass to onBackgroundMessage hook
180184
if (!!internalPayload.notification) {
181185
await showNotification(wrapInternalPayload(internalPayload));
182186
} else if (this.bgMessageHandler) {
@@ -213,7 +217,8 @@ export class SwController implements FirebaseMessaging, FirebaseService {
213217
if (!internalPayload) {
214218
return;
215219
} else if (event.action) {
216-
// User clicked on an action button. This will allow developers to act on action button clicks by using a custom onNotificationClick listener that they define.
220+
// User clicked on an action button. This will allow developers to act on action button clicks
221+
// by using a custom onNotificationClick listener that they define.
217222
return;
218223
}
219224

@@ -230,7 +235,8 @@ export class SwController implements FirebaseMessaging, FirebaseService {
230235
if (!client) {
231236
// Unable to find window client so need to open one. This also focuses the opened client.
232237
client = await self.clients.openWindow(link);
233-
// Wait three seconds for the client to initialize and set up the message handler so that it can receive the message.
238+
// Wait three seconds for the client to initialize and set up the message handler so that it
239+
// can receive the message.
234240
await sleep(3000);
235241
} else {
236242
client = await client.focus();
@@ -254,7 +260,9 @@ function wrapInternalPayload(
254260
...((internalPayload.notification as unknown) as NotificationPayloadInternal)
255261
};
256262

257-
// Put the message payload under FCM_MSG name so we can identify the notification as being an FCM notification vs a notification from somewhere else (i.e. normal web push or developer generated notification).
263+
// Put the message payload under FCM_MSG name so we can identify the notification as being an
264+
// FCM notification vs a notification from somewhere else (i.e. normal web push or developer
265+
// generated notification).
258266
wrappedInternalPayload.data = {
259267
[FCM_MSG]: internalPayload
260268
};
@@ -282,7 +290,8 @@ function getMessagePayloadInternal({
282290
* @return Returns an existing window client or a newly opened WindowClient.
283291
*/
284292
async function getWindowClient(url: string): Promise<WindowClient | null> {
285-
// Use URL to normalize the URL when comparing to windowClients. This at least handles whether to include trailing slashes or not
293+
// Use URL to normalize the URL when comparing to windowClients. This at least handles whether
294+
// to include trailing slashes or not
286295
const parsedURL = new URL(url, self.location.href);
287296

288297
const clientList = await getClientList();
@@ -305,7 +314,8 @@ function hasVisibleClients(clientList: WindowClient[]): boolean {
305314
return clientList.some(
306315
client =>
307316
client.visibilityState === 'visible' &&
308-
// Ignore chrome-extension clients as that matches the background pages of extensions, which are always considered visible for some reason.
317+
// Ignore chrome-extension clients as that matches the background pages of extensions,
318+
// which are always considered visible for some reason.
309319
!client.url.startsWith('chrome-extension://')
310320
);
311321
}
@@ -333,7 +343,8 @@ function getClientList(): Promise<WindowClient[]> {
333343
function showNotification(
334344
notificationPayloadInternal: NotificationPayloadInternal
335345
): Promise<void> {
336-
// Note: Firefox does not support the maxActions property. https://developer.mozilla.org/en-US/docs/Web/API/notification/maxActions
346+
// Note: Firefox does not support the maxActions property.
347+
// https://developer.mozilla.org/en-US/docs/Web/API/notification/maxActions
337348
const { actions } = notificationPayloadInternal;
338349
const { maxActions } = Notification;
339350
if (actions && maxActions && actions.length > maxActions) {

packages/messaging/src/controllers/window-controller.test.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ describe('WindowController', () => {
382382
const internalPayload: MessagePayloadInternal = {
383383
notification: { title: 'hello', body: 'world' },
384384
messageType: MessageType.PUSH_RECEIVED,
385-
isFirebaseMessaging: true
385+
isFirebaseMessaging: true,
386+
from: 'from',
387+
// eslint-disable-next-line camelcase
388+
collapse_key: 'collapse'
386389
};
387390

388391
await messageEventListener(
@@ -403,7 +406,10 @@ describe('WindowController', () => {
403406
const internalPayload: MessagePayloadInternal = {
404407
notification: { title: 'hello', body: 'world' },
405408
messageType: MessageType.PUSH_RECEIVED,
406-
isFirebaseMessaging: true
409+
isFirebaseMessaging: true,
410+
from: 'from',
411+
// eslint-disable-next-line camelcase
412+
collapse_key: 'collapse'
407413
};
408414

409415
await messageEventListener(
@@ -421,7 +427,10 @@ describe('WindowController', () => {
421427
const internalPayload: MessagePayloadInternal = {
422428
notification: { title: 'hello', body: 'world' },
423429
messageType: MessageType.PUSH_RECEIVED,
424-
isFirebaseMessaging: true
430+
isFirebaseMessaging: true,
431+
from: 'from',
432+
// eslint-disable-next-line camelcase
433+
collapse_key: 'collapse'
425434
};
426435

427436
await messageEventListener(
@@ -512,7 +521,10 @@ describe('WindowController', () => {
512521
const internalPayload: MessagePayloadInternal = {
513522
notification: { title: 'hello', body: 'world' },
514523
messageType: MessageType.PUSH_RECEIVED,
515-
isFirebaseMessaging: true
524+
isFirebaseMessaging: true,
525+
from: 'from',
526+
// eslint-disable-next-line camelcase
527+
collapse_key: 'collapse'
516528
};
517529

518530
await messageEventListener(
@@ -529,7 +541,10 @@ describe('WindowController', () => {
529541
const internalPayload: MessagePayloadInternal = {
530542
notification: { title: 'hello', body: 'world' },
531543
messageType: MessageType.NOTIFICATION_CLICKED,
532-
isFirebaseMessaging: true
544+
isFirebaseMessaging: true,
545+
from: 'from',
546+
// eslint-disable-next-line camelcase
547+
collapse_key: 'collapse'
533548
};
534549

535550
await messageEventListener(
@@ -550,7 +565,10 @@ describe('WindowController', () => {
550565
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]: '1'
551566
},
552567
messageType: MessageType.PUSH_RECEIVED,
553-
isFirebaseMessaging: true
568+
isFirebaseMessaging: true,
569+
from: 'from',
570+
// eslint-disable-next-line camelcase
571+
collapse_key: 'collapse'
554572
};
555573

556574
await messageEventListener(
@@ -583,7 +601,10 @@ describe('WindowController', () => {
583601
[CONSOLE_CAMPAIGN_ANALYTICS_ENABLED]: '1'
584602
},
585603
messageType: MessageType.NOTIFICATION_CLICKED,
586-
isFirebaseMessaging: true
604+
isFirebaseMessaging: true,
605+
from: 'from',
606+
// eslint-disable-next-line camelcase
607+
collapse_key: 'collapse'
587608
};
588609

589610
await messageEventListener(

packages/messaging/src/controllers/window-controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ export class WindowController implements FirebaseMessaging, FirebaseService {
7474
return;
7575
}
7676

77+
// onMessageCallback is either a function or observer/subscriber.
7778
if (
7879
this.onMessageCallback &&
7980
internalPayload.messageType === MessageType.PUSH_RECEIVED
8081
) {
81-
this.onMessageCallback(externalizePayload(internalPayload));
82+
if (typeof this.onMessageCallback === 'function') {
83+
this.onMessageCallback(externalizePayload(internalPayload));
84+
} else {
85+
this.onMessageCallback.next(externalizePayload(internalPayload));
86+
}
8287
}
8388

8489
const dataPayload = internalPayload.data;

packages/messaging/src/helpers/externalizePayload.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ describe('externalizePayload', () => {
3131
title: 'title',
3232
body: 'body',
3333
image: 'image'
34-
}
34+
},
35+
from: 'from',
36+
collapse_key: 'collapse'
3537
};
3638

3739
const payload: MessagePayload = {
38-
notification: { title: 'title', body: 'body', image: 'image' }
40+
notification: { title: 'title', body: 'body', image: 'image' },
41+
from: 'from',
42+
collapseKey: 'collapse'
3943
};
4044
expect(externalizePayload(internalPayload)).to.deep.equal(payload);
4145
});
@@ -46,11 +50,15 @@ describe('externalizePayload', () => {
4650
foo: 'foo',
4751
bar: 'bar',
4852
baz: 'baz'
49-
}
53+
},
54+
from: 'from',
55+
collapse_key: 'collapse'
5056
};
5157

5258
const payload: MessagePayload = {
53-
data: { foo: 'foo', bar: 'bar', baz: 'baz' }
59+
data: { foo: 'foo', bar: 'bar', baz: 'baz' },
60+
from: 'from',
61+
collapseKey: 'collapse'
5462
};
5563
expect(externalizePayload(internalPayload)).to.deep.equal(payload);
5664
});
@@ -70,7 +78,9 @@ describe('externalizePayload', () => {
7078
fcmOptions: {
7179
link: 'link',
7280
analytics_label: 'label'
73-
}
81+
},
82+
from: 'from',
83+
collapse_key: 'collapse'
7484
};
7585

7686
const payload: MessagePayload = {
@@ -87,7 +97,9 @@ describe('externalizePayload', () => {
8797
fcmOptions: {
8898
link: 'link',
8999
analyticsLabel: 'label'
90-
}
100+
},
101+
from: 'from',
102+
collapseKey: 'collapse'
91103
};
92104
expect(externalizePayload(internalPayload)).to.deep.equal(payload);
93105
});

packages/messaging/src/helpers/externalizePayload.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export function externalizePayload(
2727
const payload: MessagePayload = {
2828
notification: {},
2929
data: {},
30-
fcmOptions: {}
30+
fcmOptions: {},
31+
from: internalPayload.from,
32+
collapseKey: internalPayload.collapse_key
3133
} as MessagePayload;
3234

3335
propagateNotificationPayload(payload, internalPayload);

0 commit comments

Comments
 (0)