2016-06-13 108 views
0

我是iOS開發新手!我試圖用UILocalNotification創建一個應用程序。以下是我所做的:applicationIconBadgeNumber不更新或增加

func createLocalNotification(structure:Structure) { 

     let localNotification = UILocalNotification() 
     localNotification.category = "CATEGORY" 
     localNotification.alertBody = "\(structure.messageBody)" 
     localNotification.alertAction = "see" 
     localNotification.soundName = UILocalNotificationDefaultSoundName 
     localNotification.userInfo = ["uuid":structure.UUID] 
     localNotification.fireDate = structure.date 
     localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 

     print(localNotification) 

     UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 
} 

它運作良好!我的問題是applicationBadgeNumber。這property似乎有效。但在應用程序處於非活動狀態時不會增加。然後啓動一個應用程序Xcode給我這樣一個錯誤:

快照未呈現的視圖會導致一個空的快照。確保您的視圖在屏幕更新後的快照或快照之前已呈現至少一次。

什麼意思?什麼是錯的?謝謝你的幫助!

回答

1

由於我沒有足夠的重新評論來評論。當您的應用程序未運行時,您將能夠動態設置徽章號碼的唯一方法是使用推送通知。您必須跟蹤服務器端的更新

當您的應用處於後臺時,無法使用本地通知動態更新徽章號碼。您必須使用推送通知

1

您是否嘗試過使用SoundBadge設置註冊設置?

像這樣:(從this link截取的一部分)

@IBAction func registerLocal(sender: AnyObject) { 
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 
}