Santhosh S Kashyap
Santhosh S Kashyap

Reputation: 1068

IOS notification when Application is closed

i have a ios application where to which i send remote notification when sending notification from server i am setting content -available = 1 and sound = "" When on IOS device i am using

applicaiton(didReceiveRemoteNotification:fetchCompletionHandler:)

I seeing that app reaches this method when in background but when app is closed this method is not getting called But is not getting called when application is closed

applicaiton(didReceiveRemoteNotification:fetchCompletionHandler:)

My handler in this method is

        let userInfoDic = userInfo as NSDictionary as? [String: Any]
        let cato = userInfoDic?["cato"] as? String
if cato == "message" {
            let state: UIApplicationState = UIApplication.shared.applicationState
            if state == .active{
                print("Application is Active Socket IO will Handle This")
            }else{
                let apnMessage = userInfoDic?["apnMessage"] as? [String: Any]
                if (apnMessage == nil){
                    print("Error Fetching Message")
                }else{
                let count = UserDefaults.standard.integer(forKey:"TotalUnredMessage")

                    DispatchQueue.main.async {
                        UIApplication.shared.applicationIconBadgeNumber = count + 1
                    }
                    UserDefaults.standard.set(count, forKey: "TotalUnredMessage")
          }
}

So what should i change for this methods to run even when applicaiton is closed

Thanks

Upvotes: 14

Views: 21795

Answers (3)

Prakash Shaiva
Prakash Shaiva

Reputation: 1095

didReceiveRemoteNotification method will not call when the application is closed.

But you can check launchOptions to know weather the application has been launched from notification or not in didFinishLaunchingWithOptions method in appdelegate and do your task accordingly.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

     if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
    // Do your task here
    }

}

Upvotes: 12

Napster Scofield
Napster Scofield

Reputation: 1289

To complete Prakash Shaiva by showing how to retrieve the notification userIn didFinishWithLaunchOptions

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

     if let launchOptions = launchOptions,
            let userInfo =  launchOptions[.remoteNotification] as? [AnyHashable: Any] {
            //some code here and post when needed like below
            NotificationCenter.default.post(name: NSNotification.Name("your notification name"),  object: nil)
        }
}

Upvotes: 3

DoN1cK
DoN1cK

Reputation: 615

You should specify in configuration, that your app needs be waked on remote notification. Check this comment: https://stackoverflow.com/a/29231395/2732896

Upvotes: 1

Related Questions