关于ios:删除应用并重新安装后,徽章计数仍然存在

Badge count is persisted after deleting an app and installing it again

我的应用程序中有一个通知部分,并且通知的总和以"应用程序图标"上的"徽章计数"形式显示。当用户访问通知时,徽章计数会减少。
假设用户现在卸载并重新安装了构建,即使用户尚未打开应用,徽章计数也会直接显示在应用图标上。此显示的徽章计数与卸载应用程序时的徽章计数相同。用户打开应用程序并访问通知部分后,徽章计数实际上应显示。


徽章计数由操作系统维护,与应用程序无关。卸载(删除)应用程序后,操作系统会保留一些值,包括标志计数。卸载应用程序后,不会调用任何开发人员方法或脚本。您将不得不接受此限制,或者更改应用程序的设计以重新考虑并克服此问题。


几天前,当我通过testFlight测试应用程序时,我遇到了同样的问题。

通常,当您删除该应用程序(它显示一些徽章编号)并再次重新安装时,可能会出现此问题。但是很难说出实际问题在哪里。

阅读Apple的官方文档说的话。

Resetting the Push Notifications Permissions Alert on iOS The first
time a push-enabled app registers for push notifications, iOS asks the
user if they wish to receive notifications for that app. Once the user
has responded to this alert it is not presented again unless the
device is restored or the app has been uninstalled for at least a day.

If you want to simulate a first-time run of your app, you can leave
the app uninstalled for a day. You can achieve the latter without
actually waiting a day by setting the system clock forward a day or
more, turning the device off completely, then turning the device back
on.

根据我的经验有一些建议:

  • 关闭您的通知表单设置
  • 并在服务器端将徽章设置为0,然后从设备中删除并重新安装您的应用。 -安装结束后再运行应用
  • 运行app agin后,请从设备中删除您的应用,然后在服务器1上重置徽章,然后在通知表单设置中将其重置。

不确定,但可能会为您工作。 :)


在didFinishLaunchingWithOptions

中执行以下代码

1
2
3
4
5
6
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"is_first_time"])
{        
 [application cancelAllLocalNotifications];
 // Restart the Local Notifications list
 application.applicationIconBadgeNumber = 0;
 [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"is_first_time"];  }

在didFinishLaunchingWithOptions部分中运行freshInstallationCheck函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    func freshInstallationCheck() {
        let defaults = UserDefaults.standard
        guard let currentAppVersion = Bundle.main.object(forInfoDictionaryKey:"CFBundleShortVersionString") as? String else { return }
        guard let previousVersion = defaults.string(forKey:"appVersion") else {
            // Key does not exist in UserDefaults, must be a fresh install
            print("fresh install")
            //  Writing version to UserDefaults for the first time
            defaults.set(currentAppVersion, forKey:"appVersion")
            // reinstall application, force to set icon to zero
            UIApplication.shared.applicationIconBadgeNumber = 0
            return
        }

        let comparisonResult = currentAppVersion.compare(previousVersion, options: .numeric, range: nil, locale: nil)
        switch comparisonResult {
        case .orderedSame:
            // nothing to do
            break
        case .orderedAscending, .orderedDescending:
            // new version update or downgrade

            break
        }

        // Updating new version to UserDefaults
        defaults.set(currentAppVersion, forKey:"appVersion")
    }