Helo朋友
我创建了一个推送nofication的代码,但通知很简单,我需要使用声音, 在这一行,我指定了类型:
let notificationTypes = UIUserNotificationType.Alert
但是不行。 另一个dount,我如何使用Badge在app图标中添加数字?
这是我的代码:
func setupNotificationSettings() {
let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
if (notificationSettings.types == UIUserNotificationType.None){
let notificationTypes = UIUserNotificationType.Alert
let modifyListAction = UIMutableUserNotificationAction()
modifyListAction.identifier = "editList"
modifyListAction.title = "Edit list"
modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground
modifyListAction.destructive = false
modifyListAction.authenticationRequired = true
let trashAction = UIMutableUserNotificationAction()
trashAction.identifier = "trashAction"
trashAction.title = "Delete list"
trashAction.activationMode = UIUserNotificationActivationMode.Background
trashAction.destructive = true
trashAction.authenticationRequired = true
let actionsArray = NSArray(objects: modifyListAction, trashAction)
let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)
/
let shoppingListReminderCategory = UIMutableUserNotificationCategory()
shoppingListReminderCategory.identifier = "shoppingListReminderCategory"
shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)
let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)
/
let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
}
}
func pushNotificationTest(){
let localNotification = UILocalNotification()
localNotification .fireDate = fixNotificationDate(datePicker.date) //usar com datepicker
localNotification.alertBody = "Test"
localNotification.alertAction = "Test test test"
localNotification.category = "shoppingListReminderCategory"
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
答案 0 :(得分:2)
您没有请求足够的权限。您仅请求.Alert
,但.Sound
和.Badge
都应包括在内。变化:
let notificationTypes = UIUserNotificationType.Alert
为:
let notificationTypes: UIUserNotificationType = [.Alert, .Sound, .Badge]
要添加徽章编号,请使用:
localNotification.applicationIconBadgeNumber = 1