-
Notifications
You must be signed in to change notification settings - Fork 415
feat(fcm): Enable fid and deprecate token for Send API
#3145
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
base: main
Are you sure you want to change the base?
Changes from all commits
9846af3
97ac410
7bdf972
8b5e564
208378e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,13 @@ export interface BaseMessage { | |
| fcmOptions?: FcmOptions; | ||
| } | ||
|
|
||
| export interface FidMessage extends BaseMessage { | ||
| fid: string; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated Use {@link FidMessage} instead. | ||
| */ | ||
| export interface TokenMessage extends BaseMessage { | ||
| token: string; | ||
| } | ||
|
|
@@ -40,16 +47,26 @@ export interface ConditionMessage extends BaseMessage { | |
|
|
||
| /** | ||
| * Payload for the {@link Messaging.send} operation. The payload contains all the fields | ||
| * in the BaseMessage type, and exactly one of token, topic or condition. | ||
| * in the BaseMessage type, and exactly one of fid, token, topic or condition. | ||
| */ | ||
| export type Message = TokenMessage | TopicMessage | ConditionMessage; | ||
| export type Message = FidMessage | TokenMessage | TopicMessage | ConditionMessage; | ||
|
|
||
| /** | ||
| * Payload for the {@link Messaging.sendEachForMulticast} method. The payload contains all the fields | ||
| * in the BaseMessage type, and a list of tokens. | ||
| * in the BaseMessage type, and a list of tokens and/or fids. | ||
| */ | ||
| export interface MulticastMessage extends BaseMessage { | ||
| tokens: string[]; | ||
| /** | ||
| * A list of Firebase Installation IDs (FIDs) to target. | ||
| */ | ||
| fids?: string[]; | ||
|
|
||
| /** | ||
| * A list of registration tokens to target. | ||
| * | ||
| * @deprecated Use {@link MulticastMessage.fids} instead. | ||
| */ | ||
| tokens?: string[]; | ||
| } | ||
|
Comment on lines
58
to
70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing |
||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -297,27 +297,54 @@ export class Messaging { | |
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'MulticastMessage must be a non-null object'); | ||
| } | ||
| if (!validator.isNonEmptyArray(copy.tokens)) { | ||
|
|
||
| const tokens: string[] = copy.tokens || []; | ||
| const fids: string[] = copy.fids || []; | ||
|
|
||
| if ('tokens' in copy && !validator.isNonEmptyArray(copy.tokens)) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a non-empty array'); | ||
| } | ||
| if (copy.tokens.length > FCM_MAX_BATCH_SIZE) { | ||
| if ('fids' in copy && !validator.isNonEmptyArray(copy.fids)) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'fids must be a non-empty array'); | ||
| } | ||
| if (tokens.length === 0 && fids.length === 0) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'Either tokens or fids must be a non-empty array'); | ||
| } | ||
|
|
||
| const totalLength = tokens.length + fids.length; | ||
| if (totalLength > FCM_MAX_BATCH_SIZE) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, | ||
| `tokens list must not contain more than ${FCM_MAX_BATCH_SIZE} items`); | ||
| `tokens and fids list must not contain more than ${FCM_MAX_BATCH_SIZE} items in total`); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| const messages: Message[] = copy.tokens.map((token) => { | ||
| return { | ||
| const messages: Message[] = []; | ||
| tokens.forEach((token) => { | ||
| messages.push({ | ||
| token, | ||
| android: copy.android, | ||
| apns: copy.apns, | ||
| data: copy.data, | ||
| notification: copy.notification, | ||
| webpush: copy.webpush, | ||
| fcmOptions: copy.fcmOptions, | ||
| }; | ||
| }); | ||
| }); | ||
| fids.forEach((fid) => { | ||
| messages.push({ | ||
| fid, | ||
| android: copy.android, | ||
| apns: copy.apns, | ||
| data: copy.data, | ||
| notification: copy.notification, | ||
| webpush: copy.webpush, | ||
| fcmOptions: copy.fcmOptions, | ||
| }); | ||
| }); | ||
|
Comment on lines
+324
to
+346
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for constructing the const { tokens: _, fids: __, ...baseMessage } = copy;
const messages: Message[] = [
...tokens.map((token) => ({ ...baseMessage, token } as Message)),
...fids.map((fid) => ({ ...baseMessage, fid } as Message)),
]; |
||
|
|
||
| return this.sendEach(messages, dryRun); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To address your question in the PR description, it is highly recommended to add docstrings for the new
FidMessageinterface and its fields. This ensures consistency with other public interfaces and improves the developer experience for users of the SDK.