Skip to content

Commit 6b2cfb1

Browse files
authored
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.
1 parent 630f295 commit 6b2cfb1

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

server/server_firebase.go

+30-8
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,26 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
175175
} else {
176176
// If anonymous read for a topic is not allowed, we cannot send the message along
177177
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
178+
//App function needs all the data to create a message object, if not, it fails,
179+
//so we set it but put a placeholders to not to send the actual message
180+
//but generic title and message instead, we also add the poll_id so client knowns
181+
//what message is goint to "decode" (retrieve)
178182
data = map[string]string{
179-
"id": m.ID,
180-
"time": fmt.Sprintf("%d", m.Time),
181-
"event": pollRequestEvent,
182-
"topic": m.Topic,
183-
}
184-
// TODO Handle APNS?
183+
"id": m.ID,
184+
"time": fmt.Sprintf("%d", m.Time),
185+
"event": pollRequestEvent,
186+
"topic": m.Topic,
187+
"priority": fmt.Sprintf("%d", m.Priority),
188+
"tags": strings.Join(m.Tags, ","),
189+
"click": m.Click,
190+
"icon": m.Icon,
191+
"title": "Private",
192+
"message": "Message",
193+
"content_type": m.ContentType,
194+
"encoding": m.Encoding,
195+
"poll_id": m.ID,
196+
}
197+
apnsConfig = createAPNSAlertConfig(m, data)
185198
}
186199
}
187200
var androidConfig *messaging.AndroidConfig
@@ -225,14 +238,23 @@ func createAPNSAlertConfig(m *message, data map[string]string) *messaging.APNSCo
225238
for k, v := range data {
226239
apnsData[k] = v
227240
}
241+
alertTitle := m.Title
242+
alertBody := maybeTruncateAPNSBodyMessage(m.Message)
243+
// If the event is pollRequestEvent (server/topic is restricted) we dont want to
244+
//send the actual message to Firebase/APNS, so we send a generic text
245+
//if for some reason, client cant retrieve the message, it shows this as the message and title
246+
if event, ok := data["event"]; ok && event == pollRequestEvent {
247+
alertTitle = "New Notification received"
248+
alertBody = "Message cant be retrieved, open the app and refresh content"
249+
}
228250
return &messaging.APNSConfig{
229251
Payload: &messaging.APNSPayload{
230252
CustomData: apnsData,
231253
Aps: &messaging.Aps{
232254
MutableContent: true,
233255
Alert: &messaging.ApsAlert{
234-
Title: m.Title,
235-
Body: maybeTruncateAPNSBodyMessage(m.Message),
256+
Title: alertTitle,
257+
Body: alertBody,
236258
},
237259
},
238260
},

0 commit comments

Comments
 (0)