@react-native-firebase/messaging 23.7.0 → 23.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/commonjs/index.js +61 -0
  3. package/dist/commonjs/index.js.map +1 -0
  4. package/dist/commonjs/modular.js +439 -0
  5. package/dist/commonjs/modular.js.map +1 -0
  6. package/dist/commonjs/namespaced.js +407 -0
  7. package/dist/commonjs/namespaced.js.map +1 -0
  8. package/dist/commonjs/package.json +1 -0
  9. package/dist/commonjs/remoteMessageOptions.js +87 -0
  10. package/dist/commonjs/remoteMessageOptions.js.map +1 -0
  11. package/dist/commonjs/statics.js +29 -0
  12. package/dist/commonjs/statics.js.map +1 -0
  13. package/dist/commonjs/types/messaging.js +105 -0
  14. package/dist/commonjs/types/messaging.js.map +1 -0
  15. package/dist/commonjs/version.js +9 -0
  16. package/dist/commonjs/version.js.map +1 -0
  17. package/dist/module/index.js +30 -0
  18. package/dist/module/index.js.map +1 -0
  19. package/{lib/modular/index.js → dist/module/modular.js} +135 -119
  20. package/dist/module/modular.js.map +1 -0
  21. package/{lib/index.js → dist/module/namespaced.js} +70 -193
  22. package/dist/module/namespaced.js.map +1 -0
  23. package/dist/module/package.json +1 -0
  24. package/{lib → dist/module}/remoteMessageOptions.js +10 -27
  25. package/dist/module/remoteMessageOptions.js.map +1 -0
  26. package/dist/module/statics.js +25 -0
  27. package/dist/module/statics.js.map +1 -0
  28. package/dist/module/types/messaging.js +124 -0
  29. package/dist/module/types/messaging.js.map +1 -0
  30. package/dist/module/version.js +5 -0
  31. package/dist/module/version.js.map +1 -0
  32. package/dist/typescript/commonjs/lib/index.d.ts +6 -0
  33. package/dist/typescript/commonjs/lib/index.d.ts.map +1 -0
  34. package/{lib/modular/index.d.ts → dist/typescript/commonjs/lib/modular.d.ts} +86 -164
  35. package/dist/typescript/commonjs/lib/modular.d.ts.map +1 -0
  36. package/dist/typescript/commonjs/lib/namespaced.d.ts +12 -0
  37. package/dist/typescript/commonjs/lib/namespaced.d.ts.map +1 -0
  38. package/dist/typescript/commonjs/lib/remoteMessageOptions.d.ts +14 -0
  39. package/dist/typescript/commonjs/lib/remoteMessageOptions.d.ts.map +1 -0
  40. package/dist/typescript/commonjs/lib/statics.d.ts +20 -0
  41. package/dist/typescript/commonjs/lib/statics.d.ts.map +1 -0
  42. package/dist/typescript/commonjs/lib/types/messaging.d.ts +673 -0
  43. package/dist/typescript/commonjs/lib/types/messaging.d.ts.map +1 -0
  44. package/dist/typescript/commonjs/lib/version.d.ts +2 -0
  45. package/dist/typescript/commonjs/lib/version.d.ts.map +1 -0
  46. package/dist/typescript/commonjs/package.json +1 -0
  47. package/dist/typescript/module/lib/index.d.ts +6 -0
  48. package/dist/typescript/module/lib/index.d.ts.map +1 -0
  49. package/dist/typescript/module/lib/modular.d.ts +247 -0
  50. package/dist/typescript/module/lib/modular.d.ts.map +1 -0
  51. package/dist/typescript/module/lib/namespaced.d.ts +12 -0
  52. package/dist/typescript/module/lib/namespaced.d.ts.map +1 -0
  53. package/dist/typescript/module/lib/remoteMessageOptions.d.ts +14 -0
  54. package/dist/typescript/module/lib/remoteMessageOptions.d.ts.map +1 -0
  55. package/dist/typescript/module/lib/statics.d.ts +20 -0
  56. package/dist/typescript/module/lib/statics.d.ts.map +1 -0
  57. package/dist/typescript/module/lib/types/messaging.d.ts +673 -0
  58. package/dist/typescript/module/lib/types/messaging.d.ts.map +1 -0
  59. package/dist/typescript/module/lib/version.d.ts +2 -0
  60. package/dist/typescript/module/lib/version.d.ts.map +1 -0
  61. package/dist/typescript/module/package.json +1 -0
  62. package/lib/index.ts +44 -0
  63. package/lib/modular.ts +447 -0
  64. package/lib/namespaced.ts +583 -0
  65. package/lib/remoteMessageOptions.ts +110 -0
  66. package/lib/{statics.js → statics.ts} +6 -3
  67. package/lib/types/messaging.ts +815 -0
  68. package/lib/version.ts +2 -0
  69. package/package.json +54 -8
  70. package/tsconfig.json +21 -0
  71. package/lib/index.d.ts +0 -1233
  72. package/lib/version.js +0 -2
