必备知识

什么是推弹

用于在多个设备(PC、智能手机、平板电脑等)之间共享信息的应用程序。
如果您使用的是 Android,它支持在设备之间镜像通知。

什么是网络套接字

粗略地说,它是一种实时通信协议。
在这里了解更多。

我做了什么

通过 Pushbullet API 使用 Python 脚本在您的 Android 设备上获取通知。
由于我们使用 websockets 作为实时通信的手段,我们将使用 Python 库“websockets”。

示例代码

import asyncio
import websockets
import json
import logging

# デバッグレベルのロガーを追加しておくと何かと助かります。必須ではないです。
logging.basicConfig(
    format="%(asctime)s %(message)s",
    level=logging.DEBUG,
)


async def process(msg):
    event = json.loads(msg)
    # Pushbulletから30秒に1回送信される {"type": "nop"} を無視
    if event.get("type", "nop") != "nop":
        p = event.get("push", {})
        title = p.get("title")
        body = p.get("body")
        app_name = p.get("application_name")
        print(title, body, app_name)


async def __main():
    async for websocket in websockets.connect("wss://stream.pushbullet.com/websocket/<KEY>",
                                              logger=logging.getLogger("websockets.server")):
        try:
            async for message in websocket:
                await process(message)
        except websockets.ConnectionClosed:
            continue


if __name__ == '__main__':
    asyncio.run(__main())

运行此脚本时,在 Pushbullet 应用程序中“镜像”→“发送测试通知”

Pushbullet + websocketsでリアルタイムに通知を受け取る

Test notification If you see this on your computer, notification mirroring is working! Pushbullet

应该收到推送,并且应该喷出此消息。


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308622614.html

相关文章: