如何在 iOS 上收到推送通知时显示空徽章?

问题描述 投票:0回答:1

我们要求在收到推送通知时在 iOS 应用程序图标上显示空徽章。

据我了解,将徽章计数设置为-1将显示一个空徽章(但是这个花絮来自其他帖子;我在官方文档中找不到它)。

我们已经在使用具有嵌入式通知服务扩展的 SDK。我们尝试过的:

  1. didReceiveNotificationRequest
    中捕获请求对象(在将其发送到 SDK 的通知服务之前...或者我们是这么认为的?)。从请求对象复制
    content
    ,更新徽章值,使用原始对象标识符和触发器以及新内容创建一个新的请求对象,并将其发送到通知服务。 这会使通知服务崩溃,导致丰富的推送作为没有图像的标准推送进行传递。
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    self.baseExtension = [[ITBNotificationServiceExtension alloc] init];

    UNMutableNotificationContent *requestWithBadgeCount = request.mutableCopy;
    NSNumber *emptyBadgeCount = [NSNumber numberWithInt:-1];
    requestWithBadgeCount.badge = emptyBadgeCount;
    UNNotificationRequest *updatedRequest = [UNNotificationRequest requestWithIdentifier:request.identifier content:requestWithBadgeCount trigger:request.trigger];

    [self.baseExtension didReceiveNotificationRequest:updatedRequest withContentHandler:contentHandler];
}

- (void)serviceExtensionTimeWillExpire {
    [self.baseExtension serviceExtensionTimeWillExpire];
}
@end
  1. 通过更新
    bestAttempCount.badge
    来修补 SDK 的通知服务。这没有影响,但没有使服务崩溃。
  2. 通过调用
    UNUserNotificationCenter.current().setBadgeCount()
    (包含在 iOS 16+ 的防护中)来修补 SDK 的通知服务。这有效,但仅限于正整数。设置 -1 没有效果。

我们是不是找错树了?

ios swift push-notification apple-push-notifications badge
1个回答
0
投票

iOS 中不存在“空徽章”的概念。

不过,将徽章的值设置为 0 将确保不显示徽章。

{
"aps": {
    "alert": {
        "title": "Notification Title",
        "body": "Notification Body"
    },
    "badge": 0
}

}

© www.soinside.com 2019 - 2024. All rights reserved.