Git Product home page Git Product logo

adonis-fcm's Introduction

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

adonis-fcm's People

Contributors

greenkeeper[bot] avatar lookingit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

adonis-fcm's Issues

Doesn't seem to work with Adonis v5

I've followed the instructions and made sure the tsconfig is updated and the adonisrc file, but I can't seem to get VSCode to understand that it's there? Is this just a Code problem or is something wrong with my setup?

Edit: So some more information, it just keeps saying it can't find the module @ioc:Adonis/Addons/FCM

this is so cool

Do we have practical example on how to implement FCM ie using Vue js?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.