python web应用

by Lucas Hild

卢卡斯·希尔德(Lucas Hild)

如何使用Python将通知发送到Web应用 (How to send notifications to your Web App using Python)

Native apps have become hugely popular recently, mostly because of features such as working offline, transitions, easy distributability and of course push notifications. But unfortunately, you need a good knowledge of languages like Java or Swift to create a valuable native app.

本地应用程序最近变得非常流行,主要是因为具有诸如脱机工作,过渡,易于分发以及当然还有推送通知之类的功能。 但是不幸的是,您需要对Java或Swift等语言有充分的了解才能创建有价值的本机应用程序。

渐进式Web应用 (Progressive Web Apps)

Progressive Web Apps (PWAs) are JavaScript applications that run in the browser. They make the effort to bring some of the native app features to the web. PWAs are easy to develop if you have a fundamental knowledge of HTML, CSS, and in particular JavaScript. Moreover, if your service is already accessible for desktop devices on a website, it is easier to add the functionalities of a Web App, instead of developing a native mobile app.

渐进式Web应用程序(PWA)是在浏览器中运行JavaScript应用程序。 他们努力将一些本机应用程序功能引入网络。 如果您具有HTML,CSS尤其是JavaScript的基础知识,则PWA易于开发。 此外,如果网站上的桌面设备已经可以访问您的服务,则添加Web应用程序的功能要比开发本机移动应用程序容易得多。

通知事项 (Notifications)

Notifications keep users informed about new messages, tell them about a new blog post, and so on.

通知可让用户了解新消息,将新博客内容告诉他们,等等。

Many native apps send push notifications to the user. But this is also possible using PWAs and the Notifications API.

许多本机应用程序将推送通知发送给用户。 但这也可以使用PWA和Notifications API来实现。

OneSignal (OneSignal)

In this tutorial, we will be using OneSingal to send notifications to our web app. OneSignal is a powerful tool that provides a simple interface to push notifications. They also provide a Rest API, which we will be using to send notifications.

在本教程中,我们将使用OneSingal将通知发送到我们的Web应用程序。 OneSignal是功能强大的工具,提供了用于推送通知的简单界面。 他们还提供了Rest API,我们将使用该API发送通知。

设置OneSignal (Setup OneSignal)

To send push notifications, you need to setup OneSignal first. Therefor you need an account on OneSignal. Head over to their website and press “Log in” in the upper right corner.

要发送推送通知,您需要先设置OneSignal。 因此,您需要在OneSignal上拥有一个帐户。 转到他们的网站 ,然后按右上角的“登录”。

Next you will have to create an app. Give it a name and choose “Setup platform”. Here you select “All Browsers”. After that, you choose “custom code” as the integration. Then you have to provide some information about your website.

接下来,您将必须创建一个应用。 给它起一个名字,然后选择“安装平台”。 在这里,您选择“所有浏览器”。 之后,选择“自定义代码”作为集成。 然后,您必须提供有关您的网站的一些信息。

In the settings area of your app, there is a tab called “Keys & IDs”. Copy both keys for later.

在应用程序的设置区域中,有一个名为“密钥和ID”的标签。 复制两个密钥以供以后使用。

Important: Do not share you REST API Key. Keep it private!

重要提示:不要共享您的REST API密钥。 保持私密!

That’s it for setting up OneSignal. That was easy!

设置OneSignal就是这样。 那很简单!

设置我们的网站 (Setup our website)

In the next part, we will be adding the notification functionality to our website. The website will have to wait for notifications sent by OneSignal and display them to the user.

在下一部分中,我们将向网站添加通知功能。 该网站将必须等待OneSignal发送的通知并将其显示给用户。

To let the browser know that you are creating a Progressive Web App, we will add a file called manifest.json to the root of our project.

为了让浏览器知道您正在创建渐进式Web应用程序,我们将在项目的根目录中添加一个名为manifest.json的文件。

{  "name": "My Application",  "short_name": "Application",  "start_url": ".",  "display": "standalone",  "background_color" : "#fff" ,  "description": "We send notifications to you",  "gcm_sender_id": "482941778795",  "gcm_sender_id_comment": "Do not change the GCM Sender ID"}

