当后台页面处于非活动状态时,Chrome App pushMessaging不起作用 [英] Chrome App pushMessaging does not work when background page is inactive

查看:585
本文介绍了当后台页面处于非活动状态时,Chrome App pushMessaging不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 (
'',
{
类型:'basic',
iconUrl:'icon24.png',
标题:'标题',
消息:message.payload
},
函数(notificationID){
}
);
}
);
}
);

这是整个应用程序。没有HTML页面(没有窗口创建),也没有其他JavaScript。



当后台页面处于活动状态时,应用程序正常工作,并且我看到通知消息弹出我的屏幕右上角。 (没有显示一些代码,我暂时向我展示了我的服务器使用的频道ID。该服务器是一个PHP程序,使用Google云消息传递服务,看起来没问题。)



但是,如果我在几秒钟内什么都不做,应用程序就会变为非活动状态。在Chrome的扩展页面中,行:

  Inspect views:background page 



更改为:

 检查视图:背景页(非活动)

当一条消息被发送时,该行变为激活状态(后台页面,没有(不活动)部分,表示邮件在某种意义上已经收到,但通知码不会弹出。



我可以确定日志记录有什么问题,因为当JavaScript控制台打开时,应用程序保持活动状态,然后运行。



问题:我的应用似乎在消息到达,根据扩展页面上的状态指示,但它不工作。任何人都知道为什么?



BUG已存档:有很多更多的测试,包括一个稳定(不是beta版)的Chrome。在OS X,Chrome OS和Windows上试用。我提交的问题: https://code.google.com/p/ chromium / issues / detail?id = 316315

解决方案

我误解了事件监听的工作原理。以下是信息:



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



由于侦听器本身只存在于事件页面的上下文中,因此您每次加载事件页面时都必须使用addListener;只有在运行时才这样做,而不是自己安装的。



指出我对我提交的问题的评论。现在所有人都在工作。


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

解决方案

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.

这篇关于当后台页面处于非活动状态时,Chrome App pushMessaging不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