gpt4 book ai didi

ios - 当应用程序被终止/强制退出时设置角标(Badge)计数

转载 作者:行者123 更新时间:2023-11-30 11:28:18 28 4
gpt4 key购买 nike

当应用程序被终止/强制退出时,我需要设置角标(Badge)计数,我尝试了静默通知(您可以在下面看到badge_reset逻辑),但它们仅在应用程序处于前台/后台时才有效。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

if let action = userInfo["action"] as? String {
switch action {
case "badge_reset":
if let badge = userInfo["badge"] as? String {
let badge: Int = (badge as NSString).integerValue;
if (badge >= 0) {
application.applicationIconBadgeNumber = badge;
} else {
application.applicationIconBadgeNumber = 0;
}
}
break;
default:
break;
}
} else {
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {

let title = alert["title"] as! String;
let body = alert["body"] as! String;
let link = userInfo["link"] as! String;

if (UIApplication.shared.applicationState == .active) {
if let controller = self.window?.rootViewController {
let image = UIImage(named: "Notification");
let announcement = Announcement(title: title, subtitle: body, image: image, duration: 10, action: {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "ShitstuffController") as? ViewController {
controller.loadViewIfNeeded();
controller.link = link;

self.window?.rootViewController = controller;
}
})

Whisper.show(shout: announcement, to: controller)
}
}

} else if let alert = aps["alert"] as? NSString {
//
}
}
}

completionHandler(UIBackgroundFetchResult.newData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let content = response.notification.request.content;

if let link = content.userInfo["link"] as? String {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "ShitstuffController") as? ViewController {

controller.loadViewIfNeeded();
controller.link = link;

self.window?.rootViewController = controller;
}
}

completionHandler();
}

我确信这是可能的,因为无论应用程序处于事件/隐藏/终止/退出/等状态,Gmail 都会更新角标(Badge)计数,但无法找到具体方法,StackOverflow 上的一些主题说 WhatsUp/Gmail 只是垃圾邮件“已读通知”状态,但我找不到有关此类通知的任何信息。

最佳答案

你尝试过这个方法吗:

func applicationWillTerminate(_ application: UIApplication) {
// set your badge
}

关于ios - 当应用程序被终止/强制退出时设置角标(Badge)计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50516596/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com