From 8fcaf99103878446e3ad53b8810a038b1bd2da69 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 19 Jul 2024 23:27:58 +0800 Subject: [PATCH] :sparkles: Notification with avatar(only in APNs) and picture --- .idea/workspace.xml | 55 +++++++++++-------------- pkg/internal/models/notifications.go | 8 +--- pkg/internal/services/notifications.go | 27 ++++++++---- web/src/components/NotificationList.vue | 27 ++++++++---- 4 files changed, 66 insertions(+), 51 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9ef6f4d..f34dd46 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,11 +6,6 @@ - - - - - - { + "keyToString": { + "DefaultGoTemplateProperty": "Go File", + "Go Build.Backend.executor": "Run", + "Go 构建.Backend.executor": "Run", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.go.formatter.settings.were.checked": "true", + "RunOnceActivity.go.migrated.go.modules.settings": "true", + "RunOnceActivity.go.modules.automatic.dependencies.download": "true", + "RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true", + "git-widget-placeholder": "master", + "go.import.settings.migrated": "true", + "go.sdk.automatically.set": "true", + "last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/components/admin", + "node.js.detected.package.eslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "run.code.analysis.last.selected.profile": "pProject Default", + "settings.editor.selected.configurable": "preferences.pluginManager", + "ts.external.directory.path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib", + "vue.rearranger.settings.migration": "true" }, - "keyToStringList": { - "DatabaseDriversLRU": [ - "postgresql" + "keyToStringList": { + "DatabaseDriversLRU": [ + "postgresql" ] } -}]]> +} diff --git a/pkg/internal/models/notifications.go b/pkg/internal/models/notifications.go index 05d2832..9851c98 100644 --- a/pkg/internal/models/notifications.go +++ b/pkg/internal/models/notifications.go @@ -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" diff --git a/pkg/internal/services/notifications.go b/pkg/internal/services/notifications.go index d6f5148..2506212 100644 --- a/pkg/internal/services/notifications.go +++ b/pkg/internal/services/notifications.go @@ -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 { diff --git a/web/src/components/NotificationList.vue b/web/src/components/NotificationList.vue index b7415d2..9dbaf39 100755 --- a/web/src/components/NotificationList.vue +++ b/web/src/components/NotificationList.vue @@ -1,27 +1,38 @@