【问题标题】:Firebase-admin-python: configure message so that it will be received in background by Android deviceFirebase-admin-python:配置消息,以便 Android 设备在后台接收
【发布时间】:2020-06-17 13:41:02
【问题描述】:

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

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

response = messaging.send(message)

但在发布后,我的 Android 设备拒绝在后台接收消息,除非其优先级设置为“高”。显然我不能只在我的旧简单配置中添加一个“优先级”键(我试过并得到一个 ValueError:Message.android must be an instance of 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

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

【问题讨论】:

    标签: python firebase-cloud-messaging firebase-admin


    【解决方案1】:

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

    如果您想传递到特定令牌,请将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)
    

    【讨论】:

    • 非常感谢,这是合乎逻辑的,而且有效! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2016-11-17
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多