I am developing an iPhone alarm app based on local notifications.
On deleting an alarm, the related local notification should get cancelled. But how can I determine exactly which object from the array of local notifications is to be cancelled?
I am aware of [[UIApplication sharedApplication] cancelLocalNotification:notification]
method but how can I get this 'notification' to cancel it?
swift 3-style:
}
for iOS 10 use:
You can save a unique value for key in your local notification's userinfo. Get all local notification, loop through the array and delete the particular notification.
Code as follows,
OBJ-C:
SWIFT:
UserNotification:
If you use UserNotification (iOS 10+), just follow this steps:
When creating the UserNotification content, add an unique identifier
Remove specific pending notification using removePendingNotificationRequests(withIdentifiers:)
Remove specific delivered notification using removeDeliveredNotifications(withIdentifiers:)
For more info, UNUserNotificationCenter
Swift 4 solution:
Other Option:
First of All, when you create local notification, you can store it in user defaults for future use, Local notification object can not be stored directly in user defaults, This object needs to be converted into NSData object first, and then
NSData
can be stored intoUser defaults
. Below is code for that:After you have stored and scheduled local notification, In future, requirement may arise that you need to cancel any of notification that you created earlier, So you can retrieve it from User defaults.
Hope This helps
For Repeated Reminders ( For example you want your alarm to fire on Sun, Sat and Wed at 4 PM , Then you have to make 3 alarms and set repeatInterval to NSWeekCalendarUnit ).
For making Once Only Reminder :
For Making Repeated Reminder :
For Filtering you array to display it.
To remove Reminder even it was Once Only or Repeated :
You can keep a string with the category identifier when scheduling the notification like so
and search for it and cancel when needed like so