我需要在应用程序终止/强制退出时设置徽章计数,我尝试了静默通知(您可以在下面看到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只是垃圾邮件“读取通知”状态,但我找不到有关此类通知的任何信息。
答案 0 :(得分:0)
func applicationWillTerminate(_ application: UIApplication) {
// set your badge
}