FCM/GCM推送通知客户端
项目描述
一个用于订阅GCM/FCM并在Python应用程序内接收通知的库。
何时使用firebase-messaging ?
我想在Python应用程序中接收使用Firebase Cloud Messaging发送的推送通知。
何时不使用firebase-messaging ?
我想发送推送通知(请使用Firebase SDK代替)
我的应用程序运行在FCM支持的平台上(Android、iOS、Web)。
安装
PyPi
$ pip install firebase-messaging
需求
用于接收通知的Firebase配置
用法
必须在asyncio事件循环内运行。
python
from firebase_messaging import FcmPushClient, FcmRegisterConfig
def on_notification(obj, notification, data_message):
# Do something with the notification
pass
credentials = None # Start off with none or load from previous save
def on_credentials_updated(creds):
# save the credentials to a file here for future use
fcm_config = FcmRegisterConfig(fcm-project-id, fcm-app-id, fcm-api-key, fcm-message-sender-id)
pc = FcmPushClient(on_notification, fcm_config, credentials, on_credentials_updated)
fcm_token = await pc.checkin_or_register()
await pc.start()
# Adapt the following for your usage
while some_condition_to_keep_listening:
asyncio.sleep(2)
归属
代码最初基于Matthieu Lemoine的typescript/node实现。更多详情请参阅这篇博客文章。
由lolisamurai转换为Python。
decrypt.py中的http解密逻辑由Martin Thomson提供。