All Projects → lookinlab → adonis-fcm

lookinlab / adonis-fcm

Licence: MIT license
Firebase Cloud Messaging for AdonisJS

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to adonis-fcm

Adonis Bumblebee
Api Transformer for AdonisJs Framework
Stars: ✭ 125 (+557.89%)
Mutual labels:  adonisjs
create-adonis-ts-app
Boilerplate to create a new AdonisJs typescript project
Stars: ✭ 94 (+394.74%)
Mutual labels:  adonisjs
fuzzy-c-means
Fuzzy c-means Clustering
Stars: ✭ 34 (+78.95%)
Mutual labels:  fcm
Leaguestats
📈 League of Legends Stats Web App
Stars: ✭ 172 (+805.26%)
Mutual labels:  adonisjs
fcm
Golang client library for Firebase Cloud Messaging.
Stars: ✭ 22 (+15.79%)
Mutual labels:  fcm
PushNotifications
Push Notification using Embarcadero Rad Studio Tokyo 10.2.3 on Android and Apple Devices written in C++ and Delphi
Stars: ✭ 12 (-36.84%)
Mutual labels:  fcm
Adonis Lucid Filter
Addon for filtering AdonisJS Lucid ORM
Stars: ✭ 67 (+252.63%)
Mutual labels:  adonisjs
gerar-boletos
Biblioteca em Node.js para geração de boletos utilizando PDFKit.
Stars: ✭ 81 (+326.32%)
Mutual labels:  adonisjs
FCM-OnDeviceNotificationScheduler
Demo implementation to Schedule FCM Notifications on Android Device using AlarmManager + WorkManager.
Stars: ✭ 111 (+484.21%)
Mutual labels:  fcm
saas-react-starter-kit-boilerplate
SaaStr is a React SaaS boilerplate to kickstart your new SaaS adventure as fast as possible. Built on top of Adonis JS for the BackEnd and React Starter Kit for the Front-End
Stars: ✭ 100 (+426.32%)
Mutual labels:  adonisjs
Adonis Acl
demo app: https://github.com/enniel/adonis-acl-blog-demo
Stars: ✭ 195 (+926.32%)
Mutual labels:  adonisjs
Moviepark
A Nuxt universal app with an Adonis 5 api server using the TMDb API for its movie catalog.
Stars: ✭ 32 (+68.42%)
Mutual labels:  adonisjs
OneSignal-Ionic-Sample
No description or website provided.
Stars: ✭ 85 (+347.37%)
Mutual labels:  fcm
Adonisjs Hackathon Starter
A boilerplate for AdonisJS web framework
Stars: ✭ 142 (+647.37%)
Mutual labels:  adonisjs
fcmpush
Firebase Cloud Messaging API wrapper for Ruby, suppot HTTP v1 API including access_token auto refresh feature.
Stars: ✭ 44 (+131.58%)
Mutual labels:  fcm
Adonis Adminify
Admin dashboard based on AdonisJs + Adminify (based on vuetify)
Stars: ✭ 90 (+373.68%)
Mutual labels:  adonisjs
FcmNotificationHandler
Android library that helps to construct and launch system tray notifications from FCM Notification messages received when the app is in foreground mimicking the default format and behavior applied when the app is background.
Stars: ✭ 18 (-5.26%)
Mutual labels:  fcm
adonis-lucid-soft-deletes
Addon for soft deletes AdonisJS Lucid ORM
Stars: ✭ 55 (+189.47%)
Mutual labels:  adonisjs
epns
📱 Erlang Push Notifications. APNS(Apple Push Notifications) and FCM(Firebase Cloud Messaging) Push Notifications
Stars: ✭ 13 (-31.58%)
Mutual labels:  fcm
gavn
Invoice System for Freelancers as a Single Page Application built with AdonisJs and Vue.js
Stars: ✭ 54 (+184.21%)
Mutual labels:  adonisjs

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();
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].