Firebase-admin-python:配置消息以便 Android 设备在后台接收它

Firebase-admin-python: configure message so that it will be received in background by Android device

我有一个 celery 任务,它通过 firebase-admin 向我的客户端发送消息。在开发中,我曾经像这样发送通知并且一切正常(我的 React-Native 应用程序按预期在前台和后台接收消息):

message = messaging.Message(
        data={
            'text': msg.text,
        },
        token=registration_token,
    )

response = messaging.send(message)

但发布后,我的 Android 设备拒绝在后台接收消息,除非其优先级设置为 'high'。显然我不能只向我的旧简单配置添加一个 'priority' 键(我尝试并得到一个 ValueError:Message.android 必须是 Android.config class 的一个实例)。所以我找到了一个如何在 firebase-admin docs:

中设置优先级的例子
def android_message():
# [START android_message]
message = messaging.Message(
    android=messaging.AndroidConfig(
        ttl=datetime.timedelta(seconds=3600),
        priority='normal',
        notification=messaging.AndroidNotification(
            title='$GOOG up 1.43% on the day',
            body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
            icon='stock_ticker_update',
            color='#f45342'
        ),
    ),
    topic='industry-tech',
)
# [END android_message]
return message

现在我的问题是,如何在这个更详细的消息配置中设置注册令牌?令牌现在去别的地方了吗?我想我在这里遗漏了一些非常明显的东西,因此将不胜感激任何帮助和提示。

您找到的示例将消息发送到主题,该主题是客户端可以订阅的命名键。所以不需要令牌发送到这里,因为它是一个 public/subscribe 传递系统。

如果您想交付给特定令牌,请将 topic 密钥替换为 token,如 sending messages to specific devices 文档中的示例所示:

# This registration token comes from the client FCM SDKs.
registration_token = 'YOUR_REGISTRATION_TOKEN'

 # See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    token=registration_token,
)

# Send a message to the device corresponding to the provided
# registration token.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)