0

I have several notifications on my app. What i want to do is to, when a local notification arrived, it erases another previous already showed one from notification center.

I am using the the following code.

static func unscheduleNotification(userInfoValue: String){
    if let notifications = UIApplication.sharedApplication().scheduledLocalNotifications{
        for notification in notifications{
            if let userInfo = notification.userInfo as? [String:String]{
                if userInfo["info"] == userInfoValue{
                    UIApplication.sharedApplication().cancelLocalNotification(notification)                        
                }
            }
        }
    }
}

Would appreciate if someone point me out the right direction here. If that is even possible.

Diogo Aleixo
  • 841
  • 1
  • 8
  • 20

2 Answers2

0

I found the solution. Or one solution. Adding one repetition interval to the notification allows with the method i was using and posted on the question to delete it.

I don´t know if it is the correct procedure or not but it is indeed working.

Diogo Aleixo
  • 841
  • 1
  • 8
  • 20
0

If you want to receive 1 notification when getting another 1, then best idea to keep you 1st notification object in NSUserDefault, when getting another 1 notification, fetch already stored notification from NSUserDefault then remove it.

Consider example if your device is restarted after receiving 1st notification. then it will be in notification tray but you would not get object to cancel it.

Also scheduledLocalNotifications will not provide object after restating your device. scheduledLocalNotifications doesn't work properly always with iOS 8.

Refer - iOS 8 [UIApplication sharedApplication].scheduledLocalNotifications empty

While storing in NSUserDefault you have to convert notification object to NSData

let data = NSKeyedArchiver.archivedDataWithRootObject(notificationObject)

At the time of getting from NSUserDefault it will be NSData

So again convert to object

 let notificationObject : UILocalNotification = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? UILocalNotification
Community
  • 1
  • 1
Hasya
  • 9,792
  • 4
  • 31
  • 46