Delete a particular local notification

unusernotificationcenter remove notification
ios local notification
delete push notification ios swift
ios local notification at specific time swift
ios schedule local notification
remove notification swift
ios local notification when app is in background
cancel all local notifications swift

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?

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:

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
    if ([uid isEqualToString:uidtodelete])
    {
        //Cancelling local notification
        [app cancelLocalNotification:oneEvent];
        break;
    }
}

SWIFT:

var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications {
    var notification = oneEvent as UILocalNotification
    let userInfoCurrent = notification.userInfo! as [String:AnyObject]
    let uid = userInfoCurrent["uid"]! as String
    if uid == uidtodelete {
        //Cancelling local notification
        app.cancelLocalNotification(notification)
        break;
    }
}

UserNotification:

If you use UserNotification (iOS 10+), just follow this steps:

  1. When creating the UserNotification content, add an unique identifier

  2. Remove specific pending notification using removePendingNotificationRequests(withIdentifiers:)

  3. Remove specific delivered notification using removeDeliveredNotifications(withIdentifiers:)

For more info, UNUserNotificationCenter

objective c - Delete a particular local notification, objective c - Delete a particular local notification. I am developing an iPhone alarm app based on local notifications. On deleting an alarm, the related local  On the “Settings” menu, tap the “Sound & Notification” option, and then scroll down until you see the “App notifications” entry. Tap that. Tap each app to see its notification options. To disable notifications for an app, switch the “Block All” toggle the on position.

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 into User defaults. Below is code for that:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:localNotif];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:[NSString  stringWithFormat:@"%d",indexPath.row]];

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.

NSData *data= [[NSUserDefaults standardUserDefaults] objectForKey:[NSString   stringWithFormat:@"%d",UniqueKey]];

UILocalNotification *localNotif = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"Remove localnotification  are %@", localNotif);
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:[NSString stringWithFormat:@"%d",UniqueKey]];

Hope This helps

