0

Im posting a notification in iOS, I dont want to repeat the notification code i have used is.

let notification = UILocalNotification()
notification.timeZone = NSTimeZone.local
//calculation of dateTime
notification.fireDate = dateTime as Date?


notification.repeatInterval = 0

notification.alertBody = "Body"
notification.userInfo = ["title": "Notify", "type": "title", "5436" : "Notify"]

In that condition my notification was not firing. When I give like

notification.repeatInterval = NSCalendar.Unit.day

It is firing.. Is this a feature or issue? How can i post a notification without repeat interval

nayem
  • 7,285
  • 1
  • 33
  • 51
Saranjith
  • 11,242
  • 5
  • 69
  • 122

1 Answers1

2

Soultion 1: You can use dispatch_once:

static var token: dispatch_once_t = 0

dispatch_once(&token) {
  NSLog("Do it once")
}

Solution 2:

Taken from Apple documentation:

If you assign an calendar unit such as weekly (NSWeekCalendarUnit) or yearly (NSYearCalendarUnit), the system reschedules the notification for delivery at the specified interval. The default value is 0, which means don't repeat.

I assume you want to cancel the notification with the repeating interval, you can do that in two ways:

Cancel all the notifications.

Cancel only that notification.

The first option is easy, use:

cancelAllLocalNotifications In your app delegate.

The second one requires more work. You need to go through the pending notifications (use scheduledLocalNotifications) and cancel the notification.

In order to know which notifications is going to be canceled you can use the userInfo property when you set the notification. For example set an unique ID for every notification so when you cancel that notification you simply compare that ID with all the IDs in your scheduledLocalNotifications array.

  • Can you explain dispatch_once method.. i m not getting you – Saranjith Jun 07 '17 at 06:16
  • of posting notification right? what should be value of repeat interval? – Saranjith Jun 07 '17 at 06:48
  • @Saranjith make it as it is just add this line UIApplication.sharedApplication().cancelAllLocalNotifications() after your notification.repeatInterval = NSCalendar.Unit.day –  Jun 07 '17 at 06:52