【问题标题】:IOS10: badge count not displayedIOS10:徽章计数不显示
【发布时间】:2016-10-21 07:52:45
【问题描述】:

我正在使用以下代码在我的应用图标上显示一个数字作为徽章:

func triggerNotification(iAmountToday: Int) {
  UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
    if granted {
      let content = UNMutableNotificationContent()
      content.badge = iAmountToday
      content.categoryIdentifier = "com.psv.localNotification"
      let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1.0, repeats: false)
      let request = UNNotificationRequest.init(identifier: "AmountTodayUpdate", content: content, trigger: trigger)

      let center = UNUserNotificationCenter.currentNotificationCenter()
      center.addNotificationRequest(request, withCompletionHandler: { (error) in
        if (error != nil) {
          print (error)
        }
      })
    }
  }
}

虽然没有抛出错误,但应用程序图标上永远不会显示徽章。

我做错了什么?

干杯

【问题讨论】:

  • 试试这个。 content.badge = iAmountToday 作为 NSNumber?还要检查 iAmountToday 是否有价值。
  • 嗨@Wolverine,我试过这个但没有效果。 iAmountToday 至少为 0 - 我也检查过
  • 尝试将其设置为至少 1
  • 不,没有帮助! :-(

标签: ios swift notifications badge


【解决方案1】:

Swift 3.0,iOS 10。

你应该替换:

 UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in

与:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in

这对我有用。

最好的问候 法比奥

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2014-07-29
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多