1.1.2 • Published 5 years ago

adonis-notifications v1.1.2

Weekly downloads
18
License
MIT
Repository
github
Last release
5 years ago

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.

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.0.1

7 years ago