when the user opening the push notification i present view from appdelget by this code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
application.applicationIconBadgeNumber = 0; // Clear badge when app is launched
if UserDefaults.standard.bool(forKey: "PushOFF") == false
{
registerForRemoteNotification()
}
else
{
UIApplication.shared.unregisterForRemoteNotifications()
}
return true
}
func registerForRemoteNotification()
{
if #available(iOS 10.0, *)
{
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
}
else
{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("DEVICE TOKEN = \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Registration failed! error=\(error)")
}
//Called when a notification is delivered to a foreground app.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("foreground User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
print("foreground app",notification.request.content.userInfo)
}
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
completionHandler()
let result = response.notification.request.content.userInfo as! Dictionary<String, AnyObject>
print("action User Info = ",result)
let app = result["aps"] as! Dictionary<String, AnyObject>
let title = app["alert"] as! String
let id = result["id"]?.integerValue
print("alert=",title,"id=",id!)
if id != 0
{
if UserDefaults.standard.bool(forKey: "login")
{
UserDefaults.standard.set(title, forKey: "notificationTitle")
UserDefaults.standard.set(id, forKey: "notificationId")
UserDefaults.standard.synchronize()
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PostVC") as? UINavigationController
{
var currentController = window?.rootViewController
while let presentedController = currentController?.presentedViewController
{
currentController = presentedController
}
currentController?.present(controller, animated: true, completion: nil)
}
}
}
}
if the application is working the code run perfect but when the application is off it present "PostVC" view first and present the root-view on top of "PostVC" view!! so you can't see the "PostVC" view.
what is wrong ? but i wouldn't to use window?.makeKeyAndVisible()