Handle anonymous read restrictions by sending a poll_request event
If a topic does not allow anonymous reads, this change ensures that we send a "poll_request" event instead of relaying the message via Firebase. Additionally, we include generic text in the title and body/message. This way, if the client cannot retrieve the actual message, the user will still receive a notification, prompting them to update the client manually.
This commit is contained in:
parent
630f2957de
commit
6b2cfb1d1d
|
|
@ -175,13 +175,26 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
|
||||||
} else {
|
} else {
|
||||||
// If anonymous read for a topic is not allowed, we cannot send the message along
|
// If anonymous read for a topic is not allowed, we cannot send the message along
|
||||||
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
|
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
|
||||||
|
//App function needs all the data to create a message object, if not, it fails,
|
||||||
|
//so we set it but put a placeholders to not to send the actual message
|
||||||
|
//but generic title and message instead, we also add the poll_id so client knowns
|
||||||
|
//what message is goint to "decode" (retrieve)
|
||||||
data = map[string]string{
|
data = map[string]string{
|
||||||
"id": m.ID,
|
"id": m.ID,
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
"time": fmt.Sprintf("%d", m.Time),
|
||||||
"event": pollRequestEvent,
|
"event": pollRequestEvent,
|
||||||
"topic": m.Topic,
|
"topic": m.Topic,
|
||||||
}
|
"priority": fmt.Sprintf("%d", m.Priority),
|
||||||
// TODO Handle APNS?
|
"tags": strings.Join(m.Tags, ","),
|
||||||
|
"click": m.Click,
|
||||||
|
"icon": m.Icon,
|
||||||
|
"title": "Private",
|
||||||
|
"message": "Message",
|
||||||
|
"content_type": m.ContentType,
|
||||||
|
"encoding": m.Encoding,
|
||||||
|
"poll_id": m.ID,
|
||||||
|
}
|
||||||
|
apnsConfig = createAPNSAlertConfig(m, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var androidConfig *messaging.AndroidConfig
|
var androidConfig *messaging.AndroidConfig
|
||||||
|
|
@ -225,14 +238,23 @@ func createAPNSAlertConfig(m *message, data map[string]string) *messaging.APNSCo
|
||||||
for k, v := range data {
|
for k, v := range data {
|
||||||
apnsData[k] = v
|
apnsData[k] = v
|
||||||
}
|
}
|
||||||
|
alertTitle := m.Title
|
||||||
|
alertBody := maybeTruncateAPNSBodyMessage(m.Message)
|
||||||
|
// If the event is pollRequestEvent (server/topic is restricted) we dont want to
|
||||||
|
//send the actual message to Firebase/APNS, so we send a generic text
|
||||||
|
//if for some reason, client cant retrieve the message, it shows this as the message and title
|
||||||
|
if event, ok := data["event"]; ok && event == pollRequestEvent {
|
||||||
|
alertTitle = "New Notification received"
|
||||||
|
alertBody = "Message cant be retrieved, open the app and refresh content"
|
||||||
|
}
|
||||||
return &messaging.APNSConfig{
|
return &messaging.APNSConfig{
|
||||||
Payload: &messaging.APNSPayload{
|
Payload: &messaging.APNSPayload{
|
||||||
CustomData: apnsData,
|
CustomData: apnsData,
|
||||||
Aps: &messaging.Aps{
|
Aps: &messaging.Aps{
|
||||||
MutableContent: true,
|
MutableContent: true,
|
||||||
Alert: &messaging.ApsAlert{
|
Alert: &messaging.ApsAlert{
|
||||||
Title: m.Title,
|
Title: alertTitle,
|
||||||
Body: maybeTruncateAPNSBodyMessage(m.Message),
|
Body: alertBody,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue