2017-09-01 75 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條款需要編寫。