UNPKG

11.8 kBTypeScriptView Raw
1/*! firebase-admin v10.0.0 */
2/*!
3 * @license
4 * Copyright 2017 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18import { App } from '../app';
19import { BatchResponse, Message, MessagingTopicManagementResponse, MulticastMessage, MessagingDevicesResponse, MessagingDeviceGroupResponse, MessagingPayload, MessagingOptions, MessagingTopicResponse, MessagingConditionResponse } from './messaging-api';
20/**
21 * Messaging service bound to the provided app.
22 */
23export declare class Messaging {
24 private urlPath;
25 private readonly appInternal;
26 private readonly messagingRequestHandler;
27 /**
28 * The {@link firebase-admin.app#App} associated with the current `Messaging` service
29 * instance.
30 *
31 * @example
32 * ```javascript
33 * var app = messaging.app;
34 * ```
35 */
36 get app(): App;
37 /**
38 * Sends the given message via FCM.
39 *
40 * @param message - The message payload.
41 * @param dryRun - Whether to send the message in the dry-run
42 * (validation only) mode.
43 * @returns A promise fulfilled with a unique message ID
44 * string after the message has been successfully handed off to the FCM
45 * service for delivery.
46 */
47 send(message: Message, dryRun?: boolean): Promise<string>;
48 /**
49 * Sends all the messages in the given array via Firebase Cloud Messaging.
50 * Employs batching to send the entire list as a single RPC call. Compared
51 * to the `send()` method, this method is a significantly more efficient way
52 * to send multiple messages.
53 *
54 * The responses list obtained from the return value
55 * corresponds to the order of tokens in the `MulticastMessage`. An error
56 * from this method indicates a total failure -- i.e. none of the messages in
57 * the list could be sent. Partial failures are indicated by a `BatchResponse`
58 * return value.
59 *
60 * @param messages - A non-empty array
61 * containing up to 500 messages.
62 * @param dryRun - Whether to send the messages in the dry-run
63 * (validation only) mode.
64 * @returns A Promise fulfilled with an object representing the result of the
65 * send operation.
66 */
67 sendAll(messages: Message[], dryRun?: boolean): Promise<BatchResponse>;
68 /**
69 * Sends the given multicast message to all the FCM registration tokens
70 * specified in it.
71 *
72 * This method uses the `sendAll()` API under the hood to send the given
73 * message to all the target recipients. The responses list obtained from the
74 * return value corresponds to the order of tokens in the `MulticastMessage`.
75 * An error from this method indicates a total failure -- i.e. the message was
76 * not sent to any of the tokens in the list. Partial failures are indicated by
77 * a `BatchResponse` return value.
78 *
79 * @param message - A multicast message
80 * containing up to 500 tokens.
81 * @param dryRun - Whether to send the message in the dry-run
82 * (validation only) mode.
83 * @returns A Promise fulfilled with an object representing the result of the
84 * send operation.
85 */
86 sendMulticast(message: MulticastMessage, dryRun?: boolean): Promise<BatchResponse>;
87 /**
88 * Sends an FCM message to a single device corresponding to the provided
89 * registration token.
90 *
91 * See {@link https://firebase.google.com/docs/cloud-messaging/admin/legacy-fcm#send_to_individual_devices |
92 * Send to individual devices}
93 * for code samples and detailed documentation. Takes either a
94 * `registrationToken` to send to a single device or a
95 * `registrationTokens` parameter containing an array of tokens to send
96 * to multiple devices.
97 *
98 * @param registrationToken - A device registration token or an array of
99 * device registration tokens to which the message should be sent.
100 * @param payload - The message payload.
101 * @param options - Optional options to
102 * alter the message.
103 *
104 * @returns A promise fulfilled with the server's response after the message
105 * has been sent.
106 */
107 sendToDevice(registrationTokenOrTokens: string | string[], payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDevicesResponse>;
108 /**
109 * Sends an FCM message to a device group corresponding to the provided
110 * notification key.
111 *
112 * See {@link https://firebase.google.com/docs/cloud-messaging/admin/legacy-fcm#send_to_a_device_group |
113 * Send to a device group} for code samples and detailed documentation.
114 *
115 * @param notificationKey - The notification key for the device group to
116 * which to send the message.
117 * @param payload - The message payload.
118 * @param options - Optional options to
119 * alter the message.
120 *
121 * @returns A promise fulfilled with the server's response after the message
122 * has been sent.
123 */
124 sendToDeviceGroup(notificationKey: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingDeviceGroupResponse>;
125 /**
126 * Sends an FCM message to a topic.
127 *
128 * See {@link https://firebase.google.com/docs/cloud-messaging/admin/legacy-fcm#send_to_a_topic |
129 * Send to a topic} for code samples and detailed documentation.
130 *
131 * @param topic - The topic to which to send the message.
132 * @param payload - The message payload.
133 * @param options - Optional options to
134 * alter the message.
135 *
136 * @returns A promise fulfilled with the server's response after the message
137 * has been sent.
138 */
139 sendToTopic(topic: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingTopicResponse>;
140 /**
141 * Sends an FCM message to a condition.
142 *
143 * See {@link https://firebase.google.com/docs/cloud-messaging/admin/legacy-fcm#send_to_a_condition |
144 * Send to a condition}
145 * for code samples and detailed documentation.
146 *
147 * @param condition - The condition determining to which topics to send
148 * the message.
149 * @param payload - The message payload.
150 * @param options - Optional options to
151 * alter the message.
152 *
153 * @returns A promise fulfilled with the server's response after the message
154 * has been sent.
155 */
156 sendToCondition(condition: string, payload: MessagingPayload, options?: MessagingOptions): Promise<MessagingConditionResponse>;
157 /**
158 * Subscribes a device to an FCM topic.
159 *
160 * See {@link https://firebase.google.com/docs/cloud-messaging/manage-topics#suscribe_and_unsubscribe_using_the |
161 * Subscribe to a topic}
162 * for code samples and detailed documentation. Optionally, you can provide an
163 * array of tokens to subscribe multiple devices.
164 *
165 * @param registrationTokens - A token or array of registration tokens
166 * for the devices to subscribe to the topic.
167 * @param topic - The topic to which to subscribe.
168 *
169 * @returns A promise fulfilled with the server's response after the device has been
170 * subscribed to the topic.
171 */
172 subscribeToTopic(registrationTokenOrTokens: string | string[], topic: string): Promise<MessagingTopicManagementResponse>;
173 /**
174 * Unsubscribes a device from an FCM topic.
175 *
176 * See {@link https://firebase.google.com/docs/cloud-messaging/admin/manage-topic-subscriptions#unsubscribe_from_a_topic |
177 * Unsubscribe from a topic}
178 * for code samples and detailed documentation. Optionally, you can provide an
179 * array of tokens to unsubscribe multiple devices.
180 *
181 * @param registrationTokens - A device registration token or an array of
182 * device registration tokens to unsubscribe from the topic.
183 * @param topic - The topic from which to unsubscribe.
184 *
185 * @returns A promise fulfilled with the server's response after the device has been
186 * unsubscribed from the topic.
187 */
188 unsubscribeFromTopic(registrationTokenOrTokens: string | string[], topic: string): Promise<MessagingTopicManagementResponse>;
189 private getUrlPath;
190 /**
191 * Helper method which sends and handles topic subscription management requests.
192 *
193 * @param registrationTokenOrTokens - The registration token or an array of
194 * registration tokens to unsubscribe from the topic.
195 * @param topic - The topic to which to subscribe.
196 * @param methodName - The name of the original method called.
197 * @param path - The endpoint path to use for the request.
198 *
199 * @returns A Promise fulfilled with the parsed server
200 * response.
201 */
202 private sendTopicManagementRequest;
203 /**
204 * Validates the types of the messaging payload and options. If invalid, an error will be thrown.
205 *
206 * @param payload - The messaging payload to validate.
207 * @param options - The messaging options to validate.
208 */
209 private validateMessagingPayloadAndOptionsTypes;
210 /**
211 * Validates the messaging payload. If invalid, an error will be thrown.
212 *
213 * @param payload - The messaging payload to validate.
214 *
215 * @returns A copy of the provided payload with whitelisted properties switched
216 * from camelCase to underscore_case.
217 */
218 private validateMessagingPayload;
219 /**
220 * Validates the messaging options. If invalid, an error will be thrown.
221 *
222 * @param options - The messaging options to validate.
223 *
224 * @returns A copy of the provided options with whitelisted properties switched
225 * from camelCase to underscore_case.
226 */
227 private validateMessagingOptions;
228 /**
229 * Validates the type of the provided registration token(s). If invalid, an error will be thrown.
230 *
231 * @param registrationTokenOrTokens - The registration token(s) to validate.
232 * @param method - The method name to use in error messages.
233 * @param errorInfo - The error info to use if the registration tokens are invalid.
234 */
235 private validateRegistrationTokensType;
236 /**
237 * Validates the provided registration tokens. If invalid, an error will be thrown.
238 *
239 * @param registrationTokenOrTokens - The registration token or an array of
240 * registration tokens to validate.
241 * @param method - The method name to use in error messages.
242 * @param errorInfo - The error info to use if the registration tokens are invalid.
243 */
244 private validateRegistrationTokens;
245 /**
246 * Validates the type of the provided topic. If invalid, an error will be thrown.
247 *
248 * @param topic - The topic to validate.
249 * @param method - The method name to use in error messages.
250 * @param errorInfo - The error info to use if the topic is invalid.
251 */
252 private validateTopicType;
253 /**
254 * Validates the provided topic. If invalid, an error will be thrown.
255 *
256 * @param topic - The topic to validate.
257 * @param method - The method name to use in error messages.
258 * @param errorInfo - The error info to use if the topic is invalid.
259 */
260 private validateTopic;
261 /**
262 * Normalizes the provided topic name by prepending it with '/topics/', if necessary.
263 *
264 * @param topic - The topic name to normalize.
265 *
266 * @returns The normalized topic name.
267 */
268 private normalizeTopic;
269}