Ios 当应用程序位于前台时,从推送通知更新选项卡栏徽章计数
我的TabBarController中有一个通知选项卡,我希望显示与应用程序徽章计数相同的徽章计数。我让它在所有情况下都能工作,除了应用程序在前台的情况 为此,我为TabBarController创建了一个自定义类,该类注册UIApplicationWillEnterForeground通知,并将通知选项卡的标记设置为与应用程序标记相等。我也在OnLoad()函数中这样做 现在,当应用程序已经在前台时,如何更新通知选项卡徽章?我在AppDelegate函数中捕获通知:Ios 当应用程序位于前台时,从推送通知更新选项卡栏徽章计数,ios,swift,uitabbarcontroller,Ios,Swift,Uitabbarcontroller,我的TabBarController中有一个通知选项卡,我希望显示与应用程序徽章计数相同的徽章计数。我让它在所有情况下都能工作,除了应用程序在前台的情况 为此,我为TabBarController创建了一个自定义类,该类注册UIApplicationWillEnterForeground通知,并将通知选项卡的标记设置为与应用程序标记相等。我也在OnLoad()函数中这样做 现在,当应用程序已经在前台时,如何更新通知选项卡徽章?我在AppDelegate函数中捕获通知: applicati
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
我知道可以在rootViewController中查找选项卡栏控制器,但my root controller是应用程序的my入口点,用于检查用户在密钥链中是否有登录令牌,并转到登录视图控制器或选项卡栏控制器。所以考虑到它的分段,我不确定这是一个选项
我的UI是否可以注册“推送通知”通知,就像我注册接收UIApplicationWillEnterForeground通知一样?这将是理想的,但我还没有找到这样的通知
任何建议或提示将不胜感激 在
AppDelegate
中发布来自DidReceiveMemotentification
的通知,并在自定义TabBarController
中添加一个观察者,以监听和更新UI,如下所示
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
NotificationCenter.default.post(Notification(name: Notification.Name("didReceiveNotification")))
}
}
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(updateBadgeCount),
name: Notification.Name(rawValue: "didReceiveNotification"),
object: nil
)
}
@objc private func updateBadgeCount() {
// Update badge count
}
deinit {
NotificationCenter.default.removeObserver(self)
}
}
非常感谢。我在四处寻找内置的通知类型。我不想检查我是否可以发布自己的定制通知。这很好用!