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"
|
2017-10-12 14:31:14 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-10-11 13:51:43 +00:00
|
|
|
)
|
|
|
|
|
2017-10-18 20:56:39 +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
|
|
|
}
|
|
|
|
|
2017-10-18 20:56:39 +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 {
|
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 == "" {
|
|
|
|
payload.Title = "Status - new message"
|
|
|
|
}
|
|
|
|
if payload.Body == "" {
|
|
|
|
payload.Body = "ping"
|
|
|
|
}
|
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
|
|
|
}
|