Search code examples
google-chrome-extensionnotifications

How to implement a notification popup with sound in chrome extension


How to implement a notification popup with sound in chrome extension.

Just like the Checker Plus for Gmail


Solution

  • You can use following code as a reference for playing sound in desktop notifications, it uses <audio> tag in conjunction with Desktop Notifications.

    Demonstration

    manifest.json

    Registered notification permissions and background page with manifest file.

    {
        "name": "Notification with Audio",
        "description": "http://stackoverflow.com/questions/14917531/how-to-implement-a-notification-popup-with-sound-in-chrome-extension",
        "manifest_version": 2,
        "version": "1",
        "permissions": [
            "notifications"
        ],
        "background": {
            "scripts": [
                "background.js"
            ]
        }
    }
    

    background.js

    Created a notification page from background application.

    // create a HTML notification:
    var notification = webkitNotifications.createHTMLNotification(
        'notification.html' // html url - can be relative
    );
    
    // Then show the notification.
    notification.show();
    

    notification.html

    Playing some random songs

    <html>
        <body>
            <p>Some Nice Text While Playing Song.. </p>
            <audio autoplay>
            <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3" type="audio/mpeg" />
            <source src="http://www.html5rocks.com/en/tutorials/audio/quick/test.ogg" type="audio/ogg" />
            </audio>
        </body>
    </html>
    

    References