gpt4 book ai didi

ios - 通过触摸应用程序图标启动应用程序时,应用程序图标角标(Badge)编号不会更改为零

转载 作者:行者123 更新时间:2023-11-28 08:50:44 25 4
gpt4 key购买 nike

我正在开发本地通知代码。我能够根据触发的通知数量显示角标(Badge)编号,并且当用户打开通知时我能够将所有图标设置为 0,但是如果用户启动应用程序,角标(Badge)未设置为 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;


}

现在在 objective-c 中,我通常这样做是为了在应用程序启动时清除角标(Badge) -

- (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 中做到这一点?

最佳答案

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

因此,如果您需要在所有情况下启动后立即将 applicationIconBadgeNumber 设置为 0,则无需检查 UIApplicationLaunchOptionsLocalNotificationKey:

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

}

关于ios - 通过触摸应用程序图标启动应用程序时,应用程序图标角标(Badge)编号不会更改为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com