我们如何实现网页推送通知? [英] How can we implement web push notification?

查看:238
本文介绍了我们如何实现网页推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Google的GCM服务在浏览器中实现推送通知。
执行相同的步骤。

解决方案

如果你想要的基本步骤在浏览器中添加推送通知如下:


  • 首先,您需要一个HTTP S 网页其次,你需要添加一些JavaScript代码到你的网页,将做以下几点:$ b​​
    $ b

    • 向用户请求推送权限

    • 注册正在访问网页的浏览器

    • 激活将等待的ServiceWorker对于通知(当接收到通知时,它将生成浏览器通知)。

    • 最后想到的是,您需要设置 Google开发人员控制台上的项目。在这个地方,你会得到一些必需的参数(项目ID和API密钥),将需要发送和接收推送通知。


    在这个教程中,我已经提到了所有的步骤,以及一些例子。

    您必须知道浏览器通知是一个日益增长的项目,有一些已知的麻烦。其中一个原因是您无法从通知中收到有效内容,因为在HTTPS中,该消息是编写的,GCM无法解码,浏览器收到的消息是空的通知。有一些解决这个麻烦,但他们正在努力解决它。



    这是一个例如,你可以看到它的作品(你也可以用你的智能手机打开它,看看结果)。

    我希望这可以帮助你! p>

    How to implement push notification in browsers using the googles GCM service. What are the steps to implement the same.

    解决方案

    The basic steps that you will have to do if you want to add push notifications in browsers are the following:

    • First of all, you need an HTTPS webpage (GCM requires it for browser notifications).
    • Secondly, you need to add some javascript code into your webpage that will do the following points:
      • Ask the user for push permissions
      • Register the browser that are accessing into the webpage
      • Activate a ServiceWorker that will be waiting for notifications (and when a notification is received, it will generate the browser notification).
    • The last think, you need to set up a project on the Google Developer Console. In that place you will get some required params (project id and API key) that will be required to send and receive push notifications.

    In this tutorial there are all the steps I have commented but well explained with some examples.

    You must know that browser notifications is a growing project that have some known troubles. One of them is that you can't receive payload from the notification because in the HTTPS the message is encripted and it can't be decoded by GCM and the message that the browser receive is an empty notification. There are some solutions for this trouble but they are working to solve it.

    This is an example where you can see it works (you can open it using your smartphone too and watch the result).

    I hope this can help you!

    这篇关于我们如何实现网页推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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