removePendingNotificationRequests(withIdentifiers , Unschedules the specified notification requests. SDKs. iOS 10.0+; macOS Discussion. This method executes asynchronously, removing the pending notification requests on a secondary thread. Schedules a local notification for delivery. Here's how to turn off notifications and manage alerts. symbols for each type of notification to remove install the particular app you want to view notifications for if it’s not already

Here is what i do.

When creating your notification do this:

  // Create the notification

UILocalNotification *notification = [[UILocalNotification alloc]  init] ;



notification.fireDate = alertDate;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertAction = NSLocalizedString(@"Start", @"Start");
notification.alertBody = **notificationTitle**;
notification.repeatInterval= NSMinuteCalendarUnit;

notification.soundName=UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;

[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

when trying to delete it do this:

 NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;

for (UILocalNotification *localNotification in arrayOfLocalNotifications) {

    if ([localNotification.alertBody isEqualToString:savedTitle]) {
        NSLog(@"the notification this is canceld is %@", localNotification.alertBody);

        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system

    }

}

This solution should work for multiple notifications, and your not managing any arrays or dictionaries or user defaults. Your simply using the data you've already saved to the systems notification database.

Hope this helps future designers and developers.

Happy coding guys! :D

removeDeliveredNotifications(withIdentifiers:), Removes the specified notification requests from Notification Center. returning immediately and removing the specified notifications on a background thread. Upon creating a local notification, use NSKeyedArchiver to store it as Data in UserDefaults. You can create a key equal to what you're saving in the notification's userInfo dictionary. If it's associated with a Core Data object, you could use its unique objectID property. Retrieve it with NSKeyedUnarchiver.

Scheduling and removeNotification in swift:

    static func scheduleNotification(notificationTitle:String, objectId:String) {

    var localNotification = UILocalNotification()
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 24*60*60)
    localNotification.alertBody = notificationTitle
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.applicationIconBadgeNumber = 1
    //play a sound
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertAction = "View"
    var infoDict :  Dictionary<String,String!> = ["objectId" : objectId]
    localNotification.userInfo = infoDict;

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
    static func removeNotification(objectId:String) {
    var app:UIApplication = UIApplication.sharedApplication()

    for event in app.scheduledLocalNotifications {
        var notification = event as! UILocalNotification
        var userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>
        var infoDict :  Dictionary = notification.userInfo as! Dictionary<String,String!>
        var notifcationObjectId : String = infoDict["objectId"]!

        if notifcationObjectId == objectId {
            app.cancelLocalNotification(notification)
        }
    }



}

Manage, Delete and Update Notifications in iOS 10 – Make App Pie, Besides adding notifications This lets you monitor, delete and update already Create new iOS single view project in Xcode. If you want the details of what I am doing here, check out the local notifications in iOS 10 post. Open Chrome → Settings → Site settings (under "Advanced") → Notifications and click on it. The following screen will appear If you turn off the Notifications slider, you will not get any notification from Chrome at all (from sites).

iOS Notes 37: How to cancel all & single notification?, You can cancel all local notifications at once or cancel just a single notification. removeAllDeliveredNotifications() // to remove all delivered notifications center. I'd like to ask for your help how to remove particular notification area icons (Windows 7 Professional). What I mean is not just "Show" nor "Hide". For example, I want to remove VLC Media Player and Microsoft Office OneNote from the list. Also, I don't want to restore the system icons because all icons will be removed.

How to Read and Cancel Local Notification Requests, To remove a single notification, simply provide a single identifier in the list. notificationCenter.removePendingNotificationRequests(withIdentifiers:  A cross platform plugin for displaying local notifications. Android API 16+ (4.1+, the minimum version supported by Flutter). Uses the NotificationCompat APIs so it can be run older Android devices. iOS 8.0+ (the minimum version supported by Flutter). Supports the old and new iOS notification APIs (the User Notifications Framework introduced in

Deletable iOS Push - Pushwoosh, Delete remote notifications from user's Notification Center. //if push is to delete notification "dlt":string". //load notif and //create local notification of the push. Before we begin, you can download the initial draft. The application is a table with a list of types of notifications. Now, if you tap on any line, Alert pops up with the name of the notification.

Implementing removable remote notifications on iOS, UNUserNotificationCenter has a method which allows us to remove delivered notifications with specified ids. It's easy when dealing with local notifications,  You should see a text box on the top, remove everything and copy & paste this string: Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer Right-click any blank space on the right side of the window under the entries, then navigate to New > DWORD (32-bit value).

Comments
  • @kingofBliss,can u please tell me to give there at "uidtodelete".because it is undeclared in my case.
  • @ishhh itsjust a strig value.. you should declare it and intialize it with a value of the uid to be delete
  • @kingofBliss,the uid is always showing null in NSLog.dont konw how to get rid of this.Please help me
  • @ishhh have you stored any value for uid in the userinfo dictionary when creating local notification? I think you have missed out that.
  • @kingofBliss, the "uid" it's a name of your own variable, you can use any significant name like "notificationID", and store that in a NSDictionary with the value of the id of the entity related to the UILocalNotification. Then set the notification.userInfo property to the dictionary with your custom data. Now when you get the notifications you can distinct them with that custom id or anything more you can need.
  • Thanks,I have implemented it by first way but your answer is also correct.I will take this into consideration.Can you please tell which one is more efficient?Thanks for the help:)
  • @Yogi: If you look at first answer, you need to run for loop everytime if you want to cancel local notification, but in the above answer, you will not need to run any for loop, you can directly access local notification and cancel that local notification and remove it from User defaults, As per my answer, it is more efficient way
  • @JigneshBrahmkhatri Your method is effective. But it will fail when the user uninstall the app, and reinstall it.
  • @KingofBliss, in that case we have to cancel all the notifications, right? So I guess this solution is faster. :)
  • @Sufian To cancel all notification there is a much faster way [[UIApplication sharedApplication] cancelAllLocalNotifications]; ;)
  • Thanks for sharing your answer but how this logic works if all of your notifications are having same body or if the body is to be taken from the user.In that case the user can give same body to multiple notifications.
  • @Yogi, like alertbody,you can check,notification.firedate to get the required notification. thanks to abhi for a simple solution.upvote 1 for u
  • @NAZIK:Thanks for your interest in discussion. But still the user can schedule two notifications on same fire date as it is an alarm application. At least it can be a test case for a tester and this solution looks to be failing there.