I want to create a plugin that adds a raster layer (published on geoserver) on QGIS via PYQGIS. As long as i enable the advertised option in the layer publish panel the layer is easily added on the QGIS. But when I unchecked the advertised option, the layer is not added. So, please suggest a method through which i can create a custom plugin that adds published WMS layer (whose layer preview is OFF) on the QGIS desktop?
A "good" client like qgis will query the getcapabilities response to find out what layers are available, unadvertised layers will not show up in that list. A lazy client will rely on you adding the layer name you want (leaflet, arcgis) - an attacker can read you leaflet JS and will know the layer name and so you have no security.
Therefore you should use the security system built into GeoServer to restrict access to your layer in a standardised way.
Make a copy of the GeoServer GetCapabilities response, add the layer(s) you want. Save the static file to a location on a web server. Point QGIS to this static file.
Configure a GDAL WMS driver configuration file and add your required layers there.