How do I check my applicationIconBadgeNumber value

2020-04-17 08:02发布

I want my redDot to display when I have some value in badge, i.e UIApplication.shared.applicationIconBadgeNumber this is the code I wrote, but doesnt seem to work :

import UserNotifications

@IBOutlet weak var redDot: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    runCheck()

}

func runCheck(){
    if (UIApplication.shared.applicationIconBadgeNumber>=1) {
        self.redDot.alpha=1
    }
    else {
        self.redDot.alpha=0
    }
}

1条回答
可以哭但决不认输i
2楼-- · 2020-04-17 08:44

Based on our conversation in chat about this, you have three things you need to happen:

  1. You set up a notification to fire later with a badge number. But the notification will not automatically set the badge number.
  2. You have to handle the notification when it fires, check the badge number value and update your app's badge number explicitly
  3. You check the app badge number in your other view and show/hide the red dot.

I think you are doing #1 & #3 but not #2.

查看更多
登录 后发表回答