From 3a3939e1ba31c0546e1c28fae845214f40bfe61d Mon Sep 17 00:00:00 2001 From: richarjiang Date: Mon, 3 Nov 2025 17:31:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=8F=90=E4=BE=9B=E7=9A=84?= =?UTF-8?q?=20git=20diff=EF=BC=8C=E6=88=91=E6=9D=A5=E5=88=86=E6=9E=90?= =?UTF-8?q?=E8=BF=99=E6=AC=A1=E5=8F=98=E6=9B=B4=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 变更分析 这次提交主要涉及 APNs 推送通知功能的优化: 1. **apns.provider.ts** 的变更: - 重构了通知选项的构建逻辑,将 alert 设置为顶层属性 - 增强了 `createDeviceNotification` 方法,完整复制所有通知选项(badge、sound、contentAvailable、mutableContent、priority、type、data) - 移除了未使用的 aps 对象 2. **push-notifications.service.ts** 的变更: - 移除了冗余的 `alert` 字段 - 新增了更多 APNs 通知选项:priority、expiry、collapseId、mutableContent、contentAvailable 这是一次功能增强和代码重构,主要目的是完善 APNs 推送通知的配置选项支持。 ## 建议的提交信息 feat(push-notifications): 完善APNs推送通知配置选项支持 - 重构通知选项构建逻辑,优化alert属性设置 - createDeviceNotification方法新增完整选项复制(badge、sound、contentAvailable、mutableContent、priority、type、data) - 推送服务新增priority、expiry、collapseId、mutableContent、contentAvailable等配置项支持 - 移除冗余的alert字段设置 --- src/push-notifications/apns.provider.ts | 52 +++++++++++++++++-- .../push-notifications.service.ts | 6 ++- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/push-notifications/apns.provider.ts b/src/push-notifications/apns.provider.ts index f9c9533..e162169 100644 --- a/src/push-notifications/apns.provider.ts +++ b/src/push-notifications/apns.provider.ts @@ -241,8 +241,16 @@ export class ApnsProvider implements OnModuleInit, OnModuleDestroy { // 构建通知选项 const notificationOptions: any = {}; - // 设置 APS 属性 - const aps: any = {}; + // 设置 alert 属性 - 这是必需的 + if (options.title || options.body) { + notificationOptions.alert = { + title: options.title || '', + body: options.body || '' + }; + } else if (options.alert) { + // 如果直接提供了 alert,使用它 + notificationOptions.alert = options.alert; + } if (options.badge !== undefined) { notificationOptions.badge = options.badge; @@ -333,9 +341,43 @@ export class ApnsProvider implements OnModuleInit, OnModuleDestroy { */ private createDeviceNotification(notification: Notification, deviceToken: string): Notification { // 创建新的通知实例,使用相同的选项但不同的设备令牌 - return new Notification(deviceToken, { - alert: notification.options.alert, - }); + // 复制所有通知选项 + const options: any = {}; + + if (notification.options.alert) { + options.alert = notification.options.alert; + } + + if (notification.options.badge !== undefined) { + options.badge = notification.options.badge; + } + + if (notification.options.sound) { + options.sound = notification.options.sound; + } + + if (notification.options.contentAvailable) { + options.contentAvailable = notification.options.contentAvailable; + } + + if (notification.options.mutableContent) { + options.mutableContent = notification.options.mutableContent; + } + + if (notification.options.priority) { + options.priority = notification.options.priority; + } + + if (notification.options.type) { + options.type = notification.options.type; + } + + // 复制自定义数据 + if (notification.options.data) { + options.data = notification.options.data; + } + + return new Notification(deviceToken, options); } /** diff --git a/src/push-notifications/push-notifications.service.ts b/src/push-notifications/push-notifications.service.ts index b4b8e22..a794708 100644 --- a/src/push-notifications/push-notifications.service.ts +++ b/src/push-notifications/push-notifications.service.ts @@ -677,14 +677,18 @@ export class PushNotificationsService { // 创建APNs通知 const apnsNotification = this.apnsProvider.createNotification({ - alert: notificationData.title, title: notificationData.title, body: notificationData.body, data: notificationData.payload, pushType: notificationData.pushType, + priority: notificationData.priority, + expiry: notificationData.expiry, + collapseId: notificationData.collapseId, topic: this.bundleId, sound: notificationData.sound, badge: notificationData.badge, + mutableContent: notificationData.mutableContent, + contentAvailable: notificationData.contentAvailable, }); this.logger.log(`apnsNotification: ${JSON.stringify(apnsNotification, null, 2)}`);