python - 我如何每 30 秒重复一次推送通知
问题描述
目前我正在尝试根据firebase中的值通过在后台运行python来向我的应用程序发送通知。因此,当 firebase 中的值从 0 变为 1 时,它会发送通知。所以现在我正在尝试每 30 秒重复发送一次通知(至于测试)。我使用线程库在我的 python 代码中使用 Timer。但是我在运行它时遇到了错误。
推送器.py
import pyrebase
import threading
from pusher_push_notifications import PushNotifications
config = {
'apiKey': "apiKey",
'authDomain': "authDomain",
'databaseURL': "databaseURL",
'projectId': "projectId",
'storageBucket': "storageBucket",
'messagingSenderId': "messagingSenderId",
'appId': "appId",
'measurementId': "measurementId"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
beams_client = PushNotifications(
instance_id='instance_id',
secret_key='secret_key',
)
def stream_handler(message):
print(message)
if(message['data'] is 1):
response = beams_client.publish_to_interests(
interests=['hello'],
publish_body={
'apns': {
'aps': {
'alert': 'Hello!',
},
},
'fcm': {
'notification': {
'title': 'Alert!',
'body': 'It is starting to flood, please remove your car immediately!',
},
},
},
)
print(response['publishId'])
t = threading.Time(5,stream_handler)
t.starting()
my_stream = db.child("FLOOD_STATUS").stream(stream_handler,None)
我收到此错误消息。
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/home/ubuntu/.local/lib/python3.6/site-packages/pyrebase/pyrebase.py", line 563, in start_stream
self.stream_handler(msg_data)
File "pusher.py", line 42, in stream_handler
print(response['publishId'])
UnboundLocalError: local variable 'response' referenced before assignment
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 1182, in run
self.function(*self.args, **self.kwargs)
TypeError: stream_handler() missing 1 required positional argument: 'message'
解决方案
您的print(response['publishId'])
语句位于if(message['data'] is 1):
您定义和填充它的块之外。解决方案是缩进print
语句:
if(message['data'] is 1):
response = beams_client.publish_to_interests(
interests=['hello'],
publish_body={
'apns': {
'aps': {
'alert': 'Hello!',
},
},
'fcm': {
'notification': {
'title': 'Alert!',
'body': 'It is starting to flood, please remove your car immediately!',
},
},
},
)
print(response['publishId'])
推荐阅读
- flutter - 在 listtile 标题上颤动覆盖图像
- python - 这种排序算法可以被认为是冒泡排序的变体吗?
- php - 使用 PHP 电子表格从数据库下载到 Excel 文件时出错
- vue-formulate - 重置方法 VueFormulate 不适用于 InputFile
- python - 来自 str 的 Python ObjectId
- c# - 我需要帮助让 PostMessage 用鼠标点击
- python-3.x - 当客户端断开连接时,Django View中的Python请求停止请求
- c++ - 如何在 C++ 中模拟 Cheat Engine 的“找出该指令访问的地址”?
- c# - 如何在setter中打印类型为整数的方法中的字符串?
- firebase - 在 Expo Go 应用程序中使用 Expo Firebase Analytics 不起作用