Home

Awesome

Adonis FCM (Firebase Cloud Messaging)

Works with AdonisJS v5

Version for AdonisJS v4

Greenkeeper badge Build Status

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

Installation

Make sure to install it using npm or yarn.

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

# yarn
yarn add adonis-fcm@alpha
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

"providers": [
  "...other packages",
  "adonis-fcm"
]
...
FCM_API_KEY=YOUR_API_KEY
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.post()
    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

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

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();
  }
}