Notifications

Notifications #

The need for information exchange and transmission is always necessary and diverse. That happens within your application, between application and user, or even application to application.

IMPORTANT!
The Notification file should be placed in directory app/notifications

gFly notification tries to create built-in utilities to help developers easily send information.

gFly already supports four communication channels: Email, Database, SMS, Slack

IMPORTANT!
You can turn this function on and off by

# NOTE: Notification settings:
NOTIFICATION_ENABLE=false

Parameters:

# NOTE: Mail settings:
MAIL_PROTOCOL=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_SENDER=no-reply@gfly.dev
MAIL_NAME="gFly - No Reply"
MAIL_STARTTLS=true
MAIL_ENCRYPTED=true
...
# NOTE: SMS Settings:
TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
...
# NOTE: Slack Settings:
SLACK_CHANNEL=

Notification ResetPassword #

Let create simple notification ResetPassword

Define notification #

Create very simple notification ResetPassword. So, you can create below type

type ResetPassword string

And you want to message to Email, Database, Slack. So, create 3 methods of type ResetPassword. Check completed code:

package notifications

import (
    "app/core/notification"
    "fmt"
    "github.com/google/uuid"
    "time"
)

type ResetPassword string

// ToEmail send message via Email
func (n ResetPassword) ToEmail() notification.MailNotificationData {
    return notification.MailNotificationData{
        To:      "johnho@mail.com",
        Subject: "Reset password",
        Body:    fmt.Sprintf("Request to reset password at time %v", time.Now()),
    }
}

// ToDatabase send message to Database
func (n ResetPassword) ToDatabase() notification.DatabaseNotificationData {
    userId := uuid.New()

    return notification.DatabaseNotificationData{
        Type:            "ResetPassword",
        NotifiableGroup: "user",
        NotifiableId:    userId.String(),
        Data:            fmt.Sprintf("Request to reset password at time %v", time.Now()),
    }
}

// ToSlack send message to Slack
func (n ResetPassword) ToSlack() notification.SlackNotificationData {
    return notification.SlackNotificationData{
        Message: fmt.Sprintf("Request to reset password at time %v", time.Now()),
    }
}

Send notification #

Wherever you want to send a notification, just call:

_ = notification.Send(notifications.ResetPassword(""))