Ios 触摸应用程序图标启动应用程序时,应用程序图标徽章编号不会更改为零

Ios 触摸应用程序图标启动应用程序时,应用程序图标徽章编号不会更改为零,ios,swift,notifications,Ios,Swift,Notifications,我正在开发一个本地通知代码。我能够根据触发的通知数显示徽章编号,并且我能够在用户打开通知时将所有图标设置为0,但如果用户启动应用程序,则徽章不会设置为0。仅当用户打开通知时才将其设置为0。我读过多个答案和多篇文章,但找不到解决方案。为了注册我的本地通知,我有以下代码- func addItem(item: TodoItem) { // persist a representation of this todo item in NSUserDefaults

我正在开发一个本地通知代码。我能够根据触发的通知数显示徽章编号,并且我能够在用户打开通知时将所有图标设置为0,但如果用户启动应用程序,则徽章不会设置为0。仅当用户打开通知时才将其设置为0。我读过多个答案和多篇文章,但找不到解决方案。为了注册我的本地通知,我有以下代码-

func addItem(item: TodoItem)
    {
        // persist a representation of this todo item in NSUserDefaults

        var todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? Dictionary()// if todoItems hasn't been set in user defaults, initialize todoDictionary to an empty dictionary using nil-coalescing operator (??)

        todoDictionary[item.UUID] = ["deadline" : item.deadline, "title" : item.title, "UUID" : item.UUID]// store NSData representation of todo item in dictionary with UUID as key

        NSUserDefaults.standardUserDefaults().setObject(todoDictionary, forKey: ITEMS_KEY)

        let notification  = UILocalNotification()

        notification.alertBody = "Todo Item \"\(item.title)\" Is Overdue" // text that will be displayed in the notification
        notification.alertAction = "open" // text that is displayed after "slide to..." on the lock screen - defaults to "slide to view"
        notification.fireDate = item.deadline // todo item due date (when notification will be fired)
        notification.soundName = UILocalNotificationDefaultSoundName // play default sound
        notification.userInfo = ["title": item.title, "UUID": item.UUID] // assign a unique identifier to the notification so that we can retrieve it later
       let nextbadge = (UIApplication.sharedApplication().scheduledLocalNotifications?.count)! + 1
        notification.applicationIconBadgeNumber = nextbadge//here I provide an incremental badge number to my local notification.
        UIApplication.sharedApplication().scheduleLocalNotification(notification)


    }
在我的苹果课上

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
    {
        NSNotificationCenter.defaultCenter().postNotificationName("ToDoListShouldtRefresh", object: self)

         application.applicationIconBadgeNumber = 0;


    }
现在在目标C中,我通常这样做是为了在应用程序启动时清除徽章-

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Handle launching from a notification
    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (locationNotification) {
        // Set icon badge number to zero
        application.applicationIconBadgeNumber = 0;
    }

    return YES;
}
我试过这样的东西,但徒劳无功-

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {


        application.registerUserNotificationSettings(UIUserNotificationSettings (forTypes:[.Alert, .Badge, .Sound], categories: nil))
        if let notification:UILocalNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification
        {

            application.applicationIconBadgeNumber = 0
        }
        return true





    }

那么现在如何在SWIFT中实现这一点呢?

应用程序徽章号码没有问题。问题在于本地通知。将观察者和名称添加到localnotification

应用程序代码没有问题。问题在于本地通知。仅当用户通过通知打开应用程序时,才会设置本地通知的addobserver和name

UIApplicationLaunchActionsLocalNotificationKey

因此,如果在所有情况下都需要在启动后立即将ApplicationBudgeNumber设置为0,则无需检查UIApplicationLaunchActionSlocalNotificationKey

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    application.registerUserNotificationSettings(UIUserNotificationSettings (forTypes:[.Alert, .Badge, .Sound], categories: nil))
    application.applicationIconBadgeNumber = 0
    return true

}

UIApplicationLaunchActionSlocalNotificationKey仅当用户通过通知打开应用时才会设置

因此,如果在所有情况下都需要在启动后立即将ApplicationBudgeNumber设置为0,则无需检查UIApplicationLaunchActionSlocalNotificationKey

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    application.registerUserNotificationSettings(UIUserNotificationSettings (forTypes:[.Alert, .Badge, .Sound], categories: nil))
    application.applicationIconBadgeNumber = 0
    return true

}

您必须考虑,如果在不单击通知的情况下打开应用程序,将允许用户看到他已提供通知内容。如果他有很多呢

如果用户可以一次查看所有通知的内容或事件,则可以在每次应用激活时将applicationBadgeNumber设置为0,也可以仅在用户单击或查看内容时才将其设置为0

因此,我会在ApplicationIDBecomeActive上执行此操作,因此如果应用程序位于后台,它也会将其设置为0

func applicationdidebecomeactive(u应用程序:UIApplication){
application.applicationBadgeNumber=0

}

您必须考虑,如果在不单击通知的情况下打开应用程序,用户是否可以看到他已提供了通知内容。如果他有很多呢

如果用户可以一次查看所有通知的内容或事件,则可以在每次应用激活时将applicationBadgeNumber设置为0,也可以仅在用户单击或查看内容时才将其设置为0

因此,我会在ApplicationIDBecomeActive上执行此操作,因此如果应用程序位于后台,它也会将其设置为0

func applicationdidebecomeactive(u应用程序:UIApplication){
application.applicationBadgeNumber=0

}

对不起,你能再澄清一点吗?对不起,你能再澄清一点吗。