Notification with avatar(only in APNs) and picture

This commit is contained in:
2024-07-19 23:27:58 +08:00
parent 282a0891d0
commit 8fcaf99103
4 changed files with 66 additions and 51 deletions

View File

@ -12,6 +12,8 @@ type Notification struct {
Subtitle *string `json:"subtitle"`
Body string `json:"body"`
Metadata datatypes.JSONMap `json:"metadata"`
Avatar *string `json:"avatar"`
Picture *string `json:"picture"`
AccountID uint `json:"account_id"`
SenderID *uint `json:"sender_id"`
@ -19,12 +21,6 @@ type Notification struct {
IsForcePush bool `json:"is_force_push" gorm:"-"`
}
// NotificationLink Used to embed into notify and render actions
type NotificationLink struct {
Label string `json:"label"`
Url string `json:"url"`
}
const (
NotifySubscriberFirebase = "firebase"
NotifySubscriberAPNs = "apple"

View File

@ -3,12 +3,13 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"reflect"
"sync"
"time"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"firebase.google.com/go/messaging"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
@ -112,10 +113,15 @@ func PushNotification(notification models.Notification) error {
break
}
var image string
if notification.Picture != nil {
image = *notification.Picture
}
message := &messaging.Message{
Notification: &messaging.Notification{
Title: notification.Title,
Body: notification.Body,
Title: notification.Title,
Body: notification.Body,
ImageURL: image,
},
Token: subscriber.DeviceToken,
}
@ -131,13 +137,20 @@ func PushNotification(notification models.Notification) error {
}
case models.NotifySubscriberAPNs:
if ExtAPNS != nil {
data, err := payload2.
data := payload2.
NewPayload().
AlertTitle(notification.Title).
AlertBody(notification.Body).
Sound("default").
Category(notification.Topic).
MarshalJSON()
MutableContent()
if notification.Avatar != nil {
data = data.Custom("avatar_url", *notification.Avatar)
}
if notification.Picture != nil {
data = data.Custom("picture_url", *notification.Picture)
}
rawData, err := data.MarshalJSON()
if err != nil {
log.Warn().Err(err).Msg("An error occurred when preparing to notify subscriber via APNs...")
}
@ -145,7 +158,7 @@ func PushNotification(notification models.Notification) error {
ApnsID: subscriber.DeviceID,
DeviceToken: subscriber.DeviceToken,
Topic: viper.GetString("apns_topic"),
Payload: data,
Payload: rawData,
}
if resp, err := ExtAPNS.Push(payload); err != nil {