The first six key-value-pairs describe the appearance of the application. The gcm_send_id is important for sending notifications. If you want to know more about manifest.json, you can have a look in the Mozilla Documentation.

前六个键值对描述了应用程序的外观。 gcm_send_id对于发送通知很重要。 如果您想了解有关manifest.json的更多信息,请查看Mozilla文档 。

Your browser doesn’t automatically look for the manifest. You have to put the path to it in every HTML document in the <head> tag.

您的浏览器不会自动查找清单。 您必须在 lt; h e >广告代码中的每个HTML文档中放置路径。

<head>    ...    <link rel="manifest" href="manifest.json">    ...</head>

Additionally, we need some JavaScript code to connect our website to OneSignal.

此外,我们需要一些JavaScript代码才能将我们的网站连接到OneSignal。

You can put the code for that in a script tag in the &lt;head> part. Don’t forget to replace my-app-id with your own OneSignal app id.

你可以把代码在一个脚本标签在&lt; 广告>部分。 不要忘了重新的地方我- APP-ID与自己的OneSignal应用程序ID。

<head>    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>        <script>        var OneSignal = window.OneSignal || [];        OneSignal.push(function () {            OneSignal.init({                appId: "my-app-id",                autoRegister: false,                notifyButton: {                    enable: true,                },            });        });    <script></head>

When you want to prompt the user to subscribe to your notifications, you execute this piece of code.

当您想提示用户订阅您的通知时,您可以执行这段代码。

OneSignal.push(function () {    OneSignal.showHttpPrompt();});

Moreover, you need a service worker, which listens in the background for notifications. Therefore, you need two files in the root directory of your project.

此外,您需要一名服务人员,该服务人员在后台侦听通知。 因此,您在项目的根目录中需要两个文件。

OneSignalSDKUpdaterWorker.js

OneSignalSDKUpdaterWorker.js

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

OneSignalSDKWorker.js

OneSignalSDKWorker.js

importScripts('https://cdn.onesignal.com/sdks/OneSignalSDKWorker.js');

使用Python访问API (Access the API using Python)

OneSignal has an easy-to-use Rest API. The endpoints are documented in the OneSignal Developer Documentation.

OneSignal具有易于使用的Rest API。 端点记录在OneSignal开发人员文档中 。

To access it, we need to send HTTP requests. Therefore, we will use a library called requests. To install it, you can use pip, the package manager of Python.

要访问它,我们需要发送HTTP请求。 因此,我们将使用一个称为request的库。 要安装它,可以使用Python的包管理器pip。

pip install requests

This is the API endpoint we need to send a notification: https://onesignal.com/api/v1/notifications.

这是我们需要发送通知的API端点: https : //onesignal.com/api/v1/notifications 。

The HTTP protocol has several methods. In this case, we want to make a POST request. To do so, we need to import requests and execute a function.

HTTP协议有几种方法。 在这种情况下,我们要发出POST请求。 为此,我们需要导入请求并执行一个函数。

import requests
requests.post("https://onesignal.com/api/v1/notifications")

OneSignal wants to verify that only you can send notifications to your website. So you have to add an HTTP header with your Rest API Key from OneSignal.

OneSignal希望验证只有您可以将通知发送到您的网站。 因此,您必须使用OneSignal的Rest API密钥添加HTTP标头。

requests.post(    "https://onesignal.com/api/v1/notifications",    headers={"Authorization": "Basic my-rest-api-key"})

Remember to replace my-rest-api-key with your Rest API Key.

请记住用您的Rest API密钥替换my-rest-api-key

Moreover, you need some basic information about your notification.

此外,您需要有关通知的一些基本信息。

data = {    "app_id": "my-app-id",    "included_segments": ["All"],    "contents": {"en": "Hello"}}
requests.post(    "https://onesignal.com/api/v1/notifications",    headers={"Authorization": "Basic my-rest-api-key"},    json=data)

Replace my-app-id with your own app id. Next you choose who will receive your notifications. Example values are "All", "Active Users", "Inactive Users”. But you can also create your own segments. And for the last one, you add some content of the message in English. If you need another language, you can add it here too.

my-app-id替换为您自己的应用程序ID。 接下来,您选择谁将收到您的通知。 值的示例是"All", "Active Users", "Inactive Users” ,但您也可以创建自己的细分。最后一个,您可以用英语添加一些消息内容。如果您需要其他语言,则可以也将其添加到此处。

