In this article, we will understand how to use Notify module end to end to add new channels to send notifications to channels.

Add new channels

NotifyChannel

Notify module provides a NotifyChannel facade to add new channels in the system. Developers can use the methods offered by the NotifyChannel facade to add new channels to the system. Notify will handle the related tasks, such as adding templates for new channels for existing notifications.

getChannels

Use this method to get the list of available channels with their details. Additionally, specify the channel name in the argument to get details for the channel if it exists in the system.

NotifyChannel::getChannels()
//output
[
     [
       "id" => 1,
       "channel" => "email",
       "lang_key" => "email_lang_key",
       "desc_lang_key" => "email_notification_channel_template_description",
       "icon" => "fas fa-envelope",
       "template_types" => "long",
       "plugin_name" => null,
     ],
     [
       "id" => 2,
       "channel" => "in-app",
       "lang_key" => "in-app_lang_key",
       "desc_lang_key" => "in_app_desc_key",
       "icon" => "far fa-bell",
       "template_types" => "short",
       "plugin_name" => null,
     ],
]

or

NotifyChannel::getChannels('email')
//output
[
     [
       "id" => 1,
       "channel" => "email",
       "lang_key" => "email_lang_key",
       "desc_lang_key" => "email_notification_channel_template_description",
       "icon" => "fas fa-envelope",
       "template_types" => "long",
       "plugin_name" => null,
     ],
]

or

NotifyChannel::getChannels('facebook')
[] //channel not available

createOrUpdateChannel

Use this method to add new channels to the system. The method accepts the array containing the list of channels with their details. For more, see addDefaulChannels method of ‘database\seeds\v_5_0_0\NotifySeeder’.

The channel detail array must contain the given details

  • channel: Name of the channel
  • desc_lang_key: language key for channel description
  • icon: icon class for showing channel symbol in frontend
  • template_types: type of templates the channel supports. Choose from long or short. Email may support long content, while SMS/In-app will only support short content. So channel authors should define template content type as supported by the platform.
  • event_listener_class: Channel must provide the name of the event class which they expect notify module to dispatch for notifications.
NotifyChannel::createOrUpdateChannel([
   	['channel' => 'email', 'desc_lang_key' => 'email_notification_channel_template_description', 'icon' => 'fas fa-envelope', 'template_types' => 'long', 'event_listener_class' => 'App\Events\NotifyToEmail'],
    ['channel' => 'in-app', 'desc_lang_key' => 'in_app_desc_key', 'icon' => 'far fa-bell', 'template_types' => 'short', 'event_listener_class' => 'App\Events\NotifyToInApp']
])
//output
[
     [
       "id" => 1,
       "channel" => "email",
       "lang_key" => "email_lang_key",
       "desc_lang_key" => "email_notification_channel_template_description",
       "icon" => "fas fa-envelope",
       "template_types" => "long",
       "plugin_name" => null,
     ],
     [
       "id" => 2,
       "channel" => "in-app",
       "lang_key" => "in-app_lang_key",
       "desc_lang_key" => "in_app_desc_key",
       "icon" => "far fa-bell",
       "template_types" => "short",
       "plugin_name" => null,
     ],
]

Send notifications via channels.

Notify module dispatches events to notify channels about sending notifications with the same predefined payload. All channel authors must register the notify event class and their listeners, which they will use to process and send notifications.

Note: Channel authors should already plan for the event and listener class they will use, as they need to provide the event class name while adding the channels using the NotifyChannel facade.

Reference files:

Core

  • App\Providers\EventServiceProvider
  • App\Events\NotifyToEmail
  • App\Listeners\NotifyEmailListener
  • App\Events\NotifyToInApp
  • App\Listeners\NotifyInAppListener

Plugin

  • App\Plugins\SMS\EventMap
  • App\Plugins\SMS\ServiceProvider
  • App\Plugins\SMS\Events\NotifyToSMS
  • App\Plugins\SMS\Listeners\NotifySMSListener

 

Notify will use that event class and dispatch that event with the payload below.

