firebase 如何在python中从多个应用程序发送推送消息

cgvd09ve  于 2022-12-04  发布在  Python
关注(0)|答案(1)|浏览(144)

我有两个不同的应用程序与2 different credentialsfirebase。所以firebase_admin是2倍initialised
进口火药库

from firebase_admin import credentials, messaging

初始化2个firebase_admins并分配json凭证。

#initialize firebase 1
json_file_first = location of the json one
credentials_first = credentials.Certificate(json_file_first)
first_app = firebase_admin.initialize_app(credentials_first,name="first")

#initialize firebase 2
json_file_second = location of the json second
credentials_second = credentials.Certificate(json_file_second)
second_app = firebase_admin.initialize_app(credentials_second, name="second")

创建和发送消息:

def sendPushNotificationTest(title, msg, registration_token):
    message = messaging.MulticastMessage(    //here its using the default instance of firebase_admin
        notification=messaging.Notification(
        title=title,
        body=msg),
        tokens=registration_token

        )
      respons = messaging.send_multicast(message)

当创建/发送推送消息时,可以使用firebase_admin“first”或firebase_admin“second”。我在哪里分配正确的初始化firebase_admin。

mpbci0fu

mpbci0fu1#

我确实找到了解决方案,它在消息本身下面的response中。你可以添加firebase_admin示例。

def sendPushNotificationTest(title, msg, registration_token):
    message = messaging.MulticastMessage(   ///i think i have to define the sender before messaging
        notification=messaging.Notification(
        title=title,
        body=msg),
        tokens=registration_token

        )
 respons = messaging.send_multicast(message, app="first") //assign the right instance

相关问题