This may not be the solution for everyone, but I did notice a pattern of behavior that seems to hold constant. For me (iphone 5S, late 2013, OS 10.3.3, all Apple brand chargers & cables) the problem seems to be associated with heat from the battery. If I've been using a high-drain app that causes the battery to heat up that's when the error message seems to appear. I let the phone cool down, plug the cable in, and it's likely to charge OK.
I know the phone is old and the battery is near the end of its life. Also, the Lightning port has been used a lot and so may be somewhat worn.
I theorize that, at least with my phone, a combination of wear on the Lightning port plus expansion of metal from heat cause the contacts to be less physically secure. I theorize that the more recent OS's have code that makes them interpret a poor contact as if it were a non-standard accessory.
Unfortunately the Genius Bar advised me that the Lightning port is not a part that can be replaced, so when the connector gets worn the entire phone needs to be replaced. I am disappointed at Apple for designing products that are not repairable, although I suspect part of that decision was to allow the smallest physical size possible for all the components - a replaceable connector would likely be bulkier resulting in a larger, heavier phone. (It's also possible that Apple, like so many companies, includes "planned obsolescence" in their marketing and sales strategies. But I don't think that is the ONLY reason why these phones fail)