Skip to content

enniel/adonis-notifications

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adonis Notifications

A provider for easy sending notifications (Inspired Laravel Notifications)

Build Status Coverage Status

Installation

  1. Add package:
$ npm i adonis-notifications --save

or

$ yarn add adonis-notifications
  1. Register providers inside the your start/app.js file.
const providers = [
  ...
  'adonis-notifications/providers/NotificationsProvider',
  ...
]
const aceProviders = [
  ...
  'adonis-notifications/providers/CommandsProvider',
  ...
]
  1. Notifications table
./ace run notifications:setup

Examples

// app/Model/User.js

...
class User extends Lucid {
  static get traits () {
    return [
      '@provider:Morphable',
      '@provider:HasDatabaseNotifications',
      '@provider:Notifiable'
    ]
  }
}
...

This package used adonis-lucid-polymorphic for database channel.

// app/Notifications/TestNotification.js

...
class TestNotification {
  static get type () {
    return 'test'
  }

  via () {
    return ['database']
  }

  toJSON () {
    return {
      foo: 'bar'
    }
  }
}
...
// app/Http/routes.js

const Notifications = use('Notifications')

...

  // from model instance
  const user = await User.find(1)
  await user.notify(user, new TestNotification())
  // to one user
  const user = await User.find(1)
  await Notifications.send(user, new TestNotification())
  // to many users
  const users = await User.query().fetch()
  await Notifications.send(users, new TestNotification())

...

Custom Channels

// app/providers/YourProvider.js

...

boot () {
  const NotificationManager = this.app.use('Notifications')
  NotificationManager.extend('custom', () => {
    return new CustomChannel()
  })
}

...

On-Demand Notifications

const FcmMessage = use('FcmMessage')
const Notifications = use('Notifications')

class PushTestNotification {
  constructor (animal) {
    this.animal = animal
  }

  static get type () {
    return 'pushtest'
  }

  via () {
    return ['fcm']
  }

  toFcm () {
    const message = new FcmMessage()
    switch (this.animal) {
      case 'cat':
        message.addNotification('title', 'Cat')
        message.addNotification('body', 'Meow!')
        message.addNotification('icon', 'cat_black')
        message.addNotification('color', '#ffab00')
        message.addNotification('sound', 'default')
        message.addData('animal', 'cat')
        break

      case 'cow':
        message.addNotification('title', 'Cow')
        message.addNotification('body', 'Moo!')
        message.addNotification('icon', 'cow_black')
        message.addNotification('color', '#aeaeaf')
        message.addNotification('sound', 'default')
        message.addData('animal', 'cow')
        break

      case 'dog':
        message.addNotification('title', 'Dog')
        message.addNotification('body', 'Woof!')
        message.addNotification('icon', 'dog_black')
        message.addNotification('color', '#b19267')
        message.addNotification('sound', 'default')
        message.addData('animal', 'dog')
        break

      case 'duck':
        message.addNotification('title', 'Duck')
        message.addNotification('body', 'Quack!')
        message.addNotification('icon', 'duck_black')
        message.addNotification('color', '#bd7f00')
        message.addNotification('sound', 'default')
        message.addData('animal', 'duck')
        break

      case 'pig':
        message.addNotification('title', 'Pig')
        message.addNotification('body', 'Oink!')
        message.addNotification('icon', 'pig_black')
        message.addNotification('color', '#d37b93')
        message.addNotification('sound', 'default')
        message.addData('animal', 'pig')
        break

      default:
        message.addNotification('title', 'Animal')
        message.addNotification('body', 'A wild animal has appeared!')
        message.addNotification('sound', 'default')
        break
    }
    return message
  }
}

Notifications
  .route('fcm', '<DEVICE_TOKEN>')
  .notify(new PushTestNotification('cat'))

Channels

Credits

Support

Having trouble? Open an issue!

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published