【问题标题】:How to delete a local notification in iPhone如何在 iPhone 中删除本地通知
【发布时间】:2011-03-23 06:59:42
【问题描述】:

我正在制作一个设置本地通知的应用程序。

谢天谢地,我能够设置本地通知,但我不知道如何删除我的应用程序设置的通知。

XCode 确实提供了使用removeAllNotifications 删除的功能,但您不能删除应用程序设置的特定通知。

非常感谢。

【问题讨论】:

标签: iphone


【解决方案1】:

如果您使用的是自 iOS 10.0 起可用的 User Notifications 框架,则您提供的标识符为 foo

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "foo")
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "foo")

或者在 Objective-C 中:

[[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"foo"]];
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"foo"]];

【讨论】:

    【解决方案2】:

    这个问题你问了两次,所以我回答了这两个问题,希望能传达给你:

    使用此代码取消所有本地通知:

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

    用这行代码取消一个本地通知:

    [[UIApplication sharedApplication] cancelLocalNotification:theNotification];
    

    其中theNotificationUILocalNotification 对象,因此要取消特定通知,您需要保留它的UILocalNotification


    您可以在apple's documentation找到更多内容。

    【讨论】:

    • 嗨,Thomas,您会在哪里保存或获取该通知对象?
    • 如果您不想取消所有通知,则必须在内存中某处保留您可能想要取消的 NSNotification。也许是一个适当类的属性,它包含一个 NSNotifications 数组。
    • thomas,我通过序列化 UILocalNotification 对象找到了答案
    【解决方案3】:

    你好,看,你可以快速创建一个本地通知:

    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 userInfo 是你本地通知的一个不定值,现在,如果你想删除一个特定的本地通知,请尝试:

    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;
                }
            }
    

    看上面的方法,userInfoCurrent 是你的本地通知的标识符,而 uitodelete 是一个字符串,其中包含你要删除的 locla 通知的特定键...

    aaaa...如果你想删除所有本地通知,你可以使用

    UIApplication.sharedApplication().cancelAllLocalNotifications()

    我希望你为自己或其他人提供这些信息..

    再见,请原谅我的英语不好

    【讨论】:

      【解决方案4】:

      您可以通过以下函数取消通知:[[UIApplication sharedApplication]cancelNotification:object Of your UILocalNotification]

      【讨论】:

      • 这并没有去掉应用程序图标上带有圆圈的 1。有没有办法解决这个问题?
      【解决方案5】:
      [[UIApplication sharedApplication] cancelLocalNotification:notification]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多