1

manifest.json:

{
   "app": {
      "background": {
         "scripts": [ "background.js" ]
      }
   },
   "key": "...",
   "manifest_version": 2,
   "name": "Push Sample",
   "permissions": [ "notifications", "pushMessaging" ],
   "version": "1.0"
}

background.js:

chrome.runtime.onInstalled.addListener(
    function() {
        chrome.pushMessaging.onMessage.addListener(
            function (message) {
                chrome.notifications.create(
                    '',
                    {
                        type: 'basic',
                        iconUrl: 'icon24.png',
                        title: 'The Title',
                        message: message.payload
                    },
                    function (notificationID) {
                    }
                );
            }
        );
    }
);

That's the entire app. No HTML page (no window is created) and no other JavaScript.

When the background page is active, the app works fine, and I see the notification message popup at the upper right of my screen. (Not shown is some code I had in temporarily to show me the Channel ID, which my server uses. The server is a PHP program that uses Google Cloud Messaging for Chrome service. It seems to be OK.)

However, if I then do nothing for a few seconds, the app becomes inactive. On the Extensions page in Chrome, the line:

Inspect views: background page

changes to:

Inspect views: background page (Inactive)

When a message is sent then, that line changes to the active state ("background page", without the "(Inactive)" part, indicating the message has in some sense been received. However, the notification code does not result in a popup.

I can't determine what's wrong with logging, because when the JavaScript console is opened the app stays active, and then works.

Question: My app seems to be woken up when the message arrives, according to the status indication on the Extensions page, but it's not working. Anyone know why?

BUG FILED: Did a lot more testing, including with a stable (not beta) version of Chrome. Tried on OS X, Chrome OS, and Windows. Same thing. Here's the issue I filed: https://code.google.com/p/chromium/issues/detail?id=316315

Eran
  • 387,369
  • 54
  • 702
  • 768
Marc Rochkind
  • 3,678
  • 3
  • 30
  • 38
  • What did you do to fix it exactly? I'm having the same problem and I register the listener on every page load yet I'm unable to receive pushmessages (the background page does become active). – ndsc Feb 17 '14 at 15:17
  • Not receiving push messages can have all sorts of causes, as sending push messages to the app requires that lots of things be exactly right, including the client_id in the manifest.json files and lots more. Far too complex to put in here. My issue was just that the event wasn't getting listened for. In other words, somehow before the issues with the app can be fixed, it's necessary to know that the message is indeed arriving at the computer and that the all of the parameters are OK. Sorry I can't be more helpful, but push messaging is extremely complex to sort out. – Marc Rochkind Feb 17 '14 at 18:55
  • I can assure you that GCM works fine. I currently use a 'normal' background page. Everything is in place and working as intended. I'm just looking to switch to event pages. When the event page loads I add a listener for chrome.pushMessaging.onMessage but somehow the listener method never gets called. Thanks for your reponse though! – ndsc Feb 17 '14 at 20:56

2 Answers2

1

Sounds like a bug, file one at http://crbug.com/net and include the sample code. Update the question to link to the issue so others can star it to track the status.

Vincent Scheib
  • 17,142
  • 9
  • 61
  • 77
0

I misunderstood how event listening worked. Here's the info:

https://developer.chrome.com/apps/event_pages.html#registration

"Because the listeners themselves only exist in the context of the event page, you must use addListener each time the event page loads; only doing so at runtime.onInstalled by itself is insufficient."

Pointed out to me in a comment on the issue I filed. All is working now.

Marc Rochkind
  • 3,678
  • 3
  • 30
  • 38