在swift 3中更新定时通知中的徽章?

时间:2017-09-01 21:01:41

标签: ios swift notifications badge

我一直在尝试通知编码只是为了能够为我的应用添加一些,但是最重要的问题是,我可以说,

  

something.badge = 3

等等,但我不能说

  

something.badge + = 1

  

something.badge = badge +1

到目前为止,我的代码就像这样

@IBAction func sendNotification(_ sender: Any) {// in this button you make a notification

      UIApplication.shared.applicationIconBadgeNumber = 1
    let asnwer1 = UNNotificationAction(identifier: "asnwer1",  title: "You are",  options: UNNotificationActionOptions.foreground)
    let asnwer2 = UNNotificationAction(identifier: "asnwer2", title: "Obviously you fucking are!" , options: UNNotificationActionOptions.foreground)
    let category = UNNotificationCategory(identifier: "myCategory", actions: [asnwer1, asnwer2], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])
   // created nitification

    let content = UNMutableNotificationContent()
    content.title = "Are you pathetic for not working out??"
    content.subtitle = "are you?? "
    content.body = "Are you sure?"
    content.categoryIdentifier = "myCategory"
    /////// THIS NEEDS IMPROVEMENT
    if  UIApplication.shared.applicationIconBadgeNumber == 1{
            content.badge = 2
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 2{
        content.badge = 3
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 3{
        content.badge = 4
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 4{
        content.badge = 5
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 5{
        content.badge = 6
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 6{
        content.badge = 7
    }
    if  UIApplication.shared.applicationIconBadgeNumber == 7{
        content.badge = 8
    }
    if  UIApplication.shared.applicationIconBadgeNumber >= 8{
        content.badge = 9
    }


    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
    let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)// the previous lne created a let named request...youre using it here
}

这里的问题是,我继续说,如果徽章编号是那个特定的编号,那就把它换成其他特定的编号....还有其他的方法吗?

1 个答案:

答案 0 :(得分:1)

我假设您想简化如何编写所有这些条件。

怎么样:

content.badge = Swift.min(9, UIApplication.shared.applicationIconBadgeNumber + 1)

然后不需要写if个句子。