-
Notifications
You must be signed in to change notification settings - Fork 418
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
Changes from 11 commits
9846af3
97ac410
7bdf972
8b5e564
208378e
96de332
ec3fdc4
6986cc7
37b2740
cf2163d
da81dc2
d914462
55e645e
2316fde
7854f58
9e5d4cf
7a802d9
4e584c4
09f7dd7
e8febe1
2485709
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,19 @@ export interface BaseMessage { | |
| fcmOptions?: FcmOptions; | ||
| } | ||
|
|
||
| /** | ||
| * Interface representing a message that targets a Firebase Installation ID (FID). | ||
| */ | ||
| export interface FidMessage extends BaseMessage { | ||
| /** | ||
| * The Firebase Installation ID (FID) to which the message should be sent. | ||
| */ | ||
| fid: string; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated Use {@link FidMessage} instead. | ||
| */ | ||
| export interface TokenMessage extends BaseMessage { | ||
| token: string; | ||
| } | ||
|
|
@@ -40,18 +53,38 @@ 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. | ||
|
Member
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. Should we mention that token is deprecated here as well?
Contributor
Author
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. Good catch! Added, Thanks! |
||
| */ | ||
| 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. | ||
| * Payload for the `sendEachForMulticast` method. | ||
| * | ||
| * @deprecated Use {@link FidMulticastMessage} instead. | ||
| */ | ||
| export interface MulticastMessage extends BaseMessage { | ||
| /** | ||
| * A list of Firebase Installation IDs (FIDs) to target. | ||
| */ | ||
| fids?: string[]; | ||
| /** | ||
| * A list of registration tokens to target. | ||
| * | ||
| * @deprecated Use `fids` in {@link FidMulticastMessage} instead. | ||
| */ | ||
| tokens: string[]; | ||
| } | ||
|
|
||
| /** | ||
| * Payload for the `sendEachForMulticast` method containing only FIDs. | ||
| */ | ||
| export interface FidMulticastMessage extends BaseMessage { | ||
| /** | ||
| * A list of Firebase Installation IDs (FIDs) to target. | ||
| */ | ||
| fids: string[]; | ||
| } | ||
|
yvonnep165 marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * A notification that can be included in {@link Message}. | ||
| */ | ||
|
|
@@ -1001,7 +1034,7 @@ export interface MessagingTopicManagementResponse { | |
|
|
||
| /** | ||
| * Interface representing the server response from the | ||
| * {@link Messaging.sendEach} and {@link Messaging.sendEachForMulticast} methods. | ||
| * {@link Messaging.sendEach} and `sendEachForMulticast` methods. | ||
| */ | ||
| export interface BatchResponse { | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ import { FirebaseMessagingRequestHandler } from './messaging-api-request-interna | |
|
|
||
| import { | ||
| BatchResponse, | ||
| FidMulticastMessage, | ||
| Message, | ||
| MessagingTopicManagementResponse, | ||
| MulticastMessage, | ||
|
|
@@ -274,50 +275,82 @@ export class Messaging { | |
| } | ||
|
|
||
| /** | ||
| * Sends the given multicast message to all the FCM registration tokens | ||
| * Sends the given multicast message to all the FCM registration tokens or fids | ||
| * specified in it. | ||
| * | ||
| * This method uses the {@link Messaging.sendEach} API under the hood to send the given | ||
| * message to all the target recipients. The responses list obtained from the | ||
| * return value corresponds to the order of tokens in the `MulticastMessage`. | ||
| * return value corresponds to the order of tokens/fids in the `MulticastMessage`. | ||
|
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. nit: Maybe clarify that
Contributor
Author
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. Ok, added! |
||
| * An error from this method or a `BatchResponse` with all failures indicates a total | ||
| * failure, meaning that the messages in the list could be sent. Partial failures or | ||
| * failures are only indicated by a `BatchResponse` return value. | ||
| * failure, meaning that the messages in the list could not be sent. Partial failures | ||
| * are only indicated by a `BatchResponse` return value. | ||
| * | ||
| * @param message - A multicast message | ||
| * containing up to 500 tokens. | ||
| * @deprecated Use the overload accepting {@link FidMulticastMessage} instead. | ||
| * | ||
| * @param message - A multicast message containing up to 500 tokens and/or fids. | ||
| * @param dryRun - Whether to send the message in the dry-run | ||
| * (validation only) mode. | ||
| * @returns A Promise fulfilled with an object representing the result of the | ||
| * send operation. | ||
| */ | ||
| public sendEachForMulticast(message: MulticastMessage, dryRun?: boolean): Promise<BatchResponse> { | ||
| const copy: MulticastMessage = deepCopy(message); | ||
| public sendEachForMulticast(message: MulticastMessage, dryRun?: boolean): Promise<BatchResponse>; | ||
|
|
||
| /** | ||
| * Sends the given multicast message to all the FCM fids specified in it. | ||
| * | ||
| * This method uses the {@link Messaging.sendEach} API under the hood to send the given | ||
| * message to all the target recipients. The responses list obtained from the | ||
| * return value corresponds to the order of fids in the `FidMulticastMessage`. | ||
| * An error from this method or a `BatchResponse` with all failures indicates a total | ||
| * failure, meaning that the messages in the list could not be sent. Partial failures | ||
| * are only indicated by a `BatchResponse` return value. | ||
| * | ||
| * @param message - A multicast message containing up to 500 fids. | ||
| * @param dryRun - Whether to send the message in the dry-run (validation only) mode. | ||
| * @returns A Promise fulfilled with an object representing the result of the send operation. | ||
| */ | ||
| public sendEachForMulticast(message: FidMulticastMessage, dryRun?: boolean): Promise<BatchResponse>; | ||
|
|
||
| public sendEachForMulticast( | ||
| message: MulticastMessage | FidMulticastMessage, | ||
| dryRun?: boolean, | ||
| ): Promise<BatchResponse> { | ||
| const copy: any = deepCopy(message); | ||
| if (!validator.isNonNullObject(copy)) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'MulticastMessage must be a non-null object'); | ||
| } | ||
| if (!validator.isNonEmptyArray(copy.tokens)) { | ||
|
|
||
| const { tokens, fids, ...baseMessage } = copy; | ||
|
|
||
| const tokenList: string[] = tokens || []; | ||
| const fidList: string[] = fids || []; | ||
|
|
||
| if ('tokens' in copy && !validator.isArray(copy.tokens)) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a valid array'); | ||
| } | ||
| if ('fids' in copy && !validator.isArray(copy.fids)) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'tokens must be a non-empty array'); | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'fids must be a valid array'); | ||
| } | ||
|
yvonnep165 marked this conversation as resolved.
|
||
| if (copy.tokens.length > FCM_MAX_BATCH_SIZE) { | ||
| if (tokenList.length === 0 && fidList.length === 0) { | ||
| throw new FirebaseMessagingError( | ||
| MessagingClientErrorCode.INVALID_ARGUMENT, 'Either tokens or fids must be a non-empty array'); | ||
| } | ||
|
|
||
| const totalLength = tokenList.length + fidList.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`); | ||
| `The total number of tokens and fids must not exceed ${FCM_MAX_BATCH_SIZE}.`); | ||
| } | ||
|
|
||
| const messages: Message[] = copy.tokens.map((token) => { | ||
| return { | ||
| token, | ||
| android: copy.android, | ||
| apns: copy.apns, | ||
| data: copy.data, | ||
| notification: copy.notification, | ||
| webpush: copy.webpush, | ||
| fcmOptions: copy.fcmOptions, | ||
| }; | ||
| }); | ||
| const messages: Message[] = [ | ||
| ...tokenList.map((token) => ({ ...baseMessage, token } as Message)), | ||
| ...fidList.map((fid) => ({ ...baseMessage, fid } as Message)), | ||
| ]; | ||
|
|
||
| return this.sendEach(messages, dryRun); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.