我正在开发本地通知代码。我能够根据发出的通知数显示徽章编号,并且当用户打开通知时我能够将所有图标设置为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)
}
在我的appdelagate课程中
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中做到这一点?
答案 0 :(得分:0)
applicationIconBadgeNumber没问题。问题在于本地通知。 addobserver和name to localnotification
答案 1 :(得分:0)
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
}
答案 2 :(得分:0)
您必须考虑是否在不单击通知的情况下打开应用程序将使用户看到他具有可用的通知内容。如果他有很多呢?
如果用户可以一次查看所有通知的内容或事件,则可以在每次应用程序激活时将applicationIconBadgeNumber设置为0,或者仅当用户单击或查看内容时才可以将其降低
因此,我将在applicationDidBecomeActive上执行此操作,因此如果应用程序在后台运行,它也会将其设置为0。
func applicationDidBecomeActive(_ application: UIApplication) {
application.applicationIconBadgeNumber = 0
}