0

I have an app with push notifications using PHP EasyAPNS notification working fine on Swift 3, iOS 10. But one thing I can't understand is why the badge on TabItem is works fine when I launch the app from the notification alert but not when I open the app direct from the app icon (with the red Badge)

So here is the code that I use on 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)
        }
    }
}

So, when I click in the alert to open my app, the badge is fine like this:

TabItem Badge working

But when I open the app using the icon itself, the badge is not showing up:

TabItem Badge NOT working

Anyone have any idea what I'm doing wrong?

Please let me know if I can improve the question!

Anh Pham
  • 2,108
  • 9
  • 18
  • 29
  • Not sure if this has anything to do with your problem, but he function you're using is **deprecated**. You shouldn't be using this function in iOS10. If it works it's because you're lucky. See [here](https://stackoverflow.com/questions/37956482/registering-for-push-notifications-in-xcode-8-swift-3-0) and [here](https://stackoverflow.com/questions/39883112/how-to-get-the-body-of-a-push-notification-sent-from-firebase-console-in-ios-10). – mfaani Jul 17 '17 at 00:26
  • In iOS10, if you want to have a callback when user **taps** then you must use [`didReceiveNotificationResponse`](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter?language=objc) – mfaani Jul 17 '17 at 00:29

1 Answers1

1

You should be using the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method to handle notifications. As mentioned in the docs (found here), this method is called whether the app is in the foreground or background.

Also worth noting from the docs for application(_:didReceiveRemoteNotification:)

If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification.

Note, if the app was not running and the user taps on the icon, the app will call application(_:didFinishLaunchingWithOptions:). There will be appropriate launchOption key-value pairs if the app had a remote notification which needs handling.

lostInTransit
  • 70,519
  • 61
  • 198
  • 274
  • i am getting error when i click on notifications to go to a particular view controller in swift 3 , error i:-could not cast value of type 'UINavigationController' (0x3a79b0a0) to 'UITabBarController' (0x3a79b938) app crashes @lostIn Transit – anuj Oct 06 '17 at 06:55
  • Well that would be something in your code. You are getting a `UINavigationController` but trying to use it as a `UITabBarController`. Check your implementation. And this should probably go as another question. It is not related to notifications. – lostInTransit Oct 06 '17 at 07:02
  • could u help me with this @lostIn Transit – anuj Oct 06 '17 at 07:09