【发布时间】:2018-05-12 11:37:50
【问题描述】:
我正在研究跌倒检测系统。我编写了 Arduino 代码并连接到 Firebase。所以现在我有两个状态为 1 或 0 的变量,并且我创建了一个移动应用程序,以便在系统检测到跌倒 Firebase+Pusher 时接收自动推送通知。我用 PyCharm 编写了这个 Python 代码,并使用流函数从 Firebase 读取实时数据并发送自动通知。该代码适用于变量“Fall_Detection_Status”,我能够在每次跌倒检测时正常接收推送通知。但我尝试修改代码以从另一个变量“Fall_Detection_Status1”读取数据,如果两个变量都给出 1,我希望我的代码现在发送通知。我想出了这段代码,但似乎最后一个 if 语句不起作用,因为我无法接收通知,并且 if 语句末尾的 print(response['publishId']) 没有显示任何结果。
那么有什么问题呢?
import pyrebase
from pusher_push_notifications import PushNotifications
config = {
'apiKey': "***********************************",
'authDomain': "arfduinopushnotification.firebaseapp.com",
'databaseURL': "https://arduinopushnotification.firebaseio.com",
'projectId': "arduinopushnotification",
'storageBucket': "arduinopushnotification.appspot.com",
'messagingSenderId': "************"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
pn_client = PushNotifications(
instance_id='*****************************',
secret_key='**************************',
)
value = 0
value1 = 0
def stream_handler(message):
global value
print(message)
if message['data'] is 1:
value = message['data']
return value
def stream_handler1(message):
global value1
print(message)
if message['data'] is 1:
value1 = message['data']
return value1
if value == 1 & value1 == 1:
response = pn_client.publish(
interests=['hello'],
publish_body={
'apns': {
'aps': {
'alert': 'Hello!',
},
},
'fcm': {
'notification': {
'title': 'Notification',
'body': 'Fall Detected !!',
},
},
},
)
print(response['publishId'])
my_stream = db.child("Fall_Detection_Status").stream(stream_handler)
my_stream1 = db.child("Fall_Detection_Status1").stream(stream_handler1)
【问题讨论】:
-
如果您已将其设为全局,则不需要返回该值。尝试取出返回值。这可能会有所帮助
-
还是不行
-
也许消息是
str.. 试试value = int(message['data']) -
也可以尝试在
if语句中使用and而不是& -
没用 :(
标签: python firebase firebase-realtime-database push-notification