Chrome应用弹出通知

时间:2016-01-10 05:23:52

标签: javascript json google-chrome google-cloud-messaging chromium

我是Chrome应用的新手,但我确实成功让我的应用与GCM合作。我很开心!然而,我希望该应用程序可以在用户收到通知时打开一个弹出窗口。我正在制作视频聊天应用。请任何帮助将不胜感激!我所拥有的这段代码目前不适用于弹出窗口,标签或任何类型...... :(

  

background.js

    // Returns a new notification ID used in the notification.
function getNotificationId() {
  var id = Math.floor(Math.random() * 9007199254740992) + 1;
  return id.toString();
}

function messageReceived(message) {
  // A message is an object with a data property that
  // consists of key-value pairs.

  // Concatenate all key-value pairs to form a display string.
  var messageString = "";
  for (var key in message.data) {
    if (messageString != "")
      messageString += ", "
    messageString += key + ":" + message.data[key];
  }
  console.log("Message received: " + messageString);

  // Pop up a notification to show the GCM message.
  chrome.notifications.create(getNotificationId(), {
    title: 'GCM Message',
    iconUrl: 'gcm_128.png',
    type: 'basic',
    message: messageString
  }, function() {});


chrome.tabs.create({url:"http://www.google.com"});

}

var registerWindowCreated = false;

function firstTimeRegistration() {
  chrome.storage.local.get("registered", function(result) {
    // If already registered, bail out.
    if (result["registered"])
      return;

    registerWindowCreated = true;
    chrome.app.window.create(
      "register.html",
      {  width: 500,
         height: 400,
         frame: 'chrome'
      },
      function(appWin) {}
    );
  });
}

// Set up a listener for GCM message event.
chrome.gcm.onMessage.addListener(messageReceived);

// Set up listeners to trigger the first time registration.
chrome.runtime.onInstalled.addListener(firstTimeRegistration);
chrome.runtime.onStartup.addListener(firstTimeRegistration);
  

的manifest.json

{
  "name": "GCM Notifications",
  "description": "Chrome platform app.",
  "manifest_version": 2,
  "version": "0.3",
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "permissions": ["gcm", "storage", "notifications", "tabs", "<all_urls>"],
  "icons": { "128": "gcm_128.png" }
}

1 个答案:

答案 0 :(得分:0)

chrome.notifications.create应出现在浏览器之外。但是,我发现你的电话没有任何问题。我们需要确定您是否未违反Content Security Policy

  

在Chrome应用中,由于严格的内容安全政策,这些网址必须指向本地资源或使用blob or data URL。使用3:2比例的图像;否则黑色边框构成图像。

您可以查看的其他参考资料是官方notification sample repo以及如何integrate notification with GCM