I tried the code in my dev instance it's working fine.

You have to write the code in background script.

Regards,
Ajay