Cancel UILocalNotification

后端 未结 10 918
余生分开走
余生分开走 2020-11-27 12:53

I have a problem with my UILocalNotification.

I am scheduling the notification with my method.

- (void) sendNewNoteLocalReminder:(NSDate *)date  a         


        
10条回答
  •  失恋的感觉
    2020-11-27 13:26

    In swift, you first add a dict to notifications userInfo by:

    let dict:NSDictionary = ["ID" : "someString"]
    notification.userInfo = dict as! [String : String]
    

    Then, you want to get an array of existing notifications (2), and iterate through each one (3) until you find the one you're looking for. Once you find it, cancel it (4).

    // 1. Pass in the string you want to cancel
    func cancelLocalNotification(uniqueId: String){
    
        // 2. Create an array of notifications, ensuring to use `if let` so it fails gracefully
        if let notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications {
    
            // 3. For each notification in the array ...
            for notif in notifyArray as [UILocalNotification] {
                // ... try to cast the notification to the dictionary object
                if let info = notif.userInfo as? [String: String] {
    
                    // 4. If the dictionary object ID is equal to the string you passed in ...
                    if info["ID"] == uniqueId {
                        // ... cancel the current notification
                        UIApplication.sharedApplication().cancelLocalNotification(notif)
                    }
                }
            }
        }
    }
    

提交回复
热议问题