0

我有一個使用PHP EasyAPNS通知的推送通知的應用程序,可以在Swift 3,iOS 10上正常工作。但有一件事我不明白爲什麼徽章只有當我從通知觸發器啓動應用程序時才顯示帶有BadgeValue的TabItem

:在TabItem的是,當我從通知警報啓動應用程序,但不是當我打開該應用從應用程序圖標直接(與紅色徽章)

因此,這裏是我的AppDelegate中使用的代碼工作正常

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [String:Any]) 
{ 
    print("Message details \(userInfo)") 

    if let aps = userInfo["aps"] as? NSDictionary 
    { 
     if let alertMessage = aps["alert"] as? String { 
      let rootViewController = self.window?.rootViewController as! UITabBarController! 
      let tabArray = rootViewController?.tabBar.items as NSArray! 
      let tabItem = tabArray?.object(at: 3) as! UITabBarItem 
      tabItem.badgeValue = "1" 

      let myAlert = UIAlertController(title: "Message", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert) 
      let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil) 
      myAlert.addAction(okAction) 
      self.window?.rootViewController?.present(myAlert, animated: true, completion: nil) 
     } 
    } 
} 

所以,當我點擊警報來打開我的應用程序時,徽章就像這樣:

TabItem Badge working

但是當我打開使用圖標本身的應用程序,徽章沒有顯示出來:

TabItem Badge NOT working

任何人有任何知道我做錯了嗎?

請讓我知道,如果我能改善這個問題!

+0

不知道這是否與你的問題有關,但他使用的功能是**棄用**。你不應該在iOS10中使用這個功能。如果它有效,那是因爲你很幸運。見[這裏](https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift-3-0)和[這裏](https://stackoverflow.com/問題/ 39883112 /如何到獲得最體對的一推通知發送的 - 從 - 火力控制檯功能於IOS-10)。 – Honey

+0

在iOS10中,如果您想在用戶**輕按**時進行回調,則必須使用['didReceiveNotificationResponse'](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter?language= objc) – Honey

回答

0

您應該使用application(_:didReceiveRemoteNotification:fetchCompletionHandler:)方法來處理通知。正如文檔(found here)中所述,無論應用程序處於前景還是背景,都會調用此方法。

另外值得一從文檔指出的application(_:didReceiveRemoteNotification:)

如果在遠程通知到達時,應用程序沒有運行,則 方法啓動應用程序,並提供了 的啓動選項字典中的相應信息。該應用程序不會調用這個方法來處理那個遠程通知 。

請注意,如果應用程序未運行且用戶點擊圖標,應用程序將調用application(_:didFinishLaunchingWithOptions:)。如果應用程序有需要處理的遠程通知,則會有適當的launchOption鍵值對。

+0

我收到錯誤,當我點擊通知去swift 3中的特定視圖控制器,錯誤我: - 不能將'UINavigationController'(0x3a79b0a0)類型的值強制轉換爲'UITabBarController'(0x3a79b938)應用程序崩潰@lostIn Transit – anuj

+0

那麼這將是你的代碼中的東西。你正在得到一個'UINavigationController',但試圖將它用作'UITabBarController'。檢查你的實現。這應該可能成爲另一個問題。它與通知無關。 – lostInTransit

+0

可以幫我用@lostIn Transit – anuj

相關問題