Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 374
Santhosh S Kashyap Avatar asked Jul 12 '17 12:07

Santhosh S Kashyap


People also ask

Do push notifications work when app is closed iOS?

Apple does not offer a way to handle a notification that arrives when your app is closed (i.e. when the user has fully quit the application or the OS had decided to kill it while it is in the background). If this happens, the only way to handle the notification is to wait until it is opened by the user.

Do local notifications work when app is closed?

Current Behavior. A local notification is schedule at a given time. The notification only happens if the app is open or in the foreground. If the app is closed the notification does not happen (no notification card).

Does an app need to be open for push notifications?

Push notifications are small, pop-up messages sent to a user's device by a mobile app that appear even when the app isn't open. These notifications are designed to grab attention and can convey reminders, updates, promotions, and more. Push notifications can consist of a title, a message, an image, and a URL.

How do I see notifications on closed iPhone?

Notification Center shows your notifications history, allowing you to scroll back and see what you've missed. There are two ways to see your alerts from the Notification Center: From the Lock Screen, swipe up from the middle of the screen. From any other screen, swipe down from the center of the top of your screen.


2 Answers

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
    }

}
like image 184
Prakash Shaiva Avatar answered Sep 21 '22 06:09

Prakash Shaiva


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)
        }
}
like image 36
Napster Scofield Avatar answered Sep 21 '22 06:09

Napster Scofield