Amrit Tiwari Amrit Tiwari - 1 year ago 98
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")
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 {

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download