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

55
.idea/workspace.xml generated
View File

@ -6,11 +6,6 @@
<component name="ChangeListManager">
<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$/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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -45,34 +40,34 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"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"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;DefaultGoTemplateProperty&quot;: &quot;Go File&quot;,
&quot;Go Build.Backend.executor&quot;: &quot;Run&quot;,
&quot;Go 构建.Backend.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.formatter.settings.were.checked&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.migrated.go.modules.settings&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.modules.automatic.dependencies.download&quot;: &quot;true&quot;,
&quot;RunOnceActivity.go.modules.go.list.on.any.changes.was.set&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;go.import.settings.migrated&quot;: &quot;true&quot;,
&quot;go.sdk.automatically.set&quot;: &quot;true&quot;,
&quot;last_opened_file_path&quot;: &quot;/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/src/components/admin&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;run.code.analysis.last.selected.profile&quot;: &quot;pProject Default&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.pluginManager&quot;,
&quot;ts.external.directory.path&quot;: &quot;/Users/littlesheep/Documents/Projects/Hydrogen/Passport/web/node_modules/typescript/lib&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
},
"keyToStringList": {
"DatabaseDriversLRU": [
"postgresql"
&quot;keyToStringList&quot;: {
&quot;DatabaseDriversLRU&quot;: [
&quot;postgresql&quot;
]
}
}]]></component>
}</component>
<component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/web/src/components/admin" />

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 {

View File

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