|
| 1 | +import {inject, Provider} from '@loopback/core'; |
| 2 | +import {HttpErrors} from '@loopback/rest'; |
| 3 | +import * as admin from 'firebase-admin'; |
| 4 | +import {FcmBindings} from './keys'; |
| 5 | +import { |
| 6 | + FcmConfig, |
| 7 | + FcmMessage, |
| 8 | + FcmNotification, |
| 9 | + FcmSubscriberType, |
| 10 | +} from './types'; |
| 11 | + |
| 12 | +export class FcmProvider implements Provider<FcmNotification> { |
| 13 | + constructor( |
| 14 | + @inject(FcmBindings.Config, { |
| 15 | + optional: true, |
| 16 | + }) |
| 17 | + private readonly fcmConfig?: FcmConfig, |
| 18 | + ) { |
| 19 | + if (this.fcmConfig) { |
| 20 | + this.fcmService = admin.initializeApp({ |
| 21 | + credential: admin.credential.cert(this.fcmConfig.serviceAccountPath), |
| 22 | + databaseURL: this.fcmConfig.dbUrl, |
| 23 | + }); |
| 24 | + } else { |
| 25 | + throw new HttpErrors.PreconditionFailed('Firebase Config missing !'); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + fcmService: admin.app.App; |
| 30 | + |
| 31 | + initialValidations(message: FcmMessage) { |
| 32 | + if ( |
| 33 | + message.receiver.to.length === 0 && |
| 34 | + !message.options.topic && |
| 35 | + !message.options.condition |
| 36 | + ) { |
| 37 | + throw new HttpErrors.BadRequest( |
| 38 | + 'Message receiver, topic or condition not found in request !', |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (message.receiver.to.length > 500) { |
| 43 | + throw new HttpErrors.BadRequest( |
| 44 | + 'Message receiver count cannot exceed 500 !', |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (!message.subject) { |
| 49 | + throw new HttpErrors.BadRequest('Message title not found !'); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + sendingPushToReceiverTokens( |
| 54 | + message: FcmMessage, |
| 55 | + generalMessageObj: { |
| 56 | + notification: admin.messaging.Notification; |
| 57 | + android?: admin.messaging.AndroidConfig; |
| 58 | + webpush?: admin.messaging.WebpushConfig; |
| 59 | + apns?: admin.messaging.ApnsConfig; |
| 60 | + fcmOptions?: admin.messaging.FcmOptions; |
| 61 | + }, |
| 62 | + ) { |
| 63 | + const promises: Promise<string | admin.messaging.BatchResponse>[] = []; |
| 64 | + /**Partial<admin.messaging.MulticastMessage> |
| 65 | + * These are the registration tokens for all devices which this message |
| 66 | + * is intended for. |
| 67 | + * |
| 68 | + * If receiver does not hold information for type, then it is considered |
| 69 | + * as devce token. |
| 70 | + */ |
| 71 | + const receiverTokens = message.receiver.to.filter( |
| 72 | + item => item.type === FcmSubscriberType.RegistrationToken || !item.type, |
| 73 | + ); |
| 74 | + |
| 75 | + /** |
| 76 | + * if the receivers are of type |
| 77 | + * */ |
| 78 | + if (receiverTokens.length >= 1) { |
| 79 | + const tokens = receiverTokens.map(item => item.id); |
| 80 | + let msgToTransfer = { |
| 81 | + tokens: tokens, |
| 82 | + ...generalMessageObj, |
| 83 | + data: {...message.options.data}, |
| 84 | + }; |
| 85 | + promises.push( |
| 86 | + this.fcmService |
| 87 | + .messaging() |
| 88 | + .sendMulticast(msgToTransfer, (message.options.dryRun = false)), |
| 89 | + ); |
| 90 | + } |
| 91 | + return promises; |
| 92 | + } |
| 93 | + |
| 94 | + sendingPushToTopics( |
| 95 | + message: FcmMessage, |
| 96 | + generalMessageObj: { |
| 97 | + notification: admin.messaging.Notification; |
| 98 | + android?: admin.messaging.AndroidConfig; |
| 99 | + webpush?: admin.messaging.WebpushConfig; |
| 100 | + apns?: admin.messaging.ApnsConfig; |
| 101 | + fcmOptions?: admin.messaging.FcmOptions; |
| 102 | + }, |
| 103 | + ) { |
| 104 | + const promises: Promise<string | admin.messaging.BatchResponse>[] = []; |
| 105 | + const topics = message.receiver.to.filter( |
| 106 | + item => item.type === FcmSubscriberType.FCMTopic, |
| 107 | + ); |
| 108 | + |
| 109 | + if (topics.length > 0) { |
| 110 | + // Messages to multiple Topics is not allowed in single transaction. |
| 111 | + |
| 112 | + topics.forEach(topic => { |
| 113 | + let msgToTransfer = { |
| 114 | + topic: topic.id, |
| 115 | + ...generalMessageObj, |
| 116 | + data: {...message.options.data}, |
| 117 | + }; |
| 118 | + |
| 119 | + promises.push( |
| 120 | + this.fcmService |
| 121 | + .messaging() |
| 122 | + .send(msgToTransfer, (message.options.dryRun = false)), |
| 123 | + ); |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + return promises; |
| 128 | + } |
| 129 | + |
| 130 | + sendingPushToConditions( |
| 131 | + message: FcmMessage, |
| 132 | + generalMessageObj: { |
| 133 | + notification: admin.messaging.Notification; |
| 134 | + android?: admin.messaging.AndroidConfig; |
| 135 | + webpush?: admin.messaging.WebpushConfig; |
| 136 | + apns?: admin.messaging.ApnsConfig; |
| 137 | + fcmOptions?: admin.messaging.FcmOptions; |
| 138 | + }, |
| 139 | + ) { |
| 140 | + const promises: Promise<string | admin.messaging.BatchResponse>[] = []; |
| 141 | + const conditions = message.receiver.to.filter( |
| 142 | + item => item.type === FcmSubscriberType.FCMCondition, |
| 143 | + ); |
| 144 | + |
| 145 | + if (conditions.length > 0) { |
| 146 | + // Condition message |
| 147 | + |
| 148 | + conditions.forEach(condition => { |
| 149 | + let msgToTransfer = { |
| 150 | + condition: condition.id, |
| 151 | + ...generalMessageObj, |
| 152 | + data: {...message.options.data}, |
| 153 | + }; |
| 154 | + promises.push( |
| 155 | + this.fcmService |
| 156 | + .messaging() |
| 157 | + .send(msgToTransfer, (message.options.dryRun = false)), |
| 158 | + ); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + return promises; |
| 163 | + } |
| 164 | + |
| 165 | + value() { |
| 166 | + return { |
| 167 | + publish: async (message: FcmMessage) => { |
| 168 | + /** |
| 169 | + * validating the initial request |
| 170 | + */ |
| 171 | + this.initialValidations(message); |
| 172 | + |
| 173 | + /** |
| 174 | + * This method is responsible to send all the required data to mobile application |
| 175 | + * The mobile device will recieve push notification. |
| 176 | + * Push will be sent to the devices with registration token sent in receiver |
| 177 | + * Notification object holds title, body and imageUrl |
| 178 | + * FCM message must contain 2 attributes, i.e title and body |
| 179 | + * |
| 180 | + */ |
| 181 | + |
| 182 | + const promises: Promise<string | admin.messaging.BatchResponse>[] = []; |
| 183 | + |
| 184 | + const standardNotifForFCM: admin.messaging.Notification = { |
| 185 | + body: message.body, |
| 186 | + title: message.subject, |
| 187 | + imageUrl: message.options.imageUrl, |
| 188 | + }; |
| 189 | + |
| 190 | + /** |
| 191 | + * Message attributes for all kinds of messages |
| 192 | + * |
| 193 | + * If android configurations are sent in options, it will take the |
| 194 | + * precedence over normal notification |
| 195 | + * |
| 196 | + */ |
| 197 | + let generalMessageObj = { |
| 198 | + notification: standardNotifForFCM, |
| 199 | + android: message.options.android, |
| 200 | + webpush: message.options.webpush, |
| 201 | + apns: message.options.apns, |
| 202 | + fcmOptions: message.options.fcmOptions, |
| 203 | + }; |
| 204 | + |
| 205 | + /** |
| 206 | + * Sending messages for all the tokens in the request |
| 207 | + */ |
| 208 | + promises.push( |
| 209 | + ...this.sendingPushToReceiverTokens(message, generalMessageObj), |
| 210 | + ); |
| 211 | + |
| 212 | + /** |
| 213 | + * Sending messages for all the topics in the request |
| 214 | + */ |
| 215 | + promises.push(...this.sendingPushToTopics(message, generalMessageObj)); |
| 216 | + |
| 217 | + /** |
| 218 | + * Sending messages for all the conditions in the request |
| 219 | + */ |
| 220 | + promises.push( |
| 221 | + ...this.sendingPushToConditions(message, generalMessageObj), |
| 222 | + ); |
| 223 | + |
| 224 | + await Promise.all(promises); |
| 225 | + }, |
| 226 | + }; |
| 227 | + } |
| 228 | +} |
0 commit comments