Skip to content

Commit 99566c8

Browse files
committed
Switch to sending data messages only
Handling of notification messages is quite inconsistent (at least on android). Send the payload in data only and let the app handle the notification locally. Add pageTime to data payload (Unix epoch UTC).
1 parent 01eaa44 commit 99566c8

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

fcm.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77
tmpltext "text/template"
8+
"time"
89

910
"github.com/prometheus/alertmanager/template"
1011
"golang.org/x/net/context"
@@ -59,18 +60,15 @@ func NewMessaging() (*messaging.Client, error) {
5960
return client, err
6061
}
6162

62-
// NewMessage returns a new FCM message struct
63-
func NewMessage(topic, title, body string) *messaging.Message {
63+
func NewDataMessage(topic, title, body string) *messaging.Message {
64+
unixMillisecondsUTC := time.Now().UTC().UnixNano() / 1000000
65+
data := map[string]string{
66+
"title": title,
67+
"body": body,
68+
"pageTime": string(unixMillisecondsUTC),
69+
}
6470
return &messaging.Message{
65-
Notification: &messaging.Notification{
66-
Title: title,
67-
Body: body,
68-
},
69-
Data: map[string]string{
70-
"title": title,
71-
"body": body,
72-
"click_action": "FLUTTER_NOTIFICATION_CLICK",
73-
},
71+
Data: data,
7472
Topic: topic,
7573
// https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
7674
Android: &messaging.AndroidConfig{
@@ -79,7 +77,18 @@ func NewMessage(topic, title, body string) *messaging.Message {
7977
}
8078
}
8179

82-
// NewMessageFromAlertmanagerDats returns a new FCM message from alertmanager POST data
80+
// NewNoficationMessage returns a new FCM message including notificaton data
81+
func NewNoficationMessage(topic, title, body string) *messaging.Message {
82+
message := NewDataMessage(topic, title, body)
83+
message.Notification = &messaging.Notification{
84+
Title: title,
85+
Body: body,
86+
}
87+
message.Data["click_action"] = "FLUTTER_NOTIFICATION_CLICK"
88+
return message
89+
}
90+
91+
// NewMessageFromAlertmanagerDats returns a new FCM data message from alertmanager POST data
8392
func NewMessageFromAlertmanagerData(topic string, m *template.Data) (*messaging.Message, error) {
8493
title, err := tmpltextExecuteToString(tmplTitle, m)
8594
if err != nil {
@@ -91,7 +100,7 @@ func NewMessageFromAlertmanagerData(topic string, m *template.Data) (*messaging.
91100
return nil, &TemplateError{Type: "body", Err: err}
92101
}
93102

94-
return NewMessage(topic, title, body), nil
103+
return NewDataMessage(topic, title, body), nil
95104
}
96105

97106
func tmpltextExecuteToString(tmpl *tmpltext.Template, data interface{}) (string, error) {

handler.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"strconv"
1010

11+
"firebase.google.com/go/messaging"
1112
"github.com/julienschmidt/httprouter"
1213
"github.com/prometheus/alertmanager/template"
1314
)
@@ -19,8 +20,9 @@ type JSONResponse struct {
1920
}
2021

2122
type genericJSONRequest struct {
22-
Title string
23-
Body string
23+
Title string
24+
Body string
25+
Notification bool
2426
}
2527

2628
func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -36,6 +38,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
3638
}
3739

3840
func genericHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
41+
var message *messaging.Message
3942
data, err := readGenericRequestBody(req)
4043
if err != nil {
4144
log.Printf("Error parsing request body: %v", err)
@@ -44,7 +47,11 @@ func genericHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Para
4447
}
4548

4649
topic := getParamTopic(ps)
47-
message := NewMessage(topic, data.Title, data.Body)
50+
if data.Notification {
51+
message = NewNoficationMessage(topic, data.Title, data.Body)
52+
} else {
53+
message = NewDataMessage(topic, data.Title, data.Body)
54+
}
4855
msg, err := fcmClient.Send(req.Context(), message)
4956
fcmMessages.WithLabelValues(ps.MatchedRoutePath(), topic).Inc()
5057

@@ -60,13 +67,6 @@ func genericHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Para
6067
)
6168
}
6269

63-
func readGenericRequestBody(req *http.Request) (*genericJSONRequest, error) {
64-
defer req.Body.Close()
65-
data := genericJSONRequest{}
66-
err := json.NewDecoder(req.Body).Decode(&data)
67-
return &data, err
68-
}
69-
7070
func alertHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
7171
templateData, err := readAlertRequestBody(req)
7272
if err != nil {
@@ -99,6 +99,13 @@ func getParamTopic(ps httprouter.Params) string {
9999
return topic
100100
}
101101

102+
func readGenericRequestBody(req *http.Request) (*genericJSONRequest, error) {
103+
defer req.Body.Close()
104+
data := genericJSONRequest{}
105+
err := json.NewDecoder(req.Body).Decode(&data)
106+
return &data, err
107+
}
108+
102109
func readAlertRequestBody(req *http.Request) (*template.Data, error) {
103110
defer req.Body.Close()
104111
data := template.Data{}

0 commit comments

Comments
 (0)