1

我可以使用 GCM 向 Chrome Web App 发送消息。我正在使用 http 发布到: https : //android.googleapis.com/gcm/send 发送registrationid、applicationid 和senderid 值。该消息在我的屏幕上显示为 Chrome 通知。我的问题是 - 我可以使用命令或 GCM 事件/函数来启动 Chrome Web 应用程序吗?谷歌浏览器设置为在后台运行。

这是我的消息事件的 GCM 侦听器:

chrome.gcm.onMessage.addListener(messageReceived);

它调用该messageReceived函数,在此函数中我收到消息,在屏幕上弹出通知,并打开/创建窗口:

chrome.notifications.create(getNotificationId(), {
    title: 'GCM Message',
    iconUrl: 'gcm_128.png',
    type: 'basic',
    message: messageString
  }, function() {});
  
  // Center window on screen.
  var screenWidth = screen.availWidth;
  var screenHeight = screen.availHeight;
  var width = 500;
  var height = 300;
  chrome.app.window.create('register.html', {
    id: "helloWorldID",
    outerBounds: {
      width: width,
      height: height,
      left: Math.round((screenWidth-width)/2),
      top: Math.round((screenHeight-height)/2)
    }
  });
}

4

2 回答 2

1

您不能直接从接收推送消息的代码中启动 chrome。但是,一旦单击通知,您可以使用http://www.w3.org/TR/service-workers/#client-focus查看是否已经打开了一个窗口或http://www.w3.org/TR /service-workers/#client-navigate打开一个新窗口。

https://tests.peter.sh/notification-generator/是一个很好的示例站点,您可以在其中使用不同的选项。

于 2015-12-31T18:49:02.240 回答
0

这是我的消息事件的 GCM 侦听器:chrome.gcm.onMessage.addListener(messageReceived);

它调用 messageReceived 函数,在这个函数中我得到消息,在屏幕上弹出通知,并打开/创建窗口:

chrome.notifications.create(getNotificationId(), {
    title: 'GCM Message',
    iconUrl: 'gcm_128.png',
    type: 'basic',
    message: messageString
  }, function() {});
  
  // Center window on screen.
  var screenWidth = screen.availWidth;
  var screenHeight = screen.availHeight;
  var width = 500;
  var height = 300;
  chrome.app.window.create('register.html', {
    id: "helloWorldID",
    outerBounds: {
      width: width,
      height: height,
      left: Math.round((screenWidth-width)/2),
      top: Math.round((screenHeight-height)/2)
    }
  });
}

于 2015-09-29T14:28:39.647 回答