2017-08-10 48 views
0

由於我對推送通知非常陌生,因此我已經查看了幾個教程,概述了在應用程序終止時處理遠程推送通知的最佳方式,並且似乎我仍然遇到了問題應用程序。我在點擊推送通知時調用了didFinishLaunchingWithOptions,但由於某種原因,函數被跳過,並且不執行代碼以打開正確的視圖控制器。當應用程序終止時對推送通知做出響應

這裏是AppDelegate中

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { 
// Override point for customization after application launch. 

//UserNotification 
let center = UNUserNotificationCenter.current() 

center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in 
    // Enable or disable features based on authorization. 
    if granted { 
     UIApplication.shared.registerForRemoteNotifications() 

     if GlobalService.sharedInstance().g_userDeviceToken == nil { 
      // GlobalService.sharedInstance().g_userDeviceToken = "" 


     } 
    } else { 
     print("Don't Allow") 

    } 

    if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] { 

     self.getUserChatRooms() 
     let aps = notification["aps"] as! [String: AnyObject] 

     let mainNC = self.window?.rootViewController as! UINavigationController 
     let storyboard = UIStoryboard.init(name: "Main", bundle: nil) 

     let mainVC = storyboard.instantiateViewController(withIdentifier: String(describing: MainViewController.self)) as! MainViewController 
     GlobalService.sharedInstance().g_homeVC = mainVC 
     mainVC.didReceiveChat(aps) 
     mainNC.pushViewController(mainVC, animated: false) 

    } 
    ... 

//SVProgressHUD 
SVProgressHUD.setDefaultStyle(.dark) 

//Check UserObj 
GlobalService.sharedInstance().g_appDelegate = self 
if let userObj = GlobalService.sharedInstance().loadUserObj() { 
    GlobalService.sharedInstance().g_userMe = userObj 
    startApplication(animated: false) 
... 
return true 
} 

的代碼,那麼這是被稱爲在MainVC功能:

func didReceiveChat (_ notificationDictionary: [String: AnyObject]){ 
let allChats = GlobalService.sharedInstance().g_aryChatRooms 

if let noti_id = notificationDictionary["noti_id"] as? String{ 
     let pushID = Int(noti_id) 
    if pushID != nil { 
     for chat in allChats { 

      if chat.chat_room_id! == pushID! { 
      chtRoom = chat 

       NotificationCenter.default.post(name: Notification.Name(rawValue: Constants.Notifications.GET_MSG), object: self) 
      break 
      } 
     } 
    } 
} 
} 

,然後在我的viewDidLoad方法我想補充觀察員:

NotificationCenter.default.addObserver(self,selector: #selector(MainViewController.addChatScn(_:_:)), name: NSNotification.Name(rawValue: Constants.Notifications.GET_MSG), object: nil) 

終於我打電話給addChatScn的方法:

@objc func addChatScn(_ chatObj: ChatRoomObj, _ msgName: String) { 
    let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatContainerViewController") as! ChatContainerViewController 

    iconContainer.isHidden = true 
    add_Msg_Btn.isHidden = true 
    popvc.vcA = self 
    popvc.m_selectedChatRoom = chatObj 
    popvc.msgName = msgName 
    self.addChildViewController(popvc) 
    popvc.view.center = self.view.center 
    popvc.view.bounds.size = CGSize(width: 337, height: 503) 

    self.view.addSubview(popvc.view) 

    popvc.didMove(toParentViewController: self) 
} 

回答

0

在您的GlobalService.sharedInstance中,添加名爲'readyToReceivePush'的布爾值。在viewDidLoad的MainVC中,將'GlobalService.sharedInstance.readyToReceivePush'設置爲true。如果應用程序進入後臺,請將其設置爲false;如果回到前臺,則將其設置爲true。

func applicationDidEnterBackground(_ application: UIApplication) { 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    GlobalService.sharedInstance.readyToReceivePush = false 
} 

func applicationWillEnterForeground(_ application: UIApplication) { 
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    GlobalService.sharedInstance.readyToReceivePush = true 
} 

一旦你的設置,如果用戶在通知刷卡的「didReceiveRemoteNotification」將被調用。在該方法中,如果GlobalService.sharedInstance.readyToReceivePush == false,則將userInfo保存爲GlobalService.sharedInstance.savedPushInfo。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    if GlobalService.sharedInstance.readyToReceivePush == false { 
     GlobalService.sharedInstance.savedPushInfo = userInfo 
    } else { 
     // Handle notification while the user is in the foreground 
    } 
} 

一旦你做到了這一點,在MainVC檢查GlobalService.sharedInstance.savedPushInfo!=零。如果是!= nil,請調用您的'didReceiveChat'函數或任何您處理通知的userInfo數據的操作。

+0

我認爲這對於當應用程序處於後臺時很有用,但是我的具體用例是當應用程序被完全終止時 – NightHawk95

相關問題