Мне нужно установить количество значков, когда приложение убито / принудительно завершено, я пробовал тихие уведомления (вы можете увидеть логику 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 независимо от того, активно ли приложение / скрыто / убито / завершено / и т. д., но не может найти, как, в некоторых темах в StackOverflow сказано, что WhatsUp / Gmail просто спамит статус «прочитано уведомление», но я не могунайти любую информацию о таком типе уведомления.