首页 > 解决方案 > Firebase 管理员发送多播没有被调用或工作

问题描述

我正在开发 django 项目,在该项目中我需要使用 firebase admin sdk 向用户发送通知。但是当我尝试发送通知时,我看不到messaging.send_multicast(message)的日志。谁可以帮我这个事?

message = messaging.MulticastMessage(
 data={
 'data': str(self.data)
 },
 tokens=registration_tokens,
)
print('message', message)
response = messaging.send_multicast(message)
print('response', response.success_count, response.failure_count)

标签: pythondjangofirebase-cloud-messagingfirebase-admin

解决方案


上面发送多播数据消息的代码是正确的,应该可以工作。你有任何错误吗?

您还确保使用正确的凭据初始化 Firebase 管理 SDK:

import firebase_admin
from firebase_admin import credentials, messaging

cred = credentials.Certificate(<credentials_path>)
default_app = firebase_admin.initialize_app(cred)

如果要发送在用户手机上可见的通知,应使用该notification属性,如下所示:

message = messaging.MulticastMessage(
    notification=messaging.Notification(
        title="Notification Title",
        body="Notification Text",
    ),
    tokens=registration_tokens
)

response = messaging.send_multicast(message)
print('response', response.success_count, response.failure_count)

推荐阅读