Amrit Tiwari Amrit Tiwari - 3 months ago 20
Swift Question

All Notification is hidden when i clicked one of them

When push notification is clicked then push notification alert is hide from notification banner its okey but When i simply open the app not by clicking the notification, push notification auto clear, why? I haven't reset badge while launching my app.
And let's say that I have 5 notifications on my phone. If I open one of them all other notifications disappears. I want only the one clicked to disappear. Yes, I know this is default property of ios but i want to show the notification until user manually clicked or clear on it. I have handled like this

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print("this calls")
print(userInfo)//json
let aps = userInfo["aps"] as! [String: AnyObject]
let new = userInfo["news"] as! [String: AnyObject]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("navVC") as? navViewController

let state:UIApplicationState = application.applicationState
if (state == UIApplicationState.Inactive || state == UIApplicationState.Background) {
if let _ = aps["alert"] as? String,let newsId = new["news_id"] as? Int {

print(newsId)
NSUserDefaults.standardUserDefaults().setInteger(newsId, forKey: "K_Push_Id")
NSUserDefaults.standardUserDefaults().synchronize()
}

if let category_name = new["category_name"]{
print(category_name)
NSUserDefaults.standardUserDefaults().setObject(category_name, forKey: "category_name")
NSUserDefaults.standardUserDefaults().synchronize()
}
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
NSNotificationCenter.defaultCenter().postNotificationName("pushNotificationIdentifier", object: nil)
}
}

Answer

i have done like this in appdelegate

func applicationDidBecomeActive(application: UIApplication) {
         application.applicationIconBadgeNumber = 0;
    }

so every time if i open the app then its make applicationIconBadgeNumber to Zero and if applicationIconBadgeNumber is zero its clear all notification from notification center.