fcm/message.go

221 lines
9.2 KiB
Go
Raw Normal View History

package fcm
2025-01-29 14:28:24 +03:00
// Message to send by Firebase Cloud Messaging Service.
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
type Message struct {
2025-01-29 11:51:28 +03:00
// Union field target can be only one of the following:
Token string `json:"token,omitempty"`
Topic string `json:"topic,omitempty"`
Condition string `json:"condition,omitempty"`
// End of list of possible types for union field target.
Data map[string]string `json:"data,omitempty"`
Notification *Notification `json:"notification,omitempty"`
FcmOptions *FcmOptions `json:"fcm_options,omitempty"`
Android *AndroidConfig `json:"android,omitempty"`
Webpush *WebpushConfig `json:"webpush,omitempty"`
Apns *ApnsConfig `json:"apns,omitempty"`
}
// Basic notification template to use across all platforms.
2025-01-29 14:28:24 +03:00
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
type Notification struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Image string `json:"image,omitempty"`
}
type FcmOptions struct {
// Label associated with the message's analytics data.
// Format: ^[a-zA-Z0-9-_.~%]{1,50}$
AnalyticsLabel string `json:"analytics_label,omitempty"`
}
type AndroidConfig struct {
CollapseKey string `json:"collapse_key,omitempty"`
Priority AndroidMessagePriority `json:"priority,omitempty"`
TTL string `json:"ttl,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
Data map[string]string `json:"data,omitempty"`
Notification *AndroidNotification `json:"notification,omitempty"`
FcmOptions *AndroidFcmOptions `json:"fcm_options,omitempty"`
DirectBootOk bool `json:"direct_boot_ok,omitempty"`
}
type AndroidMessagePriority uint8
const (
// Default priority for data messages.
//
// Normal priority messages won't open network connections on a sleeping device,
// and their delivery may be delayed to conserve the battery.
AndroidMessagePriority_NORMAL AndroidMessagePriority = 0
// Default priority for notification messages.
//
// FCM attempts to deliver high priority messages immediately,
// allowing the FCM service to wake a sleeping device when possible
// and open a network connection to your app server.
AndroidMessagePriority_HIGH AndroidMessagePriority = 1
)
type AndroidFcmOptions struct {
AnalyticsLabel string `json:"analytics_label,omitempty"`
}
type NotificationPriority uint8
const (
NotificationPriority_UNSPECIFIED NotificationPriority = 0
// Notifications might not be shown to the user except under special circumstances, such as detailed notification logs.
NotificationPriority_MIN NotificationPriority = 1
// The UI may choose to show the notifications smaller, or at a different position in the list, compared with notifications with DEFAULT.
NotificationPriority_LOW NotificationPriority = 2
// If the application does not prioritize its own notifications, use this value for all notifications.
NotificationPriority_DEFAULT NotificationPriority = 3
// Use this for more important notifications or alerts.
//
// The UI may choose to show these notifications larger, or at a different position in the notification lists, compared with notifications with DEFAULT.
NotificationPriority_HIGH NotificationPriority = 4
// Use this for the application's most important items that require the user's prompt attention or input.
NotificationPriority_MAX NotificationPriority = 5
)
type NotificationVisibility uint8
const (
NotificationVisibility_UNSPECIFIED NotificationVisibility = 0
// Show this notification on all lockscreens, but conceal sensitive or private information on secure lockscreens.
NotificationVisibility_PRIVATE NotificationVisibility = 1
// Show this notification in its entirety on all lockscreens.
NotificationVisibility_PUBLIC NotificationVisibility = 2
// Do not reveal any part of this notification on a secure lockscreen.
NotificationVisibility_SECRET NotificationVisibility = 3
)
type LightSettings struct {
Color Color `json:"color,omitempty"`
OnDuration string `json:"light_on_duration,omitempty"`
OffDuration string `json:"light_off_duration,omitempty"`
}
type Color struct {
Red float32 `json:"red,omitempty"`
Green float32 `json:"green,omitempty"`
Blue float32 `json:"blue,omitempty"`
Alpha float32 `json:"alpha,omitempty"`
}
type AndroidNotification struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Image string `json:"image,omitempty"`
Icon string `json:"icon,omitempty"`
Color string `json:"color,omitempty"`
Sound string `json:"sound,omitempty"`
Tag string `json:"tag,omitempty"`
ClickAction string `json:"click_action,omitempty"`
ChannelID string `json:"channel_id,omitempty"`
BodyLocalizationKey string `json:"body_loc_key,omitempty"`
BodyLocalizationArgs []string `json:"body_loc_args,omitempty"`
TitleLocalizationKey string `json:"title_loc_key,omitempty"`
TitleLocalizationArgs []string `json:"title_loc_args,omitempty"`
Ticker string `json:"ticker,omitempty"`
Sticky bool `json:"sticky,omitempty"`
EventTime string `json:"event_time,omitempty"`
LocalOnly bool `json:"local_only,omitempty"`
Priority NotificationPriority `json:"notification_priority,omitempty"`
UseDefaultSound bool `json:"default_sound,omitempty"`
UseDefaultVibrateTimings bool `json:"default_vibrate_timings,omitempty"`
UseDefaultLightSettings bool `json:"default_light_settings,omitempty"`
VibrateTimings []string `json:"vibrate_timings,omitempty"`
Visibility NotificationVisibility `json:"visibility,omitempty"`
Count int `json:"notification_count,omitempty"`
LightSettings *LightSettings `json:"light_settings,omitempty"`
}
type WebpushConfig struct {
Headers map[string]string `json:"headers,omitempty"`
Data map[string]string `json:"data,omitempty"`
Notification WebpushNotification `json:"notification,omitempty"`
FcmOptions WebpushFcmOptions `json:"fcm_options,omitempty"`
}
type WebpushNotification struct{}
type WebpushFcmOptions struct {
Link string `json:"link,omitempty"`
AnalyticsLabel string `json:"analytics_label,omitempty"`
}
// Apple specific options for message
type ApnsConfig struct {
Headers *ApnsHeaders `json:"headers,omitempty"`
Notification *ApnsPayload `json:"payload,omitempty"`
FcmOptions *ApnsFcmOptions `json:"fcm_options,omitempty"`
}
type ApnsHeaders struct {
PushType string `json:"apns-push-type,omitempty"`
Id string `json:"apns-id,omitempty"`
Expiration int64 `json:"apns-expiration,omitempty"`
Priority uint8 `json:"apns-priority,omitempty"`
Topic string `json:"apns-topic,omitempty"`
CollapseId string `json:"apns-collapse-id,omitempty"`
}
type ApnsPayload struct {
Aps ApnsPayloadKeys `json:"aps"`
// TODO: add support for custom keys
2025-01-29 14:34:26 +03:00
// https://developer.apple.com/documentation/usernotifications/generating-a-remote-notification
2025-01-29 14:31:13 +03:00
customKeys map[string]any
}
type ApnsPayloadKeys struct {
Alert *ApnsAlert `json:"alert,omitempty"`
Badge int `json:"badge,omitempty"`
Sound string `json:"sound,omitempty"`
ThreadId string `json:"thread-id,omitempty"`
Category string `json:"category,omitempty"`
ContentAvailable int `json:"content-available,omitempty"`
MutableContent int `json:"mutable-content,omitempty"`
TargetContentId string `json:"target-content-id,omitempty"`
InterruptionLevel string `json:"interruption-level,omitempty"`
RelevanceScore int `json:"relevance-score,omitempty"`
CriticalSound *ApnsCriticalSound `json:"-"`
}
type ApnsAlert struct {
Title string `json:"title,omitempty"`
Subtitle string `json:"subtitle,omitempty"`
Body string `json:"body,omitempty"`
LaunchImg string `json:"launch-image,omitempty"`
TitleLocKey string `json:"title-loc-key,omitempty"`
TitleLocArgs []string `json:"title-loc-args,omitempty"`
SubtitleLocKey string `json:"subtitle-loc-key,omitempty"`
SubtitleLocArgs []string `json:"subtitle-loc-args,omitempty"`
LocKey string `json:"loc-key,omitempty"`
LocArgs []string `json:"loc-args,omitempty"`
}
type ApnsCriticalSound struct {
Critical int `json:"critical,omitempty"`
Name string `json:"name,omitempty"`
Volume int `json:"volume,omitempty"`
}
type ApnsFcmOptions struct {
AnalyticsLabel string `json:"analytics_label,omitempty"`
Image string `json:"image,omitempty"`
}