That’s it! If you subscribed to the notifications, you should get a push notification.

而已! 如果您订阅了这些通知,则应该收到推送通知。

使用API​​包装器发送通知 (Send notifications using an API Wrapper)

Because my code became kind of messy with many different notifications, I created an API wrapper for OneSignal.

因为我的代码因许多不同的通知而变得混乱,所以我为OneSignal创建了API包装器 。

API包装器 (API Wrapper)

But what is an API wrapper? An API wrapper makes it easier for you to access an API. You can say that it is an API for an API. You call the API wrapper instead of the API directly.

但是什么是API包装器? API包装器使您可以更轻松地访问API。 您可以说这是API的API。 您调用API包装器,而不是直接调用API。

You can install the wrapper called OneSignal-Notifications from pip.

您可以从pip安装称为OneSignal-Notifications的包装器。

pip install onesignal-notifications

Now you can import it and setup your client.

现在,您可以导入它并设置您的客户端。

from onesignal import OneSignal, SegmentNotificationclient = OneSignal("MY_APP_ID", "MY_REST_API_KEY")

To send a Notification, you have to initialize the class SegmentNotification and use the method send.

要发送通知,您必须初始化SegmentNotification类 并使用send方法。

notification_to_all_users = SegmentNotification(    {        "en": "Hello from OneSignal-Notifications"    },    included_segments=SegmentNotification.ALL)client.send(notification_to_all_users)

Maybe this looks kind of unnecessary to you, because it takes even more lines of code. But if you have several notifications, it makes the process much easier and your code more beautiful.

对于您来说,这似乎不必要,因为它需要更多的代码行。 但是,如果您有多个通知,它将使处理过程变得更加容易,代码也将变得更漂亮。

For example if you want to send a notification, which is based on some conditions, the API wrapper has a custom class for that.

例如,如果您要发送基于某些条件的通知,则API包装器为此具有一个自定义类。

from onesignal import OneSignal, FilterNotification, Filterclient = OneSignal("MY_APP_ID", "MY_REST_API_KEY")
filter_notification = FilterNotification(    {        "en": "Hello from OneSignal-Notifications"    },    filters=[        Filter.Tag("my_key", "<", "5"),        "AND",        Filter.AppVersion(">", "5"),        "OR",        Filter.LastSession(">", "1"),    ])

There are many custom parameters you can provide to adapt your notification. For example, you can add buttons to the notification. All list of all parameters can be found here.

您可以提供许多自定义参数来适应您的通知。 例如,您可以在通知中添加按钮。 所有参数的所有列表都可以在此处找到。

from onesignal import OneSignal, FilterNotification, Filterclient = OneSignal("MY_APP_ID", "MY_REST_API_KEY")
filter_notification = SegmentNotification(    {        "en": "Hello from OneSignal-Notifications"    },    web_buttons=[        {          "id": "like-button",          "text": "Like",          "icon": "http://i.imgur.com/N8SN8ZS.png",          "url": "https://github.com/Lanseuo/onesignal-notifications"}    ],    included_segments=SegmentNotification.ALL)

If you want to find out more about OneSignal-Notifications, you can have a look in the GitHub Repository or in the docs.

如果您想了解有关OneSignal-Notifications的更多信息,可以在GitHub Repository或docs中查看。

翻译自: https://www.freecodecamp.org/news/how-to-send-notifications-to-your-web-app-using-python-ba490b893292/

python web应用

