The camera of your smartphone uses "optical image stabilization." Since the lens unit can't be fixed when the camera is not working, when the smartphone is shaken, the unit hits the cushion structure in the camera unit and makes a rattling sound. This is a mechanical specification and is not a malfunction. In addition, thorough product reliability tests have been conducted, and this phenomenon doesn't lead to failure.
What is the optical image stabilization function?
This function keeps the lens unit, which consists of everything from the lens to the image sensor, in a floating state and moves the entire lens unit to correct camera shake and reduce image blur.