Reputation: 701
Helo friends
I created a code to push nofication, but the notification are silient, I need to use sound, On this line I specify the types:
let notificationTypes = UIUserNotificationType.Alert
But dont work. Another dount, How can I use Badge to add number in app icon?
this is my code:
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)
}
Upvotes: 1
Views: 96
Reputation: 1242
You didn't request enough permissions. You only requested .Alert
, but .Sound
and .Badge
should all be included. Change:
let notificationTypes = UIUserNotificationType.Alert
to:
let notificationTypes: UIUserNotificationType = [.Alert, .Sound, .Badge]
To add a badge number, use:
localNotification.applicationIconBadgeNumber = 1
Upvotes: 2