App\Events\NotifyToEmail {#3969
  +channel: "email"
  +subject: "Registration Confirmation"
  +content: "<div style="width: 100%!important; margin: 0; padding: 0;"><div style="padding: 15px; line-height: 18px; font-family: Lucida Grande,Verdana,Arial,sans-serif; font-size: 12px; color: #444444;"><p>Hello Manish Verma,<br /><br />This email is confirmation that you are now registered on <a href="https://faveo.pro" target="_blank">Support Center</a>.<br /><br /><strong>Registered Email:</strong> mverma16@outlook.com<br /><strong>Password:</strong> vIupkD9c<br /><br />You can visit the helpdesk to browse articles and contact us at any time: <a href="https://faveo.pro" target="_blank">https://faveo.pro</a></p>Kind regards,<br />Support Center</div></div>"
  +user: App\User {#3980
    #table: "users"
    #fillable: array:45 [
      0 => "user_name"
      1 => "email"
      2 => "password"
      3 => "active"
      4 => "first_name"
      5 => "last_name"
      6 => "ext"
      7 => "mobile"
      8 => "profile_pic"
      9 => "phone_number"
      10 => "company"
      11 => "agent_sign"
      12 => "account_type"
      13 => "account_status"
      14 => "user_language"
      15 => "assign_group"
      16 => "primary_dpt"
      17 => "agent_tzone"
      18 => "daylight_save"
      19 => "limit_access"
      20 => "directory_listing"
      21 => "vacation_mode"
      22 => "role"
      23 => "internal_note"
      24 => "country_code"
      25 => "not_accept_ticket"
      26 => "is_delete"
      27 => "mobile_verify"
      28 => "phone_country_code"
      29 => "phone_iso"
      30 => "email_verify"
      31 => "location"
      32 => "department"
      33 => "import_identifier"
      34 => "google2fa_secret"
      35 => "is_2fa_enabled"
      36 => "google2fa_activation_date"
      37 => "last_login_at"
      38 => "delete_account_requested"
      39 => "processing_account_disabling"
      40 => "iso"
      41 => "password"
      42 => "created_by"
      43 => "deactivated_by"
      44 => "deactivated_at"
    ]
    #hidden: array:6 [
      0 => "password"
      1 => "remember_token"
      2 => "hash_ids"
      3 => "google2fa_secret"
      4 => "mobile_verify"
      5 => "email_verify"
    ]
    #appends: array:5 [
      0 => "hash_ids"
      1 => "full_name"
      2 => "meta_name"
      3 => "email_verified"
      4 => "mobile_verified"
    ]
    #htmlAble: array:1 [
      0 => "agent_sign"
    ]
    #connection: "mysql"
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #withCount: []
    #perPage: 15
    +exists: true
    +wasRecentlyCreated: true
    #attributes: array:21 [
      "user_name" => "mverma16@outlook.com"
      "password" => "$2y$10$t3LkXLbJ.3/GM61U6u2Auew/daS5hMOk2VfprBz9tFgXmtc3woyEG"
      "first_name" => "Manish"
      "last_name" => "Verma"
      "email_verify" => "xKUoYpUaUrauOX9N9mR4HF8nn7R30rgginghZ9unVNG3IT8nU65kVSzuXGBc"
      "email" => "mverma16@outlook.com"
      "role" => "user"
      "mobile_verify" => "xXaeDqNFbZNQWgHqpO5NssGCPCKTcA58ChN6zWLvsgUiad4dgDGMi2Gg2rfA"
      "mobile" => null
      "phone_number" => null
      "phone_iso" => null
      "phone_country_code" => ""
      "iso" => null
      "country_code" => ""
      "agent_tzone" => "81"
      "ext" => null
      "internal_note" => null
      "created_by" => 1
      "updated_at" => "2022-10-09 11:49:48"
      "created_at" => "2022-10-09 11:49:48"
      "id" => 2
    ]
    #original: array:21 [
      "user_name" => "mverma16@outlook.com"
      "password" => "$2y$10$t3LkXLbJ.3/GM61U6u2Auew/daS5hMOk2VfprBz9tFgXmtc3woyEG"
      "first_name" => "Manish"
      "last_name" => "Verma"
      "email_verify" => "xKUoYpUaUrauOX9N9mR4HF8nn7R30rgginghZ9unVNG3IT8nU65kVSzuXGBc"
      "email" => "mverma16@outlook.com"
      "role" => "user"
      "mobile_verify" => "xXaeDqNFbZNQWgHqpO5NssGCPCKTcA58ChN6zWLvsgUiad4dgDGMi2Gg2rfA"
      "mobile" => null
      "phone_number" => null
      "phone_iso" => null
      "phone_country_code" => ""
      "iso" => null
      "country_code" => ""
      "agent_tzone" => "81"
      "ext" => null
      "internal_note" => null
      "created_by" => 1
      "updated_at" => "2022-10-09 11:49:48"
      "created_at" => "2022-10-09 11:49:48"
      "id" => 2
    ]
    #changes: []
    #casts: []
    #dates: []
    #dateFormat: null
    #dispatchesEvents: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #visible: []
    #guarded: array:1 [
      0 => "*"
    ]
    #rememberTokenName: "remember_token"
    #accessToken: null
  }
  +payload: array:2 [
    "eventModel" => App\User {#3980}
    "variables" => array:3 [
      "new_user_name" => "Manish Verma"
      "new_user_email" => "mverma16@outlook.com"
      "user_password" => "vIupkD9c"
    ]
  ]
  +scenario: "new_user_registration_confirmation"
  +socket: null
}

Notify event payload attributes explained

  • channel (string): name of the channel
  • subject (string): the subject for the notification resolved from the template
  • content (string): content for the notification resolved from the template
  • user (App\User): User model, the receiver of the notification
  • payload (Array): Additional payload data passed by the module authors. Can be used to format the notification further as per channel requirements. Such as adding attachments etc.
  • scenario (string): Scenario of the notification
  • queue (string): Used only by Notify to process notifications in the background.

Summary

Channel authors can use methods offered by NotifyChannel to add new channels. They must implement event and listener class which notify will use to dispatch the payload for notifications.