If you are using a silent push notification, then you cannot include the badge key in the aps dictionary, so you can't update the badge directly with a silent push.
You can include your own keys and values in the aps dictionary and this dictionary is available in the didReceiveRemoteNotification:fetchCompletionHandler UIApplicationDelegate function.
In this function you can extract your own "badge" key from the aps dictionary and use this to update the badge number directly using the UIApplication property [applicationIconBadgeNumber][1]
UIApplication.shared.applicationIconBadgeNumber = someValue
Something like:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let badgeNumberStr = userInfo["myBadge"] as? String {
if let badgeNumber = Int(badgeNumberStr) {
application.applicationIconBadgeNumber = badgeNumber
}
}
}