Activate Chrome native notifications

13,042

For LibNotify, the JSON file that it installs has the incorrect extension ID. Updating the extension ID to the correct one fixes it.

Go to .config/google-chrome/NativeMessagingHosts (for Google Chrome) or .config/chromium/NativeMessagingHosts (for Chromium). Open up the JSON file in the folder, and notice that in the allowed_origins section, it allows the extension ID gphchdpdmccpjmpiilaabhpdfogeiphf. However, the extension ID (at least in my case, but it should be the same for everyone) is actually epckjefillidgmfmclhcbaembhpdeijg.

To fix this, either replace the incorrect extension ID with the correct one, or add a comma and the correct extension ID after it. I personally chose the latter option, and here's what my JSON file looks like:

{
  "name": "com.initiated.chrome_libnotify_notifications",
  "description": "Libnotify Notifications in Chrome",
  "path": path to the location of install.sh,
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://gphchdpdmccpjmpiilaabhpdfogeiphf/",
    "chrome-extension://epckjefillidgmfmclhcbaembhpdeijg/"
  ]
}

EDIT: That's not the only change that needs to be made. The extension relies on Webkit notifications, which were deprecated and removed in Chrome(ium) and likely other browsers in favor of HTML5 notifications. Therefore, google-chrome/default/Extensions/epckjefillidgmfmclhcbaembhpdeijg/1.0_0/notify_hook.js needs to be updated. I've written a short script for this, but it breaks most of the standard except for displaying the notification. Replace everything in the file with the following (added basic support for sites still using window.webkitNotifications and (hopefully) improved image support) (permissions support added):

OriginalNotification = Notification

Notification = function(title, properties) {
        if (Notification.permission != "granted") {
                if (this.onError) {
                        this.onError();
                }
                return;
        }
        if (!properties.hasOwnProperty("body")) {
                properties["body"] = "";
        }
        if (!properties.hasOwnProperty("icon")) {
                properties["icon"] = "";
        }
        if (properties["icon"]) {
                properties["icon"] = getBaseURL() + properties["icon"];
        }
        document.getElementById('libnotify-notifications-transfer-dom-area').innerText = JSON.stringify({title:title, body:properties["body"], iconUrl:properties["icon"]});
        var event = document.createEvent("UIEvents");
        event.initUIEvent("change", true, true);
        document.getElementById('libnotify-notifications-transfer-dom-area').dispatchEvent(event);
        if (this.onShow) {
                this.onShow();
        }
};

Object.defineProperty(Notification, "permission", {
        get: function() {
                return OriginalNotification.permission;
        },
        set: undefined
});

Notification.requestPermission = function(callback) {
        OriginalNotification.requestPermission(callback);
}

window.webkitNotifications = {}

window.webkitNotifications.checkPermission = function() {
        return 0;
}

window.webkitNotifications.createNotification = function(image, title, body) {
        if (image) {
                image = getBaseURL() + image;
        }
        document.getElementById('libnotify-notifications-transfer-dom-area').innerText = JSON.stringify({title:title, body:body, iconUrl:image});
        var event = document.createEvent("UIEvents");
        event.initUIEvent("change", true, true);
        document.getElementById('libnotify-notifications-transfer-dom-area').dispatchEvent(event);
}

function getBaseURL() {
           return location.protocol + "//" + location.hostname + 
                   (location.port && ":" + location.port) + "/";
}
Share:
13,042

Related videos on Youtube

Louis Matthijssen
Author by

Louis Matthijssen

I like almost anything that has something to do with technology. Especially installing, configuring and maintaining PCs & servers. I also like programming and cookies.

Updated on September 18, 2022

Comments

  • Louis Matthijssen
    Louis Matthijssen almost 2 years

    I'm trying to get native notifications to work using Google Chrome (or Chromium) on Ubuntu, but no luck so far.

    Things I've tried already:

    And I remember I've tried another extension as well but I don't remember its name.

    None of them work. I keep getting the normal notifications of Chrome itself.

    I'm using Google Chrome 34.0.1847.137 on Ubuntu 14.04 x64.

    Can someone tell me how to get this working?

    • emi
      emi almost 7 years
      The latest version of Google Chrome Stable (59 as now) supports the Enable Native Notifications flag, which works just perfectly on Gnome Shell. It's available via chrome://flags/#enable-native-notifications.
  • Louis Matthijssen
    Louis Matthijssen about 10 years
    It's working and the CPU usage seems to have solved itself. However, still doesn't work in the application I want (Webogram). But thank you very much for your answer and effort and I'll just wait for Chrome to support native notifications!
  • Konstigt
    Konstigt about 10 years
    Please see my comment below, works by default on Chrome 35
  • Louis Matthijssen
    Louis Matthijssen about 10 years
    Those are not Unity notifications. It's just a new skin for Chrome's notifications.
  • saiarcot895
    saiarcot895 about 10 years
    @Konstigt: It's not that notifications weren't working; it's that the notifications weren't native Linux notifications, and existing solutions (at least the first two links above) used the deprecated method. (I personally don't blame Chrome(ium) for this.)
  • umpirsky
    umpirsky over 9 years
    I have no .config/chromium/NativeMessagingHosts folder.
  • saiarcot895
    saiarcot895 over 9 years
    @umpirsky: Did you install the binary that's linked on the extension page?
  • umpirsky
    umpirsky over 9 years
    @saiarcot895 I don't see any binary on chrome.google.com/webstore/detail/libnotify-notifications-i/‌​…. Thank
  • saiarcot895
    saiarcot895 over 9 years
    @umpirsky: Just run it as ./install.sh. I believe it expects the terminal it runs in to be Bash, and not just sh.
  • saiarcot895
    saiarcot895 over 9 years
    @umpirsky: That's strange. I made a minimized version at gist.github.com/saiarcot895/a80e0fb8725a1844dc34. If you use Chromium, uncomment the second TARGET_DIR line, and comment the first TARGET_DIR. Place that in the same directory as the install.sh.
  • umpirsky
    umpirsky over 9 years
    @saiarcot895 Thanks, but still error gist.github.com/saiarcot895/…