OmerN
OmerN

Reputation: 953

Local Notification Not Firing

I'm trying to use local notification, this is my code:

appdelegate

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Sound, UIUserNotificationType.Alert, UIUserNotificationType.Badge], categories: nil))    

notificationViewController

let localNotification:UILocalNotification = UILocalNotification()

var BDate = friend.birthday.componentsSeparatedByString("/")

let date = NSDate.date(year: 2015, month: Int(BDate[1])!, day: Int(BDate[0])! - daysBefore, hour: hour, minute: min, second: 0)
localNotification.soundName = "notificationSound.mp3"

localNotification.alertBody = friend.fullName + " has a birthday today!"

localNotification.fireDate = date
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = NSCalendarUnit.Year

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)    

friend.birthday is a string - "DD/MM/YYYY"

I'm calling the setNotification function for every friend in a friends array. When there are only one or two friends I get the notification but one there are ~100+ I no longer get the notification.

I know the fireDate is correct, I checked it.

Why the code isn't working?

Upvotes: 2

Views: 1227

Answers (2)

Andriy
Andriy

Reputation: 2796

Looks like you're really exceeded the notifications limit. There's several tips and guidelines that might help:

  1. Try to check your date versus NSDate() and schedule notification only if date.timeIntervalSinceReferenceDate > NSDate().timeIntervalSinceReferenceDate (There is a compare method in NSDate, but I prefer to compare raw values)
  2. Scheduling local notifications one-by-one is very expensive for application performance, so you can form an array of notifications and schedule them all at once. Use UIApplication.sharedApplication().scheduledLocalNotifications property for that purpose.
  3. This will allow you effectively reschedule all notifications on every application launch. For example in applicationDidFinishLaunching() delegate method.
  4. And also, this will give you another advantage: you can easily check for notifications limit and, if necessary, add/replace 64-th notification with prompt to user to launch your app to allow it to schedule more notifications. This is a common practice for many applications to deal with Apple's limitations.

Upvotes: 1

Rinku
Rinku

Reputation: 920

Each app on a device is limited to 64 scheduled local notifications. The system discards scheduled notifications in excess of this limit, keeping only the 64 notifications that will fire the soonest. Recurring notifications are treated as a single notification.

You can find more detail here

Upvotes: 3

Related Questions