├── CHANGELOG.md ├── src ├── fcm.constants.ts ├── index.ts ├── interfaces │ └── fcm-options.interface.ts ├── fcm.module.ts └── services │ └── fcm.service.ts ├── .prettierrc ├── nest-cli.json ├── tsconfig.build.json ├── tsconfig.json ├── tslint.json ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md └── package.json /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | RELEASE 0.1.0 2 | -------------------------------------------------------------------------------- /src/fcm.constants.ts: -------------------------------------------------------------------------------- 1 | export const FCM_OPTIONS = 'FcmOptions'; 2 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './fcm.module'; 2 | 3 | //services 4 | export * from './services/fcm.service'; 5 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "language": "ts", 3 | "collection": "@nestjs/schematics", 4 | "sourceRoot": "src" 5 | } 6 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /src/interfaces/fcm-options.interface.ts: -------------------------------------------------------------------------------- 1 | import { Logger } from '@nestjs/common'; 2 | 3 | export interface FcmOptions { 4 | firebaseSpecsPath: string; 5 | logger?: any; 6 | } 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "target": "es6", 9 | "sourceMap": false, 10 | "outDir": "./dist", 11 | "rootDir": "./src", 12 | "baseUrl": "./", 13 | "noLib": false 14 | }, 15 | "include": ["src/**/*.ts"], 16 | "exclude": ["node_modules"] 17 | } 18 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": { 5 | "no-unused-expression": true 6 | }, 7 | "rules": { 8 | "quotemark": [true, "single"], 9 | "member-access": [false], 10 | "ordered-imports": [false], 11 | "max-line-length": [true, 150], 12 | "member-ordering": [false], 13 | "interface-name": [false], 14 | "arrow-parens": false, 15 | "object-literal-sort-keys": false 16 | }, 17 | "rulesDirectory": [] 18 | } 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. [Fork it](https://help.github.com/articles/fork-a-repo/) 4 | 2. Install dependencies (`npm install`) 5 | 3. Create your feature branch (`git checkout -b my-new-feature`) 6 | 4. Commit your changes (`git commit -am 'Added some feature'`) 7 | 5. Test your changes (`npm test`) 8 | 6. Push to the branch (`git push origin my-new-feature`) 9 | 7. [Create new Pull Request](https://help.github.com/articles/creating-a-pull-request/) 10 | 11 | ## Testing 12 | 13 | We use [Jest](https://github.com/facebook/jest) to write tests. Run our test suite with this command: 14 | 15 | ``` 16 | npm test 17 | ``` 18 | 19 | ## Code Style 20 | 21 | We use [Prettier](https://prettier.io/) and tslint to maintain code style and best practices. 22 | Please make sure your PR adheres to the guides by running: 23 | 24 | ``` 25 | npm run format 26 | ``` 27 | 28 | and 29 | ``` 30 | npm run lint 31 | ``` -------------------------------------------------------------------------------- /src/fcm.module.ts: -------------------------------------------------------------------------------- 1 | import { Global, Module, DynamicModule, Logger } from '@nestjs/common'; 2 | import { FcmOptions } from './interfaces/fcm-options.interface'; 3 | import { FcmService } from './services/fcm.service'; 4 | import { FCM_OPTIONS } from './fcm.constants'; 5 | import { ValueProvider } from '@nestjs/common/interfaces/modules/provider.interface'; 6 | 7 | @Global() 8 | @Module({}) 9 | export class FcmModule { 10 | static forRoot(options: FcmOptions): DynamicModule { 11 | const optionsProvider: ValueProvider = { 12 | provide: FCM_OPTIONS, 13 | useValue: options, 14 | }; 15 | const logger = options.logger ? options.logger : new Logger('FcmService'); 16 | return { 17 | module: FcmModule, 18 | providers: [ 19 | { provide: Logger, useValue: logger }, 20 | FcmService, 21 | optionsProvider, 22 | ], 23 | exports: [FcmService], 24 | }; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Razvan Costianu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |
4 | 5 | Nest Logo 6 | 7 |
8 | 9 |

NestJS FCM push notifications module

10 | 11 |
12 | 13 | Built with NestJS 14 | 15 |
16 | 17 | ### Installation 18 | 19 | ```bash 20 | npm install --save nestjs-fcm 21 | ``` 22 | 23 | ### FcmModule 24 | 25 | To user FcmService you must add the module first. **The `FcmModule` has a `@Global()` attribute so you should only import it once**. 26 | 27 | ```typescript 28 | import { Module } from '@nestjs/common'; 29 | import * as path from 'path'; 30 | import { FcmModule } from 'nestjs-fcm'; 31 | 32 | @Module({ 33 | imports: [ 34 | FcmModule.forRoot({ 35 | firebaseSpecsPath: path.join(__dirname, '../firebase.spec.json'), 36 | }), 37 | ], 38 | controllers: [], 39 | }) 40 | export class AppModule {} 41 | ``` 42 | 43 | ### `FcmService` use firebase.spec.json file to send notifications using firebase-admin dependency. 44 | 45 | ```typescript 46 | @Injectable() 47 | export class SampleService { 48 | constructor(private readonly fcmService: FcmService) {} 49 | 50 | async doStuff() { 51 | await this.fcmService.sendNotification([ 52 | 'device_id_1', 53 | 'device_id_2', 54 | ] 55 | payload, 56 | silent, 57 | ]); 58 | } 59 | } 60 | ``` 61 | 62 | ## Change Log 63 | 64 | See [Changelog](CHANGELOG.md) for more information. 65 | 66 | ## Contributing 67 | 68 | Contributions welcome! See [Contributing](CONTRIBUTING.md). 69 | 70 | ## Author 71 | 72 | **Razvan Costianu** 73 | 74 | ## License 75 | 76 | Licensed under the MIT License - see the [LICENSE](LICENSE) file for details. 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nestjs-fcm", 3 | "version": "0.1.0", 4 | "description": "NestJS npm package starter", 5 | "author": "Razvan Costianu ", 6 | "license": "MIT", 7 | "readmeFilename": "README.md", 8 | "main": "dist/index.js", 9 | "files": [ 10 | "dist/**/*", 11 | "*.md" 12 | ], 13 | "scripts": { 14 | "start:dev": "tsc -w", 15 | "build": "tsc", 16 | "prepare": "npm run build", 17 | "format": "prettier --write \"src/**/*.ts\"", 18 | "lint": "tslint -p tsconfig.json -c tslint.json", 19 | "test": "jest", 20 | "test:watch": "jest --watch", 21 | "test:cov": "jest --coverage", 22 | "test:e2e": "jest --config ./test/jest-e2e.json" 23 | }, 24 | "keywords": [ 25 | "nestjs" 26 | ], 27 | "publishConfig": { 28 | "access": "public" 29 | }, 30 | "repository": { 31 | "type": "git", 32 | "url": "https://github.com/CostianuRazvan/nestjs-fcm" 33 | }, 34 | "bugs": "https://github.com/CostianuRazvan/nestjs-fcm", 35 | "peerDependencies": { 36 | "@nestjs/common": "^7.6.12" 37 | }, 38 | "dependencies": { 39 | "firebase-admin": "^9.5.0", 40 | "reflect-metadata": "^0.1.13", 41 | "rxjs": "^6.5.5" 42 | }, 43 | "devDependencies": { 44 | "@nestjs/common": "7.6.12", 45 | "@nestjs/core": "^6.0.0", 46 | "@nestjs/platform-express": "^6.0.0", 47 | "@nestjs/testing": "6.1.1", 48 | "@types/express": "4.16.1", 49 | "@types/jest": "24.0.11", 50 | "@types/node": "11.13.4", 51 | "@types/supertest": "2.0.7", 52 | "jest": "24.7.1", 53 | "prettier": "1.17.0", 54 | "supertest": "4.0.2", 55 | "ts-jest": "^26.5.1", 56 | "ts-node": "8.1.0", 57 | "tsc-watch": "2.2.1", 58 | "tsconfig-paths": "3.8.0", 59 | "tslint": "5.16.0", 60 | "typescript": "^3.7" 61 | }, 62 | "jest": { 63 | "moduleFileExtensions": [ 64 | "js", 65 | "json", 66 | "ts" 67 | ], 68 | "rootDir": "src", 69 | "testRegex": ".spec.ts$", 70 | "transform": { 71 | "^.+\\.(t|j)s$": "ts-jest" 72 | }, 73 | "coverageDirectory": "../coverage", 74 | "testEnvironment": "node" 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/services/fcm.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common/decorators/core/injectable.decorator'; 2 | import { Inject, Logger } from '@nestjs/common'; 3 | import { FCM_OPTIONS } from '../fcm.constants'; 4 | import { FcmOptions } from '../interfaces/fcm-options.interface'; 5 | import * as firebaseAdmin from 'firebase-admin'; 6 | 7 | @Injectable() 8 | export class FcmService { 9 | constructor( 10 | @Inject(FCM_OPTIONS) private fcmOptionsProvider: FcmOptions, 11 | private readonly logger: Logger, 12 | ) {} 13 | 14 | async sendNotification( 15 | deviceIds: Array, 16 | payload: firebaseAdmin.messaging.MessagingPayload, 17 | silent: boolean, 18 | imageUrl?: string 19 | ) { 20 | if (deviceIds.length == 0) { 21 | throw new Error('You provide an empty device ids list!'); 22 | } 23 | 24 | if (firebaseAdmin.apps.length === 0) { 25 | firebaseAdmin.initializeApp({ 26 | credential: firebaseAdmin.credential.cert( 27 | this.fcmOptionsProvider.firebaseSpecsPath, 28 | ), 29 | }); 30 | } 31 | 32 | const body: firebaseAdmin.messaging.MulticastMessage = { 33 | tokens: deviceIds, 34 | data: payload?.data, 35 | notification: { 36 | title: payload?.notification?.title, 37 | body: payload?.notification?.body, 38 | imageUrl 39 | }, 40 | apns: { 41 | payload: { 42 | aps: { 43 | sound: payload?.notification?.sound, 44 | contentAvailable: silent ? true : false, 45 | mutableContent: true 46 | } 47 | }, 48 | fcmOptions: { 49 | imageUrl 50 | } 51 | }, 52 | android: { 53 | priority: 'high', 54 | ttl: 60 * 60 * 24, 55 | notification: { 56 | sound: payload?.notification?.sound 57 | } 58 | } 59 | } 60 | 61 | let result = null 62 | let failureCount = 0 63 | let successCount = 0 64 | const failedDeviceIds = [] 65 | 66 | while(deviceIds.length) { 67 | try { 68 | result = await firebaseAdmin 69 | .messaging() 70 | .sendMulticast({...body, tokens: deviceIds.splice(0,500)}, false) 71 | if (result.failureCount > 0) { 72 | const failedTokens = []; 73 | result.responses.forEach((resp, id) => { 74 | if (!resp.success) { 75 | failedTokens.push(deviceIds[id]); 76 | } 77 | }); 78 | failedDeviceIds.push(...failedTokens) 79 | } 80 | failureCount += result.failureCount; 81 | successCount += result.successCount; 82 | } catch (error) { 83 | this.logger.error(error.message, error.stackTrace, 'nestjs-fcm'); 84 | throw error; 85 | } 86 | 87 | } 88 | return {failureCount, successCount, failedDeviceIds}; 89 | } 90 | } 91 | --------------------------------------------------------------------------------