Notification with avatar(only in APNs) and picture

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

View File

@ -6,11 +6,6 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":arrow_up: Upgrade dealer"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":arrow_up: Upgrade dealer">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.mod" beforeDir="false" afterPath="$PROJECT_DIR$/go.mod" afterDir="false" />
<change beforePath="$PROJECT_DIR$/go.sum" beforeDir="false" afterPath="$PROJECT_DIR$/go.sum" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/notifications.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -45,34 +40,34 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"DefaultGoTemplateProperty": "Go File", &quot;DefaultGoTemplateProperty&quot;: &quot;Go File&quot;,
"Go Build.Backend.executor": "Run", &quot;Go Build.Backend.executor&quot;: &quot;Run&quot;,
"Go 构建.Backend.executor": "Run", &quot;Go 构建.Backend.executor&quot;: &quot;Run&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.go.formatter.settings.were.checked": "true", &quot;RunOnceActivity.go.formatter.settings.were.checked&quot;: &quot;true&quot;,
"RunOnceActivity.go.migrated.go.modules.settings": "true", &quot;RunOnceActivity.go.migrated.go.modules.settings&quot;: &quot;true&quot;,
"RunOnceActivity.go.modules.automatic.dependencies.download": "true", &quot;RunOnceActivity.go.modules.automatic.dependencies.download&quot;: &quot;true&quot;,
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true", &quot;RunOnceActivity.go.modules.go.list.on.any.changes.was.set&quot;: &quot;true&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"go.import.settings.migrated": "true", &quot;go.import.settings.migrated&quot;: &quot;true&quot;,
"go.sdk.automatically.set": "true", &quot;go.sdk.automatically.set&quot;: &quot;true&quot;,
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/components/admin", &quot;last_opened_file_path&quot;: &quot;/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/components/admin&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"run.code.analysis.last.selected.profile": "pProject Default", &quot;run.code.analysis.last.selected.profile&quot;: &quot;pProject Default&quot;,
"settings.editor.selected.configurable": "preferences.pluginManager", &quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;,
"ts.external.directory.path": "/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib", &quot;ts.external.directory.path&quot;: &quot;/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
}, },
"keyToStringList": { &quot;keyToStringList&quot;: {
"DatabaseDriversLRU": [ &quot;DatabaseDriversLRU&quot;: [
"postgresql" &quot;postgresql&quot;
] ]
} }
}]]></component> }</component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS"> <key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/web/src/components/admin" /> <recent name="$PROJECT_DIR$/web/src/components/admin" />

View File

@ -12,6 +12,8 @@ type Notification struct {
Subtitle *string `json:"subtitle"` Subtitle *string `json:"subtitle"`
Body string `json:"body"` Body string `json:"body"`
Metadata datatypes.JSONMap `json:"metadata"` Metadata datatypes.JSONMap `json:"metadata"`
Avatar *string `json:"avatar"`
Picture *string `json:"picture"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
SenderID *uint `json:"sender_id"` SenderID *uint `json:"sender_id"`
@ -19,12 +21,6 @@ type Notification struct {
IsForcePush bool `json:"is_force_push" gorm:"-"` 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 ( const (
NotifySubscriberFirebase = "firebase" NotifySubscriberFirebase = "firebase"
NotifySubscriberAPNs = "apple" NotifySubscriberAPNs = "apple"

View File

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

View File

@ -1,27 +1,38 @@
<template> <template>
<v-navigation-drawer :model-value="props.open" @update:model-value="(val: any) => emits('update:open', val)" location="right" <v-navigation-drawer
temporary order="0" width="400"> :model-value="props.open"
@update:model-value="(val: any) => emits('update:open', val)"
location="right"
temporary
order="0"
width="400"
>
<v-list-item prepend-icon="mdi-bell" title="Notifications" class="py-3"></v-list-item> <v-list-item prepend-icon="mdi-bell" title="Notifications" class="py-3"></v-list-item>
<v-divider color="black" class="mb-1" /> <v-divider color="black" class="mb-1" />
<v-list v-if="notify.notifications.length <= 0" density="compact"> <v-list v-if="notify.notifications.length <= 0" density="compact">
<v-list-item color="secondary" prepend-icon="mdi-check" title="All notifications read" <v-list-item
subtitle="There is no more new things for you..." /> color="secondary"
prepend-icon="mdi-check"
title="All notifications read"
subtitle="There is no more new things for you..."
/>
</v-list> </v-list>
<v-list v-else density="compact" lines="three"> <v-list v-else density="compact" lines="three">
<v-list-item v-for="(item, idx) in notify.notifications" :key="idx"> <v-list-item v-for="(item, idx) in notify.notifications" :key="idx">
<template #title>{{ item.subject }}</template> <template #title>{{ item.title }}</template>
<template #subtitle>{{ item.content }}</template> <template #subtitle>{{ item.subtitle }}<br v-if="item.subtitle" />{{ item.body }}</template>
<template #append> <template #append>
<v-btn icon="mdi-check" size="x-small" variant="text" :disabled="loading" @click="markAsRead(item, idx)" /> <v-btn icon="mdi-check" size="x-small" variant="text" :disabled="loading" @click="markAsRead(item, idx)" />
</template> </template>
<div class="flex text-xs gap-1"> <div class="flex text-xs gap-1">
<a v-for="(link, idx) in item.links" :key="idx" class="mt-1 underline" target="_blank" <a v-for="(link, idx) in item.links" :key="idx" class="mt-1 underline" target="_blank" :href="link.url">{{
:href="link.url">{{ link.label }}</a> link.label
}}</a>
</div> </div>
</v-list-item> </v-list-item>
</v-list> </v-list>