python web应用_如何使用Python将通知发送到Web应用相关推荐

  1. 如何学好python web开发_如何学好python web开发

    1.前言 我在没有接触互联网这个行业的时候,就一直好奇网站是怎么构建的.我现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,不过兴趣终归还是要有的,还要自己动手去实践的.Web开 ...

  2. 网易图灵学院python公开课_图灵学院 Python全系列教程全栈工程师 python视频教程下载...

    大家怎么说? 老师很好,我认为,若想学好python,应该多练.多想.多看.学习资料不能仅限于老师给定的这些内容,这些毕竟是入门资料 老师讲的真不错,对于我们这种小白来说 也比较容易懂,虽然有些时候自 ...

  3. python什么环境_什么是Python?(基础环境建设),NO2,搭建

    1. Python语言 1.1 Python语言的基本概念 Python 是⼀种极少数能兼具 "简单" 与 "功能强⼤" 的编程语⾔.你将惊异于发现你正在使⽤的 ...

  4. python框架大全_常用的Python开源框架有哪些?列举这3个

    随着人工智能快速发展,不仅在各个领域应用日益广泛,同时也引发了教学内容的变革和创新.人工智能的火热得益于成功的开源以及深度学习框架的不断涌现.而Python作为一种脚本语言,具有易学.易维护等优点,它 ...

  5. python len函数_知识清单Python必备的69个函数,你掌握了吗?

    本文纲要 Python 作为一门高级编程语言,为我们提供了许多方便易用的内置函数,节省了不少开发应用的时间.目前,Python 3.7 共有 69 个内置函数,一些是我们耳熟能详的函数,另一些却不是很 ...

  6. python优化网站_[练习] 用PYTHON来优化网站中的图片

    我到公司以来,第一次加班,哇,加一晚上加一上午,现在还没下班的迹象,555,困. 对于网站中的一些关键的页面,多重缓存.静态化.程序代码优化--之外,为了提高用户打开页面的速度,图片是必须要优化的. ...

  7. python编写函数_如何用Python编写自己喜欢的R函数

    python编写函数 数据科学和机器学习的伟大现代斗争之一是" Python vs. R". 毫无疑问,近年来两者都已经取得了巨大的发展,成为数据科学,预测分析和机器学习的顶级编程 ...

  8. csdn学院的python培训怎么样_这段 Python 代码让程序员赚 300W,公司已确认!网友:神操作...

    Python到底还能给人多少惊喜? 笔者最近看到了这两天关于Python最热门的话题,关于<地产大佬潘石屹学Python的原因>,结果被这个回答惊到了: 来源:知乎 https://www ...

  9. python框架漏洞_注意!Python中的10个常见安全漏洞及修复方法

    编写安全的代码很困难,当你学习一门编程语言.一个模块或框架时,你会学习其使用方法.在考虑安全性时,你需要考虑如何避免代码被滥用,Python也不例外,即使在标准库中,也存在着许多糟糕的实例.然而,许多 ...

最新文章

  1. iOS开发-开发总结
  2. poj 1269 计算几何
  3. 原始尺寸_螺母尺寸检测,螺丝螺母外观检测设备
  4. 2020-12-11 Python yield 使用浅析
  5. hadoop元数据合并过程_Hadoop元数据合并异常及解决方法
  6. c++判断ftp服务器文件存在性判断_BitTorrent协议与传统文件分发协议对比
  7. jQuery中的 ||
  8. CentOS 6.2安装配置pacemaker
  9. php中文分词nlp,几种常见的PHP中文分词系统
  10. 超高频RFID智慧酒店管理系统解决方案
  11. 计算机光驱启动设置,bios设置光驱启动图文教程
  12. flutter 踩的那些坑 (一) Scheme not starting with alphabetic character
  13. 电脑桌面打开计算机打开多个,如何在电脑桌面并排显示多个Excel工作表
  14. 入夏短裤热 教你怎么穿
  15. java自制语音识别,老司机搞定java使用语音识别将文字转换成语音
  16. 中国移动的指令大全!(不再需要人工台)
  17. 计算机二级vb考试教材,计算机二级考试VB基础题教材.doc
  18. 磨金石教育摄影技能干货分享|王汉冰摄影作品欣赏—《沙狐之眼》
  19. JAVA中如何实现代码优化(技巧讲解)
  20. 你见过最心酸的事是什么?

热门文章

  1. linux下下安装jdk-6u45-linux-x64.bin
  2. 【Linux入门学习之】Linux关机命令总结
  3. Collection的使用 字符串保存 java
  4. c#操作XML文件 1614260503
  5. 草稿 图片盒子定时器模式窗口
  6. Spring Cloud Config Server
  7. 玩转iOS开发:iOS 10 新特性《Thread Sanitizer》
  8. 《C语言深度解剖》学习笔记之内存管理
  9. php中常见的header类型
  10. LINUX考证优惠信息转发(图)