当应用程序在前台时更新来自推送通知的标签栏徽章计数
Updating a Tab Bar badge count from Push Notification when the app is in the foreground
我的 TabBarController 中有一个通知选项卡,我想显示与 App 徽章计数相同的徽章计数。除了应用程序在前台时,我可以在所有情况下使用它。
为此,我为我的 TabBarController 创建了一个自定义 class,它注册了 UIApplicationWillEnterForeground 通知,并将通知选项卡的标志设置为与 App 标志相同。我也在 OnLoad() 函数中执行此操作。
现在,当应用程序已经在前台时,我该如何更新通知标签徽章?我在 AppDelegate 函数中捕获通知:
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
我知道可能可以在 rootViewController 周围挖掘以找到选项卡栏控制器,但我的根控制器是我的应用程序入口点,它检查用户是否有钥匙串中的登录令牌,然后转到登录视图控制器或选项卡栏控制器。所以鉴于它 segues 我不确定这是一个选项吗?
我的 UI 是否可以注册 'push notification' 通知,就像我注册 UIApplicationWillEnterForeground 的通知一样?这将是理想的,但我还没有设法找到这样的通知。
如有任何建议或提示,我们将不胜感激!
Post didReceiveRemoteNotification
在 AppDelegate
中的通知,并在您的自定义 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)
}
}
我的 TabBarController 中有一个通知选项卡,我想显示与 App 徽章计数相同的徽章计数。除了应用程序在前台时,我可以在所有情况下使用它。
为此,我为我的 TabBarController 创建了一个自定义 class,它注册了 UIApplicationWillEnterForeground 通知,并将通知选项卡的标志设置为与 App 标志相同。我也在 OnLoad() 函数中执行此操作。
现在,当应用程序已经在前台时,我该如何更新通知标签徽章?我在 AppDelegate 函数中捕获通知:
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
我知道可能可以在 rootViewController 周围挖掘以找到选项卡栏控制器,但我的根控制器是我的应用程序入口点,它检查用户是否有钥匙串中的登录令牌,然后转到登录视图控制器或选项卡栏控制器。所以鉴于它 segues 我不确定这是一个选项吗?
我的 UI 是否可以注册 'push notification' 通知,就像我注册 UIApplicationWillEnterForeground 的通知一样?这将是理想的,但我还没有设法找到这样的通知。
如有任何建议或提示,我们将不胜感激!
Post didReceiveRemoteNotification
在 AppDelegate
中的通知,并在您的自定义 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)
}
}