Reputation: 765
Well, none of the existing discussions presented a working solution for me. So here's my code to show local notifications. What am I missing?
let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate(timeIntervalSinceNow: 5)
print("Notification scheduled")
notification.userInfo = ["title": "Test", "UUID": "1"]
notification.repeatInterval = NSCalendarUnit.WeekOfMonth
UIApplication.sharedApplication().scheduleLocalNotification(notification)
I'm aware that when the app is in foreground there should be an event in didReceiveLocalNotification. I do not get an event in the appdelegate's didReceiveLocalNotification or the notification. However, when I use presentLocalNotificationNow with the notification - I the didReceiveLocalNotification in app delegate does get called. I also tried with other fireDates but it doesn't work. What am I missing?
Oh and I do have the below code in my appdelegate didfinishlaunchingapplication
let application = UIApplication.sharedApplication()
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
Upvotes: 0
Views: 667
Reputation: 527
So I also encountered a same problem. I was trying to fire UILocalNotification from background fetch. I had UILocalNotification scheduled, I stepped through in Xcode, make sure that scheduleLocalNotification was called, but it only fired about 1/5 times.
It seems that scheduling UINotification from background fetch simply does not work reliably in iOS 10. I switched to using UNUserNotification and it fires reliably, every time I step through the code.
Upvotes: 0
Reputation: 1033
To send notification in iOS 10 you need to ask permission as bellow before you fire the notification.
func requestPermition() {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if settings.authorizationStatus != .authorized {
let options: UNAuthorizationOptions = [.alert, .sound];
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
print("Something went wrong")
}else {
print("permition granted")
}
}
}
}
}
UILocalNotification
is deprecated in iOS 10 you may use UNUserNotificationCenter
instead.to fire a notification try bellow code
//to fire notification after 5 seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5,
repeats: false)
//Set contents to be desplayed on the notification
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "Test reminder"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
//set UNNotificationActionOptions .foreground if you need to open when button tapped.
let action = UNNotificationAction(identifier: "remindOpen", title: "Open", options: [.foreground])
let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("notification error: \(error)")
}
}
Upvotes: 1
Reputation: 20804
I had been testing, and this are my results, the problem seems to be related with time unit weekOfMonth
with day
works as intended, here is the picture of console log, using XCode 8.2, device iOS 10.2, still working
func scheduleTestNotification()
{
let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName
notification.timeZone = NSTimeZone.default
notification.fireDate = Date(timeIntervalSinceNow: 5)
print("Notification scheduled")
notification.userInfo = ["title": "Test", "UUID": "1"]
notification.repeatInterval = NSCalendar.Unit.day
UIApplication.shared.scheduleLocalNotification(notification)
debugPrint(UIApplication.shared.scheduledLocalNotifications!)
}
but if I use weekOfMonth
then
I think you can use this code as a workaround, it's not the best solution, but maybe can help you to achieve what you need
func scheduleTestNotification()
{
let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName
notification.timeZone = NSTimeZone.default
notification.fireDate = Date(timeIntervalSinceNow: 5)
print("Notification scheduled")
notification.userInfo = ["title": "Test", "UUID": "1"]
UIApplication.shared.scheduleLocalNotification(notification)
notification.repeatInterval = NSCalendar.Unit.day
notification.fireDate = Calendar.current.date(byAdding: .day, value: 7, to: notification.fireDate!)
UIApplication.shared.scheduleLocalNotification(notification)
debugPrint(UIApplication.shared.scheduledLocalNotifications!)
}
I hope this helps you
Upvotes: 2
Reputation: 1111
I tested and this code works on my side, iOS 10 with XCode 8.2
let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert ], categories: nil)
UIApplication.shared.registerUserNotificationSettings(settings)
let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = Date(timeIntervalSinceNow: 5)
print("Notification scheduled")
notification.userInfo = ["title": "Test", "UUID": "1"]
notification.repeatInterval = NSCalendar.Unit.minute
UIApplication.shared.cancelAllLocalNotifications()
UIApplication.shared.scheduleLocalNotification(notification)
The method didReceiveNotification is called whenever the app pushes a local notification. My repeatInterval is minute instead of weekOfMonth. I think weekOfMonth is the problem because when I change repeatInterval to weekOfMonth, I did not receive notifications, the method didReceiveNotification never called.
Upvotes: 0