首页 > 解决方案 > 我如何每 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'

标签: pythonfirebasetimerpush-notification

解决方案


您的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'])

推荐阅读