diff --git a/notifications/push/contract.go b/notifications/push/contract.go index 57ad2a5..1626a3b 100644 --- a/notifications/push/contract.go +++ b/notifications/push/contract.go @@ -30,14 +30,18 @@ type ( Title string `json:"title,omitempty"` Body string `json:"body,omitempty"` ImageURL string `json:"imageUrl,omitempty"` - MinDelay uint `json:"minDelay"` - MaxDelay uint `json:"maxDelay"` + } + DelayedNotification struct { + *Notification[SubscriptionTopic] + MinDelay uint `json:"minDelay"` + MaxDelay uint `json:"maxDelay"` } Client interface { io.Closer Send(context.Context, *Notification[DeviceToken], chan<- error) Broadcast(context.Context, *Notification[SubscriptionTopic]) error + BroadcastDelayed(context.Context, *DelayedNotification) error } ) @@ -52,12 +56,13 @@ const ( dataOnlyTitle = "title" dataOnlyBody = "body" - dataOnlyImageURL = "imageURL" + dataOnlyImageURL = "imageUrl" dataOnlyMinDelay = "minDelaySec" dataOnlyMaxDelay = "maxDelaySec" dataOnlyType = "type" typeDelayedNotification = "delayed" + priorityHigh = "high" ) type ( diff --git a/notifications/push/push.go b/notifications/push/push.go index 2476b19..a7dc721 100644 --- a/notifications/push/push.go +++ b/notifications/push/push.go @@ -123,6 +123,22 @@ func (p *push) Send(ctx context.Context, notif *Notification[DeviceToken], respo } func (p *push) Broadcast(ctx context.Context, notification *Notification[SubscriptionTopic]) error { + return errors.Wrapf(retry(ctx, func() error { + _, err := p.client.Send(ctx, &fcm.Message{ + Data: notification.Data, + Notification: &fcm.Notification{ + Title: notification.Title, + Body: notification.Body, + ImageURL: notification.ImageURL, + }, + Topic: string(notification.Target), + }) + + return err //nolint:wrapcheck // No need to do that, it's wrapped outside. + }), "[%v] permanently failed to broadcast %#v", p.applicationYAMLKey, notification) +} + +func (p *push) BroadcastDelayed(ctx context.Context, notification *DelayedNotification) error { return errors.Wrapf(retry(ctx, func() error { _, err := p.client.Send(ctx, &fcm.Message{ Data: notification.Data, @@ -132,10 +148,10 @@ func (p *push) Broadcast(ctx context.Context, notification *Notification[Subscri }) return err //nolint:wrapcheck // No need to do that, it's wrapped outside. - }), "[%v] permanently failed to broadcast %#v", p.applicationYAMLKey, notification) + }), "[%v] permanently failed to broadcast delayed notification %#v", p.applicationYAMLKey, notification) } -func buildAndroidDataOnlyNotification(notification *Notification[SubscriptionTopic]) *fcm.AndroidConfig { +func buildAndroidDataOnlyNotification(notification *DelayedNotification) *fcm.AndroidConfig { dataOnlyNotification := make(map[string]string, len(notification.Data)+3) for k, v := range notification.Data { dataOnlyNotification[k] = v @@ -149,11 +165,11 @@ func buildAndroidDataOnlyNotification(notification *Notification[SubscriptionTop return &fcm.AndroidConfig{ Data: dataOnlyNotification, - Priority: "high", + Priority: priorityHigh, } } -func buildAppleNotification(notification *Notification[SubscriptionTopic]) *fcm.APNSConfig { +func buildAppleNotification(notification *DelayedNotification) *fcm.APNSConfig { return &fcm.APNSConfig{ Payload: &fcm.APNSPayload{ Aps: &fcm.Aps{