Last active
April 20, 2022 01:19
-
-
Save mattbajorek/d42252f41b097ecb5060d4ea69a8fa49 to your computer and use it in GitHub Desktop.
sendFirebaseMessages method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { mapLimit } from 'async'; | |
import * as firebase from 'firebase-admin'; | |
import { BatchResponse } from 'firebase-admin/lib/messaging/messaging-api'; | |
import { chunk } from 'lodash'; | |
import * as shell from 'shelljs'; | |
export interface ISendFirebaseMessages { | |
token: string; | |
title?: string; | |
message: string; | |
} | |
@Injectable() | |
export class NotificationsService { | |
... | |
public async sendFirebaseMessages(firebaseMessages: ISendFirebaseMessages[], dryRun?: boolean): Promise<BatchResponse> { | |
const batchedFirebaseMessages = chunk(firebaseMessages, 500); | |
const batchResponses = await mapLimit<ISendFirebaseMessages[], BatchResponse>( | |
batchedFirebaseMessages, | |
process.env.FIREBASE_PARALLEL_LIMIT, // 3 is a good place to start | |
async (groupedFirebaseMessages: ISendFirebaseMessages[]): Promise<BatchResponse> => { | |
try { | |
const tokenMessages: firebase.messaging.TokenMessage[] = groupedFirebaseMessages.map(({ message, title, token }) => ({ | |
notification: { body: message, title }, | |
token, | |
apns: { | |
payload: { | |
aps: { | |
'content-available': 1, | |
}, | |
}, | |
}, | |
})); | |
return await this.sendAll(tokenMessages, dryRun); | |
} catch (error) { | |
return { | |
responses: groupedFirebaseMessages.map(() => ({ | |
success: false, | |
error, | |
})), | |
successCount: 0, | |
failureCount: groupedFirebaseMessages.length, | |
}; | |
} | |
}, | |
); | |
return batchResponses.reduce( | |
({ responses, successCount, failureCount }, currentResponse) => { | |
return { | |
responses: responses.concat(currentResponse.responses), | |
successCount: successCount + currentResponse.successCount, | |
failureCount: failureCount + currentResponse.failureCount, | |
}; | |
}, | |
({ | |
responses: [], | |
successCount: 0, | |
failureCount: 0, | |
} as unknown) as BatchResponse, | |
); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment