adonis-fcm
npm i adonis-fcm

adonis-fcm

Firebase Cloud Messaging for AdonisJS

by lookinlab

1.0.1 (see all)License:MITTypeScript:Built-In
npm i adonis-fcm
Readme

Adonis FCM (Firebase Cloud Messaging)

npm-image license-image typescript-image

This wrapper for send messages to Firebase Cloud Messaging with help node-gcm

Works with AdonisJS v5

Docs for AdonisJS v4

Installation

Make sure to install it using npm or yarn.

# npm
npm i adonis-fcm
node ace invoke adonis-fcm

# yarn
yarn add adonis-fcm
node ace invoke adonis-fcm

How to use

Step 1: Get API key

You need add your app and to receive API key into Firebase Console

Step 2: Initialization

  • Make sure to register the provider inside .adonisrc.json file.
"providers": [
  "...other packages",
  "adonis-fcm"
]
  • Add a variables to .env file of project.
...
FCM_API_KEY=YOUR_API_KEY
  • Set options in config/fcm.ts.
import Env from '@ioc:Adonis/Core/Env'
import { FCMConfig } from '@ioc:Adonis/Addons/FCM'

const fcmConfig: FCMConfig = {
  apiKey: Env.get('FCM_API_KEY'),
  requestOptions: {
    // proxy: 'http://127.0.0.1'
    // timeout: 5000
  }
}
export default fcmConfig

Step 3: Use service into controllers/routes/listeners

Example:

import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Notification from 'App/Models/Notification'
import Event from '@ioc:Adonis/Core/Event'

export default class NotificationController {
  // ...
  async store({ request }: HttpContextContract) {
    const { title, text } = request.body()
    const notification = await Notification.create({ title, text })

    Event.emit('notification::created', notification)
    return notification
  }
}

Add an event to start/events.ts file.

import Event from '@ioc:Adonis/Core/Event'
Event.on('notification::created', 'Notification.created');

Create a listener:

node ace make:listener Notification

Add a listener to app/Listeners/Notification.ts file.

import { EventsList } from '@ioc:Adonis/Core/Event'
import FCM from '@ioc:Adonis/Addons/FCM'

export default class NotificationListener {
  public async created(notification: EventsList['notification::created']) {
    const { title, text } = notification.toJSON();
    
    const recipients = { condition: "'notifications' in topics" }; // or { registrationTokens: [...] }
    const response = await FCM.send({ notification: { title, body: text }}, recipients);
  }
}

Methods

FCM.message(params) - return a Message instance

FCM.send(message, options, numberOfTimes) - return a Promise

  • message: {Object|Message}
  • options: {Object} - Docs

FCM.sendNoRetry(message, options) - return a Promise

  • message: {Object|Message}
  • options: {Object} - Docs

Answers:

Q: Where get a registration tokens?

The registration token is sent by the user when he uses your application. You need save token to use it later

Q: I need to remove all "bad" token from my database, how do I do that?

For example like this:

import { EventsList } from '@ioc:Adonis/Core/Event'
import FCM from '@ioc:Adonis/Addons/FCM'
import Device from 'App/Models/Device'

export default class NotificationListener {
  public async created(notification: EventsList['notification::created']) {
    const { title, text } = notification.toJSON();

    const devices = await Device.all();
    const registrationTokens = devices.toJSON().map(device => device.token);
    
    const recipients = { registrationTokens };
    const response = await FCM.send({ notification: { title, body: text }}, recipients);
    
    const badTokens = registrationTokens.filter((token, i) => response[i].error !== null);
    await Device.query().whereIn('token', badTokens).delete();
  }
}

Downloads/wk

220

GitHub Stars

17

LAST COMMIT

1yr ago

MAINTAINERS

1

CONTRIBUTORS

1

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
1.0.1
latest
1yr ago
0.1.2
legacy
1yr ago
1.0.1-rc1
alpha
2yrs ago
No alternatives found
No tutorials found
Add a tutorial