使用最新的 HTTP 将 FCM 通知发送到 PHP 中的多个令牌

问题描述 投票:0回答:1

我正在尝试使用 PHP 向多个令牌发送通知,但它不起作用。我能够向单个令牌发送通知,但在多个设备上收到错误。有人可以帮忙吗?

多重通知:-

    $deviceTokens = $token_ids; // Add your device tokens to this array

    $fcmNotifications = [];

    foreach ($deviceTokens as $token_indv) {
        $fcmNotifications[] = [
            'message' => [
                'token' => $token_indv,
                'notification' => $notification,
            ],
        ];
    }

    $headers = [
        'Authorization: Bearer ' . $accessToken,
        'Content-Type: application/json'
    ];
    
    echo json_encode($fcmNotifications);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$fcmUrl);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($fcmNotifications));  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
    echo $server_output;

我正在尝试向多个令牌发送通知。

php firebase-cloud-messaging
1个回答
0
投票

以下是生成的 JSON:- ''' [{"message":{"token":"coa9vySeTOKxy....","notification":{"title":"一些标题","body":"一些消息"}}}, {"message":{"token":"d-dfqCOhRgyiwQw...","notification":{"title":"一些标题","body":"一些消息"}}}] ''' 我收到的错误如下:- ''' {“error”:{“code”:400,“message”:“收到无效的 JSON 负载。未知名称“”:根元素必须是消息。”,“status”:“INVALID_ARGUMENT”,“details”:[ { "@type": "type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ { "description": "收到无效的 JSON 负载。未知名称 "":根元素必须是消息。" } ] } ] } } '''

© www.soinside.com 2019 - 2024. All rights reserved.