status-go/geth/notification/fcm/notification.go

48 lines
1.0 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"
2017-10-12 14:31:14 +00:00
"github.com/status-im/status-go/geth/common"
2017-10-11 13:51:43 +00:00
)
// Notification represents messaging provider for notifications.
type Notification struct {
2017-10-12 14:31:14 +00:00
client firebaseClient
2017-10-11 13:51:43 +00:00
}
// NewNotification Firebase Cloud Messaging client constructor.
func NewNotification(key string) common.NotificationConstructor {
2017-10-12 14:31:14 +00:00
return func() common.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 - new message"
}
if payload.Body == "" {
payload.Body = "ping"
}
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
}