How to delete a local notification in iPhone

后端 未结 4 1705
一向
一向 2020-12-08 11:07

I am making an application that sets a local notification.

Thankfully I was able to set the local notification but I don\'t know how to delete the notification which

相关标签:
4条回答
  • 2020-12-08 11:40

    You can cancel notification by following function:[[UIApplication sharedApplication]cancelNotification:object Of your UILocalNotification]

    0 讨论(0)
  • 2020-12-08 11:46
    [[UIApplication sharedApplication] cancelLocalNotification:notification]
    
    0 讨论(0)
  • 2020-12-08 11:54

    helloou, look, in swift you can create a local notification :

    var notif = UILocalNotification()
            notif.timeZone = NSTimeZone.defaultTimeZone()
    
            let morningOfChristmasComponents = NSDateComponents()
            morningOfChristmasComponents.year = 2016
            morningOfChristmasComponents.month = 03
            morningOfChristmasComponents.day = 30
            morningOfChristmasComponents.hour = 15
            morningOfChristmasComponents.minute = 59
            morningOfChristmasComponents.second = 0
    
            let morningOfChristmas = NSCalendar.currentCalendar().dateFromComponents(morningOfChristmasComponents)!
    
            let formatter = NSDateFormatter()
            formatter.dateStyle = NSDateFormatterStyle.LongStyle
            formatter.timeStyle = .MediumStyle
    
            let dateString = formatter.stringFromDate(morningOfChristmas)
    
            notif.fireDate = morningOfChristmas
            notif.alertBody = "alarma wolf"
            notif.userInfo = ["identificador": "wolf"]
            UIApplication.sharedApplication().scheduleLocalNotification(notif)
            print("alarma fijada para \(dateString)")
    

    loo the userInfo is a indeitifer to you local notification, now, if you want delete a specific local notificaction please try:

    var uidtodelete = "wolf"
            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["identificador"]! as! String
                if uid == uidtodelete {
                    //Cancelling local notification
                    app.cancelLocalNotification(notification)
                    break;
                }
            }
    

    look the method, above, userInfoCurrent is the identificator of your's local notification, and uitodelete is a string that contain the specific key of the locla notification that you want delete...

    aaaa... if you want to delete all local notification you can use

    UIApplication.sharedApplication().cancelAllLocalNotifications()

    oki

    I hope you serve yourself or someone else this information..

    good bye, andforgive my bad English

    0 讨论(0)
  • 2020-12-08 12:03

    You asked this question twice, so I'm answering on both questions in the hopes of it reaching you:

    Cancel all local notifications with this code:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

    Cancel one local notification with this line of code:

    [[UIApplication sharedApplication] cancelLocalNotification:theNotification];
    

    where theNotification is a UILocalNotification object, so in order to cancel a specific notification you need to hold on to it's UILocalNotification.


    You can find more stuff in apple's documentation.

    0 讨论(0)
提交回复
热议问题