@@ -0,0 +1,583 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import {
19
+ hasOwnProperty,
20
+ isAndroid,
21
+ isBoolean,
22
+ isFunction,
23
+ isIOS,
24
+ isObject,
25
+ isString,
26
+ isUndefined,
27
+ } from '@react-native-firebase/app/lib/common';
28
+ import {
29
+ createModuleNamespace,
30
+ FirebaseModule,
31
+ getFirebaseRoot,
32
+ } from '@react-native-firebase/app/lib/internal';
33
+ import { AppRegistry } from 'react-native';
34
+ import remoteMessageOptions from './remoteMessageOptions';
35
+ import { version } from './version';
36
+ import {
37
+ AuthorizationStatus,
38
+ NotificationAndroidPriority,
39
+ NotificationAndroidVisibility,
40
+ } from './statics';
41
+ import type {
42
+ Messaging,
43
+ Statics,
44
+ RemoteMessage,
45
+ IOSPermissions,
46
+ AuthorizationStatus as AuthorizationStatusType,
47
+ } from './types/messaging';
48
+ import type { ReactNativeFirebase } from '@react-native-firebase/app';
49
+
50
+ const statics: Partial<Statics> = {
51
+ AuthorizationStatus,
52
+ NotificationAndroidPriority,
53
+ NotificationAndroidVisibility,
54
+ };
55
+ const namespace = 'messaging';
56
+
57
+ const nativeModuleName = 'RNFBMessagingModule';
58
+
59
+ let backgroundMessageHandler: ((remoteMessage: RemoteMessage) => Promise<any>) | undefined;
60
+ let openSettingsForNotificationHandler: ((remoteMessage: RemoteMessage) => any) | undefined;
61
+ let playServicesAvailability: any;
62
+
63
+ class FirebaseMessagingModule extends FirebaseModule implements Messaging {
64
+ _isAutoInitEnabled: boolean;
65
+ _isDeliveryMetricsExportToBigQueryEnabled: boolean;
66
+ _isRegisteredForRemoteNotifications: boolean;
67
+ _isNotificationDelegationEnabled: boolean;
68
+
69
+ constructor(
70
+ app: ReactNativeFirebase.FirebaseAppBase,
71
+ config: any,
72
+ customUrlOrRegion?: string | null,
73
+ ) {
74
+ super(app, config, customUrlOrRegion);
75
+ this._isAutoInitEnabled =
76
+ this.native.isAutoInitEnabled != null ? this.native.isAutoInitEnabled : true;
77
+ this._isDeliveryMetricsExportToBigQueryEnabled =
78
+ this.native.isDeliveryMetricsExportToBigQueryEnabled != null
79
+ ? this.native.isDeliveryMetricsExportToBigQueryEnabled
80
+ : false;
81
+ this._isRegisteredForRemoteNotifications =
82
+ this.native.isRegisteredForRemoteNotifications != null
83
+ ? this.native.isRegisteredForRemoteNotifications
84
+ : true;
85
+ this._isNotificationDelegationEnabled =
86
+ this.native.isNotificationDelegationEnabled != null
87
+ ? this.native.isNotificationDelegationEnabled
88
+ : false;
89
+
90
+ AppRegistry.registerHeadlessTask('ReactNativeFirebaseMessagingHeadlessTask', () => {
91
+ if (!backgroundMessageHandler) {
92
+ // eslint-disable-next-line no-console
93
+ console.warn(
94
+ 'No background message handler has been set. Set a handler via the "setBackgroundMessageHandler" method.',
95
+ );
96
+ return () => Promise.resolve();
97
+ }
98
+ return (remoteMessage: RemoteMessage) => backgroundMessageHandler!(remoteMessage);
99
+ });
100
+
101
+ if (isIOS) {
102
+ this.emitter.addListener(
103
+ 'messaging_message_received_background',
104
+ (remoteMessage: RemoteMessage) => {
105
+ if (!backgroundMessageHandler) {
106
+ // eslint-disable-next-line no-console
107
+ console.warn(
108
+ 'No background message handler has been set. Set a handler via the "setBackgroundMessageHandler" method.',
109
+ );
110
+ return Promise.resolve();
111
+ }
112
+
113
+ // Ensure the handler is a promise
114
+ const handlerPromise = Promise.resolve(backgroundMessageHandler(remoteMessage));
115
+ handlerPromise.finally(() => {
116
+ this.native.completeNotificationProcessing();
117
+ });
118
+
119
+ return handlerPromise;
120
+ },
121
+ );
122
+
123
+ this.emitter.addListener(
124
+ 'messaging_settings_for_notification_opened',
125
+ (remoteMessage: RemoteMessage) => {
126
+ if (!openSettingsForNotificationHandler) {
127
+ // eslint-disable-next-line no-console
128
+ console.warn(
129
+ 'No handler for notification settings link has been set. Set a handler via the "setOpenSettingsForNotificationsHandler" method',
130
+ );
131
+
132
+ return Promise.resolve();
133
+ }
134
+
135
+ return openSettingsForNotificationHandler(remoteMessage);
136
+ },
137
+ );
138
+ }
139
+ }
140
+
141
+ get isAutoInitEnabled(): boolean {
142
+ return this._isAutoInitEnabled;
143
+ }
144
+
145
+ /**
146
+ * @ios
147
+ */
148
+ get isDeviceRegisteredForRemoteMessages(): boolean {
149
+ if (isAndroid) {
150
+ return true;
151
+ }
152
+
153
+ return this._isRegisteredForRemoteNotifications;
154
+ }
155
+
156
+ get isNotificationDelegationEnabled(): boolean {
157
+ return this._isNotificationDelegationEnabled;
158
+ }
159
+
160
+ get isDeliveryMetricsExportToBigQueryEnabled(): boolean {
161
+ return this._isDeliveryMetricsExportToBigQueryEnabled;
162
+ }
163
+
164
+ setAutoInitEnabled(enabled: boolean): Promise<void> {
165
+ if (!isBoolean(enabled)) {
166
+ throw new Error(
167
+ "firebase.messaging().setAutoInitEnabled(*) 'enabled' expected a boolean value.",
168
+ );
169
+ }
170
+
171
+ this._isAutoInitEnabled = enabled;
172
+ return this.native.setAutoInitEnabled(enabled);
173
+ }
174
+
175
+ getInitialNotification(): Promise<RemoteMessage | null> {
176
+ return this.native.getInitialNotification().then((value: RemoteMessage | null) => {
177
+ if (value) {
178
+ return value;
179
+ }
180
+ return null;
181
+ });
182
+ }
183
+
184
+ getDidOpenSettingsForNotification(): Promise<boolean> {
185
+ if (!isIOS) return Promise.resolve(false);
186
+ return this.native.getDidOpenSettingsForNotification().then((value: boolean) => value);
187
+ }
188
+
189
+ getIsHeadless(): Promise<boolean> {
190
+ return this.native.getIsHeadless();
191
+ }
192
+
193
+ getToken(options?: { appName?: string; senderId?: string }): Promise<string> {
194
+ if (!isUndefined(options?.appName) && !isString(options?.appName)) {
195
+ throw new Error("firebase.messaging().getToken(*) 'appName' expected a string.");
196
+ }
197
+
198
+ if (!isUndefined(options?.senderId) && !isString(options?.senderId)) {
199
+ throw new Error("firebase.messaging().getToken(*) 'senderId' expected a string.");
200
+ }
201
+
202
+ const appName = options?.appName || this.app.name;
203
+ const senderId = options?.senderId || this.app.options.messagingSenderId;
204
+
205
+ return this.native.getToken(appName, senderId);
206
+ }
207
+
208
+ deleteToken(options?: { appName?: string; senderId?: string }): Promise<void> {
209
+ if (!isUndefined(options?.appName) && !isString(options?.appName)) {
210
+ throw new Error("firebase.messaging().deleteToken(*) 'appName' expected a string.");
211
+ }
212
+
213
+ if (!isUndefined(options?.senderId) && !isString(options?.senderId)) {
214
+ throw new Error("firebase.messaging().deleteToken(*) 'senderId' expected a string.");
215
+ }
216
+
217
+ const appName = options?.appName || this.app.name;
218
+ const senderId = options?.senderId || this.app.options.messagingSenderId;
219
+
220
+ return this.native.deleteToken(appName, senderId);
221
+ }
222
+
223
+ onMessage(listener: (message: RemoteMessage) => any): () => void {
224
+ if (!isFunction(listener)) {
225
+ throw new Error("firebase.messaging().onMessage(*) 'listener' expected a function.");
226
+ }
227
+
228
+ const subscription = this.emitter.addListener('messaging_message_received', listener);
229
+ return () => subscription.remove();
230
+ }
231
+
232
+ onNotificationOpenedApp(listener: (message: RemoteMessage) => any): () => void {
233
+ if (!isFunction(listener)) {
234
+ throw new Error(
235
+ "firebase.messaging().onNotificationOpenedApp(*) 'listener' expected a function.",
236
+ );
237
+ }
238
+
239
+ const subscription = this.emitter.addListener('messaging_notification_opened', listener);
240
+ return () => subscription.remove();
241
+ }
242
+
243
+ onTokenRefresh(listener: (token: string) => any): () => void {
244
+ if (!isFunction(listener)) {
245
+ throw new Error("firebase.messaging().onTokenRefresh(*) 'listener' expected a function.");
246
+ }
247
+
248
+ const subscription = this.emitter.addListener(
249
+ 'messaging_token_refresh',
250
+ (event: { token: string }) => {
251
+ const { token } = event;
252
+ listener(token);
253
+ },
254
+ );
255
+ return () => subscription.remove();
256
+ }
257
+
258
+ /**
259
+ * @platform ios
260
+ */
261
+ requestPermission(permissions?: IOSPermissions): Promise<AuthorizationStatusType> {
262
+ if (isAndroid) {
263
+ return Promise.resolve(AuthorizationStatus.AUTHORIZED);
264
+ }
265
+
266
+ const defaultPermissions: IOSPermissions = {
267
+ alert: true,
268
+ announcement: false,
269
+ badge: true,
270
+ carPlay: true,
271
+ provisional: false,
272
+ sound: true,
273
+ criticalAlert: false,
274
+ providesAppNotificationSettings: false,
275
+ };
276
+
277
+ if (!permissions) {
278
+ return this.native.requestPermission(defaultPermissions);
279
+ }
280
+
281
+ if (!isObject(permissions)) {
282
+ throw new Error('firebase.messaging().requestPermission(*) expected an object value.');
283
+ }
284
+
285
+ Object.entries(permissions).forEach(([key, value]) => {
286
+ if (!hasOwnProperty(defaultPermissions, key)) {
287
+ throw new Error(
288
+ `firebase.messaging().requestPermission(*) unexpected key "${key}" provided to permissions object.`,
289
+ );
290
+ }
291
+
292
+ if (!isBoolean(value)) {
293
+ throw new Error(
294
+ `firebase.messaging().requestPermission(*) the permission "${key}" expected a boolean value.`,
295
+ );
296
+ }
297
+
298
+ (defaultPermissions as any)[key] = value;
299
+ });
300
+
301
+ return this.native.requestPermission(defaultPermissions);
302
+ }
303
+
304
+ registerDeviceForRemoteMessages(): Promise<void> {
305
+ if (isAndroid) {
306
+ return Promise.resolve();
307
+ }
308
+
309
+ const autoRegister = this.firebaseJson['messaging_ios_auto_register_for_remote_messages'];
310
+ if (autoRegister === undefined || autoRegister === true) {
311
+ // eslint-disable-next-line no-console
312
+ console.warn(
313
+ `Usage of "messaging().registerDeviceForRemoteMessages()" is not required. You only need to register if auto-registration is disabled in your 'firebase.json' configuration file via the 'messaging_ios_auto_register_for_remote_messages' property.`,
314
+ );
315
+ }
316
+
317
+ this._isRegisteredForRemoteNotifications = true;
318
+ return this.native.registerForRemoteNotifications();
319
+ }
320
+
321
+ /**
322
+ * @platform ios
323
+ */
324
+ unregisterDeviceForRemoteMessages(): Promise<void> {
325
+ if (isAndroid) {
326
+ return Promise.resolve();
327
+ }
328
+ this._isRegisteredForRemoteNotifications = false;
329
+ return this.native.unregisterForRemoteNotifications();
330
+ }
331
+
332
+ /**
333
+ * @platform ios
334
+ */
335
+ getAPNSToken(): Promise<string | null> {
336
+ if (isAndroid) {
337
+ return Promise.resolve(null);
338
+ }
339
+ return this.native.getAPNSToken();
340
+ }
341
+
342
+ /**
343
+ * @platform ios
344
+ */
345
+ setAPNSToken(token: string, type?: string): Promise<void> {
346
+ if (isUndefined(token) || !isString(token)) {
347
+ throw new Error("firebase.messaging().setAPNSToken(*) 'token' expected a string value.");
348
+ }
349
+
350
+ if (!isUndefined(type) && (!isString(type) || !['prod', 'sandbox', 'unknown'].includes(type))) {
351
+ throw new Error(
352
+ "firebase.messaging().setAPNSToken(*) 'type' expected one of 'prod', 'sandbox', or 'unknown'.",
353
+ );
354
+ }
355
+
356
+ if (isAndroid) {
357
+ return Promise.resolve();
358
+ }
359
+
360
+ return this.native.setAPNSToken(token, type);
361
+ }
362
+
363
+ hasPermission(): Promise<AuthorizationStatusType> {
364
+ return this.native.hasPermission();
365
+ }
366
+
367
+ // https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#public-void-ondeletedmessages-
368
+ onDeletedMessages(listener: () => void): () => void {
369
+ if (!isFunction(listener)) {
370
+ throw new Error("firebase.messaging().onDeletedMessages(*) 'listener' expected a function.");
371
+ }
372
+
373
+ const subscription = this.emitter.addListener('messaging_message_deleted', listener);
374
+ return () => subscription.remove();
375
+ }
376
+
377
+ // https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#onMessageSent(java.lang.String)
378
+ onMessageSent(listener: (messageId: string) => any): () => void {
379
+ if (!isFunction(listener)) {
380
+ throw new Error("firebase.messaging().onMessageSent(*) 'listener' expected a function.");
381
+ }
382
+
383
+ const subscription = this.emitter.addListener('messaging_message_sent', listener);
384
+ return () => {
385
+ subscription.remove();
386
+ };
387
+ }
388
+
389
+ // https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService.html#onSendError(java.lang.String,%20java.lang.Exception)
390
+ onSendError(
391
+ listener: (evt: { messageId: string; error: ReactNativeFirebase.NativeFirebaseError }) => any,
392
+ ): () => void {
393
+ if (!isFunction(listener)) {
394
+ throw new Error("firebase.messaging().onSendError(*) 'listener' expected a function.");
395
+ }
396
+
397
+ const subscription = this.emitter.addListener('messaging_message_send_error', listener);
398
+ return () => subscription.remove();
399
+ }
400
+
401
+ /**
402
+ * Set a handler that will be called when a message is received while the app is in the background.
403
+ * Should be called before the app is registered in `AppRegistry`, for example in `index.js`.
404
+ * An app is considered to be in the background if no active window is displayed.
405
+ * @param handler called with an argument of type messaging.RemoteMessage that must be async and return a Promise
406
+ */
407
+ setBackgroundMessageHandler(handler: (message: RemoteMessage) => Promise<any>): void {
408
+ if (!isFunction(handler)) {
409
+ throw new Error(
410
+ "firebase.messaging().setBackgroundMessageHandler(*) 'handler' expected a function.",
411
+ );
412
+ }
413
+
414
+ backgroundMessageHandler = handler;
415
+ if (isIOS) {
416
+ this.native.signalBackgroundMessageHandlerSet();
417
+ }
418
+ }
419
+
420
+ setOpenSettingsForNotificationsHandler(handler: (message: RemoteMessage) => any): void {
421
+ if (!isIOS) {
422
+ return;
423
+ }
424
+
425
+ if (!isFunction(handler)) {
426
+ throw new Error(
427
+ "firebase.messaging().setOpenSettingsForNotificationsHandler(*) 'handler' expected a function.",
428
+ );
429
+ }
430
+
431
+ openSettingsForNotificationHandler = handler;
432
+ }
433
+
434
+ sendMessage(remoteMessage: RemoteMessage): Promise<void> {
435
+ if (isIOS) {
436
+ throw new Error(`firebase.messaging().sendMessage() is only supported on Android devices.`);
437
+ }
438
+ let options;
439
+ try {
440
+ const senderId = this.app.options.messagingSenderId;
441
+ if (!senderId) {
442
+ throw new Error("'messagingSenderId' is required in Firebase app options.");
443
+ }
444
+ options = remoteMessageOptions(senderId, remoteMessage);
445
+ } catch (e: any) {
446
+ throw new Error(`firebase.messaging().sendMessage(*) ${e.message}.`);
447
+ }
448
+
449
+ return this.native.sendMessage(options);
450
+ }
451
+
452
+ subscribeToTopic(topic: string): Promise<void> {
453
+ if (!isString(topic)) {
454
+ throw new Error("firebase.messaging().subscribeToTopic(*) 'topic' expected a string value.");
455
+ }
456
+
457
+ if (topic.indexOf('/') > -1) {
458
+ throw new Error('firebase.messaging().subscribeToTopic(*) \'topic\' must not include "/".');
459
+ }
460
+
461
+ return this.native.subscribeToTopic(topic);
462
+ }
463
+
464
+ unsubscribeFromTopic(topic: string): Promise<void> {
465
+ if (!isString(topic)) {
466
+ throw new Error(
467
+ "firebase.messaging().unsubscribeFromTopic(*) 'topic' expected a string value.",
468
+ );
469
+ }
470
+
471
+ if (topic.indexOf('/') > -1) {
472
+ throw new Error(
473
+ 'firebase.messaging().unsubscribeFromTopic(*) \'topic\' must not include "/".',
474
+ );
475
+ }
476
+
477
+ return this.native.unsubscribeFromTopic(topic);
478
+ }
479
+
480
+ /**
481
+ * unsupported
482
+ */
483
+
484
+ useServiceWorker(): void {
485
+ // eslint-disable-next-line no-console
486
+ console.warn(
487
+ 'firebase.messaging().useServiceWorker() is not supported on react-native-firebase.',
488
+ );
489
+ }
490
+
491
+ usePublicVapidKey(): void {
492
+ // eslint-disable-next-line no-console
493
+ console.warn(
494
+ 'firebase.messaging().usePublicVapidKey() is not supported on react-native-firebase.',
495
+ );
496
+ }
497
+
498
+ setDeliveryMetricsExportToBigQuery(enabled: boolean): Promise<void> {
499
+ if (!isBoolean(enabled)) {
500
+ throw new Error(
501
+ "firebase.messaging().setDeliveryMetricsExportToBigQuery(*) 'enabled' expected a boolean value.",
502
+ );
503
+ }
504
+
505
+ this._isDeliveryMetricsExportToBigQueryEnabled = enabled;
506
+ return this.native.setDeliveryMetricsExportToBigQuery(enabled);
507
+ }
508
+
509
+ setNotificationDelegationEnabled(enabled: boolean): Promise<void> {
510
+ if (!isBoolean(enabled)) {
511
+ throw new Error(
512
+ "firebase.messaging().setNotificationDelegationEnabled(*) 'enabled' expected a boolean value.",
513
+ );
514
+ }
515
+
516
+ this._isNotificationDelegationEnabled = enabled;
517
+ if (isIOS) {
518
+ return Promise.resolve();
519
+ }
520
+
521
+ return this.native.setNotificationDelegationEnabled(enabled);
522
+ }
523
+
524
+ async isSupported(): Promise<boolean> {
525
+ if (isAndroid) {
526
+ const firebase = getFirebaseRoot();
527
+ const app = this.app;
528
+ // @ts-ignore - secret "app" argument to avoid deprecation warning when getApp() is called under the hood
529
+ playServicesAvailability = firebase.utils(app).playServicesAvailability;
530
+ return playServicesAvailability.isAvailable;
531
+ }
532
+ // Always return "true" for iOS. Web will be implemented when it is supported
533
+ return true;
534
+ }
535
+ }
536
+
537
+ // import { SDK_VERSION } from '@react-native-firebase/messaging';
538
+ export const SDK_VERSION = version;
539
+
540
+ // import messaging from '@react-native-firebase/messaging';
541
+ // messaging().X(...);
542
+ const messagingNamespace = createModuleNamespace({
543
+ statics,
544
+ version,
545
+ namespace,
546
+ nativeModuleName,
547
+ nativeEvents: [
548
+ 'messaging_token_refresh',
549
+ 'messaging_message_sent',
550
+ 'messaging_message_deleted',
551
+ 'messaging_message_received',
552
+ 'messaging_message_send_error',
553
+ 'messaging_notification_opened',
554
+ ...(isIOS
555
+ ? ['messaging_message_received_background', 'messaging_settings_for_notification_opened']
556
+ : []),
557
+ ],
558
+ hasMultiAppSupport: false,
559
+ hasCustomUrlOrRegionSupport: false,
560
+ ModuleClass: FirebaseMessagingModule,
561
+ });
562
+
563
+ type MessagingNamespace = ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<
564
+ Messaging,
565
+ Statics
566
+ > & {
567
+ messaging: ReactNativeFirebase.FirebaseModuleWithStaticsAndApp<Messaging, Statics>;
568
+ firebase: ReactNativeFirebase.Module;
569
+ app(name?: string): ReactNativeFirebase.FirebaseApp;
570
+ };
571
+
572
+ export default messagingNamespace as unknown as MessagingNamespace;
573
+
574
+ // import messaging, { firebase } from '@react-native-firebase/messaging';
575
+ // messaging().X(...);
576
+ // firebase.messaging().X(...);
577
+ export const firebase =
578
+ getFirebaseRoot() as unknown as ReactNativeFirebase.FirebaseNamespacedExport<
579
+ 'messaging',
580
+ Messaging,
581
+ Statics,
582
+ false
583
+ >;
@@ -0,0 +1,110 @@
1
+ /*
2
+ * Copyright (c) 2016-present Invertase Limited & Contributors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this library except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ */
17
+
18
+ import {
19
+ generateFirestoreId,
20
+ hasOwnProperty,
21
+ isInteger,
22
+ isNumber,
23
+ isObject,
24
+ isString,
25
+ isUndefined,
26
+ } from '@react-native-firebase/app/lib/common';
27
+ import type { RemoteMessage } from './types/messaging';
28
+
29
+ interface RemoteMessageOptions {
30
+ to: string;
31
+ messageId: string;
32
+ ttl: number;
33
+ data: { [key: string]: string };
34
+ collapseKey?: string;
35
+ messageType?: string;
36
+ }
37
+
38
+ export default function remoteMessageOptions(
39
+ messagingSenderId: string,
40
+ remoteMessage: RemoteMessage,
41
+ ): RemoteMessageOptions {
42
+ const out: Partial<RemoteMessageOptions> = {};
43
+
44
+ if (isUndefined(remoteMessage) || !isObject(remoteMessage)) {
45
+ throw new Error("'remoteMessage' expected an object value");
46
+ }
47
+
48
+ if (!remoteMessage.to) {
49
+ out.to = `${messagingSenderId}@fcm.googleapis.com`;
50
+ } else if (!isString(remoteMessage.to)) {
51
+ throw new Error("'remoteMessage.to' expected a string value");
52
+ } else {
53
+ out.to = remoteMessage.to;
54
+ }
55
+
56
+ if (!remoteMessage.messageId) {
57
+ out.messageId = generateFirestoreId();
58
+ } else if (!isString(remoteMessage.messageId)) {
59
+ throw new Error("'remoteMessage.messageId' expected a string value");
60
+ } else {
61
+ out.messageId = remoteMessage.messageId;
62
+ }
63
+
64
+ if (!hasOwnProperty(remoteMessage, 'ttl')) {
65
+ out.ttl = 3600;
66
+ } else {
67
+ if (!isNumber(remoteMessage.ttl)) {
68
+ throw new Error("'remoteMessage.ttl' expected a number value");
69
+ }
70
+ if (remoteMessage.ttl! < 0 || !isInteger(remoteMessage.ttl!)) {
71
+ throw new Error("'remoteMessage.ttl' expected a positive integer value");
72
+ }
73
+ out.ttl = remoteMessage.ttl!;
74
+ }
75
+
76
+ if (!remoteMessage.data) {
77
+ out.data = {};
78
+ } else if (!isObject(remoteMessage.data)) {
79
+ throw new Error("'remoteMessage.data' expected an object value");
80
+ } else {
81
+ // Serialize all objects to strings
82
+ out.data = {};
83
+ for (const key in remoteMessage.data) {
84
+ if (hasOwnProperty(remoteMessage.data, key)) {
85
+ const value = remoteMessage.data[key];
86
+ if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
87
+ out.data[key] = JSON.stringify(value);
88
+ } else {
89
+ out.data[key] = String(value);
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+ if (remoteMessage.collapseKey) {
96
+ if (!isString(remoteMessage.collapseKey)) {
97
+ throw new Error("'remoteMessage.collapseKey' expected a string value");
98
+ }
99
+ out.collapseKey = remoteMessage.collapseKey;
100
+ }
101
+
102
+ if (remoteMessage.messageType) {
103
+ if (!isString(remoteMessage.messageType)) {
104
+ throw new Error("'remoteMessage.messageType' expected a string value");
105
+ }
106
+ out.messageType = remoteMessage.messageType;
107
+ }
108
+
109
+ return out as RemoteMessageOptions;
110
+ }