status-go/notifications/push/fcm/notification.go

55 lines
1.2 KiB
Go
Raw Normal View History

2017-10-11 13:51:43 +00:00
package fcm
import (
"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
// Notification represents messaging provider for notifications.
type Notification struct {
client FirebaseClient
2017-10-11 13:51:43 +00:00
}
// NewNotification Firebase Cloud Messaging client constructor.
2018-03-23 08:55:05 +00:00
func NewNotification(key string) NotificationConstructor {
return func() Notifier {
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.
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
if payload.Title == "" {
payload.Title = "Status"
}
if payload.Body == "" {
payload.Body = "You have a new message"
}
2017-10-11 13:51:43 +00:00
fmt.Println(payload.Title, payload.Body)
2017-10-11 13:51:43 +00:00
n.client.NewFcmRegIdsMsg(tokens, data)
n.client.SetNotificationPayload(&payload)
_, err := n.client.Send()
2017-10-11 13:51:43 +00:00
return err
2017-10-11 13:51:43 +00:00
}