Initial support for iOS app

This commit is contained in:
Andrew Cope 2022-02-23 22:15:22 -05:00
parent 2eff8d6b47
commit 1b5be4fc6b
5 changed files with 61 additions and 1 deletions

View file

@ -13,6 +13,7 @@ import (
const (
fcmMessageLimit = 4000
fcmApnsBodyMessageLimit = 100
)
// maybeTruncateFCMMessage performs best-effort truncation of FCM messages.
@ -34,6 +35,14 @@ func maybeTruncateFCMMessage(m *messaging.Message) *messaging.Message {
return m
}
func maybeTruncateAPNSBodyMessage(s string) string {
if len(s) >= fcmApnsBodyMessageLimit {
over := len(s) - fcmApnsBodyMessageLimit + 3 // len("...")
return s[:len(s)-over] + "..."
}
return s
}
func createFirebaseSubscriber(credentialsFile string, auther auth.Auther) (subscriber, error) {
fb, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsFile(credentialsFile))
if err != nil {
@ -55,6 +64,7 @@ func createFirebaseSubscriber(credentialsFile string, auther auth.Auther) (subsc
func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, error) {
var data map[string]string // Mostly matches https://ntfy.sh/docs/subscribe/api/#json-message-format
var apnsConfig *messaging.APNSConfig
switch m.Event {
case keepaliveEvent, openEvent:
data = map[string]string{
@ -88,6 +98,17 @@ func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, erro
data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
data["attachment_url"] = m.Attachment.URL
}
apnsConfig = &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
MutableContent: true,
Alert: &messaging.ApsAlert{
Title: m.Title,
Body: maybeTruncateAPNSBodyMessage(m.Message),
},
},
},
}
} else {
// 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.
@ -109,5 +130,6 @@ func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, erro
Topic: m.Topic,
Data: data,
Android: androidConfig,
APNS: apnsConfig,
}), nil
}