简体   繁体   English

如何在Chrome中实施推送通知

[英]How to implement push notification in Chrome

I am writing a chrome extension that will receive at some points messages. 我正在编写一个Chrome扩展程序,该扩展程序有时会收到消息。 I would like to have that little red circle on the bottom-right side of my ico extension with 1,2,3... depending on the number of messages received by the user. 我想在我的ico扩展的右下角有一个小小的红色圆圈 ,分别为1,2,3 ...,具体取决于用户收到的消息数量。 More, if the user pushes on the extension and reads that/those message(s), that little circle will disappear 而且,如果用户按下扩展程序并阅读了该(那些)消息,那么这个小圆圈将消失

I've read some nice articles about how will I be able to implement such a thing but for now, all that I've come with is a basic notification which disappear after ten seconds. 我读了一些不错的文章,介绍了如何实现这样的事情,但是现在,我所附带的只是一个基本通知,该通知会在十秒钟后消失。

manifest.json manifest.json

{
    "name": "Notification with Audio",
    "description": "notification",
    "manifest_version": 2,
    "version": "1",
    "permissions": [
        "notifications"
    ],
    "background": {
        "scripts": [
            "background.js"
        ]
    }
}

background.js background.js

createNotification(); createNotification(); audioNotification(); audioNotification();

function audioNotification(){
    var yourSound = new Audio('yourSound.mp3');
    yourSound.play();
}

function createNotification(){
    var opt = {type: "basic",title: "Your Title",message: "Your message",iconUrl: "your_icon.png"}
    chrome.notifications.create("notificationName",opt,function(){});

    //include this line if you want to clear the notification after 5 seconds
    setTimeout(function(){chrome.notifications.clear("notificationName",function(){});},5000);
}

Could somebody describe the process of achieving what I want ? 有人可以描述实现我想要的过程吗? It's a must to use the GCM service ? 必须使用GCM服务吗?

If you want to present an icon that displays the number of unread messages and does something on click, then you want a Browser Action . 如果您想显示一个图标,显示未读消息的数量并单击以执行某些操作,则需要一个浏览器操作

A browser action can either open a small page on click called a popup (useful if you want to display something) or trigger an onClicked event in the background page (useful if you want to do something else, like open a web page). 浏览器操作可以在点击打开一个小页面称为弹出(有用的,如果你想显示的东西)或触发onClicked (如果你想做些别的事情,比如开一个网页有用)在后台页面事件。

As for the counter, the standard way is to use the badge , which can be set with setBadgeText and setBadgeBackgroundColor . 至于计数器,标准方法是使用徽章 ,可以通过setBadgeTextsetBadgeBackgroundColor进行设置。 Alternatively, you can draw some sort of custom badge over your icon using a <canvas> and update the icon dynamically with setIcon - but that would go against UI traditions of Chrome. 另外,您可以使用<canvas>在图标上绘制某种自定义徽章,并使用setIcon动态更新图标-但这违反了Chrome的UI传统。

徽章样本


As for push messages - GCM API is a ready solution, but you can implement your own; 至于推送消息-GCM API是现成的解决方案,但是您可以实现自己的解决方案; WebSockets would be a suitable technology to do it. WebSockets将是一种适合的技术。

If you have a more concrete question about that, ask it separately. 如果您对此有更具体的问题,请单独询问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM