2017-10-11 13:51:43 +00:00
|
|
|
package fcm
|
|
|
|
|
|
|
|
import (
|
2017-10-23 15:46:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-10-11 13:51:43 +00:00
|
|
|
"github.com/NaySoftware/go-fcm"
|
|
|
|
)
|
|
|
|
|
2018-03-23 08:55:05 +00:00
|
|
|
// Notifier manages Push Notifications.
|
|
|
|
type Notifier interface {
|
|
|
|
Send(body string, payload fcm.NotificationPayload, tokens ...string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationConstructor returns constructor of configured instance Notifier interface.
|
|
|
|
type NotificationConstructor func() Notifier
|
|
|
|
|
2017-10-18 20:56:39 +00:00
|
|
|
// Notification represents messaging provider for notifications.
|
|
|
|
type Notification struct {
|
2018-04-05 13:14:47 +00:00
|
|
|
client FirebaseClient
|
2017-10-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 20:56:39 +00:00
|
|
|
// NewNotification Firebase Cloud Messaging client constructor.
|
2018-03-23 08:55:05 +00:00
|
|
|
func NewNotification(key string) NotificationConstructor {
|
|
|
|
return func() Notifier {
|
2017-10-23 15:46:51 +00:00
|
|
|
client := fcm.NewFcmClient(key).
|
|
|
|
SetDelayWhileIdle(true).
|
|
|
|
SetContentAvailable(true).
|
|
|
|
SetTimeToLive(fcm.MAX_TTL)
|
|
|
|
|
|
|
|
return &Notification{client}
|
2017-10-12 14:31:14 +00:00
|
|
|
}
|
2017-10-11 13:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 20:17:40 +00:00
|
|
|
// Send send to the tokens list.
|
2017-10-23 15:46:51 +00:00
|
|
|
func (n *Notification) Send(body string, payload fcm.NotificationPayload, tokens ...string) error {
|
|
|
|
data := map[string]string{
|
|
|
|
"msg": body,
|
|
|
|
}
|
2017-10-11 13:51:43 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
if payload.Title == "" {
|
2018-02-26 17:17:42 +00:00
|
|
|
payload.Title = "Status"
|
2017-10-23 15:46:51 +00:00
|
|
|
}
|
|
|
|
if payload.Body == "" {
|
2018-02-26 17:17:42 +00:00
|
|
|
payload.Body = "You have a new message"
|
2017-10-23 15:46:51 +00:00
|
|
|
}
|
2017-10-11 13:51:43 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
fmt.Println(payload.Title, payload.Body)
|
2017-10-11 13:51:43 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
n.client.NewFcmRegIdsMsg(tokens, data)
|
|
|
|
n.client.SetNotificationPayload(&payload)
|
|
|
|
_, err := n.client.Send()
|
2017-10-11 13:51:43 +00:00
|
|
|
|
2017-10-23 15:46:51 +00:00
|
|
|
return err
|
2017-10-11 13:51:43 +00:00
|
|
|
}
|