0

I am working with Push notification and local notification also but in foreground notification working fine but when terminate app in this condition i can not able to redirect specific view when tap on notifications.

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  

         // Check if launched from notification
        // 1
        if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
            // 2
            let aps = notification["aps"] as! [String: AnyObject]
            //Redirect to notification view
            handlePushMessage(aps)
        }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? [String: AnyObject] {
            // 2
            self.postData = notification["aps"] as! [String: AnyObject]
            //Redirect to notification view
            didTapNotification()
        }else{
            //Session
            //Redirect to Main view
            checkUserSession()
        }
   return true
}

I am facing this facing this problem from both APNS notification and local notification when app is Inactive or terminate.Please help me fro find the solution.

Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
  • When you click on the notification your app in running in background or not? your code will work only if your app will be launched again but not when it is in the background and you click on the notification – Ran Hassid Aug 02 '16 at 06:18
  • Then how to manage when app in background / Inactive condition how to redirect to particular view? – Anand Nimje Aug 02 '16 at 06:21
  • When i am clicking on notification that time my app in background condition then it will not redirecting particular page. – Anand Nimje Aug 02 '16 at 06:21
  • you can find a good answer in here: http://stackoverflow.com/a/32079458/4287861 . You need also to enable to remote notification capability if you want to use it – Ran Hassid Aug 02 '16 at 06:23
  • 2
    we need more info, when from comments you are talking about app in background then you can easily just setup breakpoints and check what is wrong and definitly not in didFinishLaunchingWithOptions – Lu_ Aug 02 '16 at 06:44

3 Answers3

0

How about you try in didReceiveRemoteNotification function?

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
     if(application.applicationState == UIApplicationStateBackground){



     }else if(application.applicationState == UIApplicationStateInactive){



     }
}
Rurouni
  • 963
  • 10
  • 31
  • It is already working on my side only one thing is when you terminate the app and click on notification then it is not working. – Anand Nimje Aug 02 '16 at 06:23
  • 1
    how about you add if(application.applicationState == UIApplicationStateBackground){ } – Rurouni Aug 02 '16 at 06:29
0

try this code, you can modify it according to you requirement. if you have any problem understanding this logic let me know. didReceiveRemoteNotification with completionHandler works both in background and in foreground. Don't forget to do appropriate changes in plist to support background fetch and background notification.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

    if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
        notificationDataDict = notification;
    }


    return true
}
func handleRemoteNotificationWithUserInfo(application:UIApplication, userInfo:NSDictionary){

}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    print(userInfo)

    if application.applicationState != .Inactive{

        notificationDataDict = userInfo;

        if let navigationController = self.window?.rootViewController as? UINavigationController{


            if application.applicationState == .Active{

                if application.backgroundRefreshStatus == .Available{

                    completionHandler(.NewData)

                    self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo)


                }
                else
                {
                    completionHandler(.NoData)
                }
            }
            else{
                completionHandler(.NewData)

                self.handleRemoteNotificationWithUserInfo(application, userInfo: userInfo)
            }

        }


    }
}
Deepak Kumar
  • 175
  • 9
0

Finally i got the solution for Manage APNS Push notification and Local notification, its working fine now.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

    //Register here For Notification

    if #available(iOS 9, *) {

        let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        application.registerUserNotificationSettings(pushNotificationSettings)
        application.registerForRemoteNotifications()

    }else{

        UIApplication.sharedApplication().registerUserNotificationSettings(
            UIUserNotificationSettings(forTypes: .Alert, categories: nil))
        application.registerForRemoteNotifications()
    }

dispatch_async(dispatch_get_main_queue()) {
        // Check if launched from notification
        // 1
        if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
            // 2
            let aps = notification["aps"] as! [String: AnyObject]
            handlePushMessage(aps)
        }else if let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
            // 2
            self.postData = notification.userInfo as! [String: AnyObject]
            didTapNotification()
        }else{
            //Session
            checkUserSession()
        }
      }

}
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43