Skip to content
Merged
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
62 changes: 11 additions & 51 deletions src/messaging/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Client } from "../apiv2";
import { logger } from "../logger";
import { FirebaseError } from "../error";
import { Message, Notification, TopicMessage } from "./interfaces";
import { Message, Notification } from "./interfaces";

const TIMEOUT = 10000;

Expand All @@ -13,10 +13,8 @@

/**
* Function to send a message to an FCM Token.
* @param projectId Project ID to which this token belongs to.

Check warning on line 16 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.image"

Check warning on line 16 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.body"

Check warning on line 16 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.title"

Check warning on line 16 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.token"

Check warning on line 16 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.topic"
* @param fcmToken The FCM Token to send to.
* @param title The title of the message.
* @param options The body of the message.
* @param options Parameters including message body and target.
* @return {Promise} Returns a promise fulfilled with a unique message ID string
* after the message has been successfully handed off to the FCM service for delivery.
*/
Expand All @@ -39,11 +37,15 @@
if (!options.token && !options.topic) {
throw new FirebaseError("Must supply either token or topic to send FCM message.");
}
const message: Message = {
token: options.token!,
topic: options.topic!,
notification: notification,
};
const message: Message = options.token
? {
token: options.token!,

Check warning on line 42 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion

Check warning on line 42 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

This assertion is unnecessary since it does not change the type of the expression
notification: notification,
}
: {
topic: options.topic!,

Check warning on line 46 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
notification: notification,
};
const messageData = {
message: message,
};
Expand All @@ -54,53 +56,11 @@
timeout: TIMEOUT,
});
return res.body;
} catch (err: any) {

Check warning on line 59 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
logger.debug(err.message);

Check warning on line 60 in src/messaging/sendMessage.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `Error`
throw new FirebaseError(
`Failed to send message to '${options.token || options.topic}' for the project '${projectId}'. `,
{ original: err },
);
}
}

/**
* Function to send a message to an FCM topic. This will initiate a message fanout to the topic members.
* @param projectId Project ID to which this token belongs to.
* @param fcmToken The FCM Token to send to.
* @param title The title of the message.
* @param body The body of the message.
* @return {Promise} Returns a promise fulfilled with a unique message ID string
* after the message has been successfully handed off to the FCM service for delivery.
*/
export async function sendMessageToFcmTopic(
projectId: string,
topic: string,
title?: string,
body?: string,
): Promise<string> {
try {
const notification: Notification = {
title: title,
body: body,
};
const message: TopicMessage = {
topic: topic,
notification: notification,
};
const messageData = {
message: message,
};
const res = await apiClient.request<null, string>({
method: "POST",
path: `/projects/${projectId}/messages:send`,
body: JSON.stringify(messageData),
timeout: TIMEOUT,
});
return res.body;
} catch (err: any) {
logger.debug(err.message);
throw new FirebaseError(`Failed to send message for the project ${projectId}. `, {
original: err,
});
}
}
Loading