2017-09-01 74 views
0

我一直在尝试通知编码只是为了能够在一定添加到我的应用程序,但是最大的问题是,我可以说,在swift 3中更新定时通知中的徽章?

something.badge = 3

和这样的,但我不能说

something.badge + = 1

something.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 
} 

这里的问题是,我一直在说,如果工号是特定号码,使它其他具体的数字....有没有其他方式呢?

+0

当你说“我不能说'something.badge + = 1'”你是什么意思,你不能'说'它? – lloyd

+0

给我们尝试它时得到的错误。 – Digits

回答

0

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

如何:

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

然后就没if条款需要编写。