简体   繁体   English

向 curl 和 api v1 主题发送 firebase 通知时出错

[英]Error when sending firebase notification to topics with curl and api v1

in my laravel app I'm using curl to send a notification to all users suscribed to a topic, however when I send the notification I get this error:在我的 laravel 应用程序中,我使用 curl 向所有订阅某个主题的用户发送通知,但是当我发送通知时出现此错误:

"code": 400,
"message": "Invalid JSON payload received. Unknown name \"to\": Cannot find field."

This is how I send topic notif with curl:这就是我使用 curl 发送主题通知的方式:

public function sendTopic($topic,$title,$body, $data , $type, $image='')
    { 
        $client = new Client();
        
        $url = 'https://fcm.googleapis.com/v1/projects/wooloveapp-dda64/messages:send';
       
        $fields = 
        [
            'message' =>  
            [
                "to" => $topic,
                "notification" =>
                [
                    "title" => $title,
                    "body" => $body,
                ],
                "data" =>   [ "data" =>  json_encode($data) ],
                "android" =>
                [
                    "notification" => 
                    [
                        "sound" => "default",
                        "title" => $title,
                        "body" => $body,
                        'tag' => $topic,
                        "channel_id" => "500",
                        
                    ], 
                    "priority" => "high",
                    "ttl" => "86400s"
                    //"badge" => 1
                ],
                "apns" =>
                [
                    "payload" => 
                    [
                        "aps" => [ "sound" => "default" ]
                    ],
                    "headers" => [
                        "apns-priority" => "5",
                        "content_available" => "1"
                    ], 
                ],
                "webpush"=>[
                    "headers"=>[
                      "Urgency"=> "high",
                      //"image" => "https://wooloveapp.com/img/misc/logo-02.jpg"
                    ]
                    ],
            ]
        ];

        $headers = 
        [
            'Authorization: Bearer ' .$this->getGoogleAccessToken(),
            'Accept:application/json',
            'Content-Length:'.strlen(json_encode($fields)),
            'Content-Type:application/json',
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        $result = curl_exec($ch);

        $result =
        [
            'result' => $result,
            //'statusCode' => $statusCode
        ];

        return $result;
    }

You have to use topic instead of to if you're sending to a topic.如果要发送到主题,则必须使用topic而不是to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 静音推送通知 HTTP V1 - Firebase Silence Push Notification with HTTP V1 Firebase 云消息传递 API (V1):缺少服务器密钥 - Firebase Cloud Messaging API (V1): Server Key missing Firebase 消息传递 v1 401 项目不允许错误 - Firebase Messaging v1 401 Project Not Permitted error 我试图通过 firebase 控制台 api 发送通知 - i was trying to sending notification through firebase console api 错误:发布“http://localhost/api/v1/namespaces/kube-system/configmaps”:拨打 tcp 127.0.0.1:80 - Error: Post "http://localhost/api/v1/namespaces/kube-system/configmaps": dial tcp 127.0.0.1:80 在哪里可以找到 Firebase 服务器密钥,我已启用云消息传递 API V1 但我找不到它 - Where to find Firebase server key, I have enabled Cloud Messaging API V1 but i can't find it ERROR: (gcloud.beta.container.clusters.create) ResponseError: code=400, message=v1 API 无法用于访问 GKE 区域集群 - ERROR: (gcloud.beta.container.clusters.create) ResponseError: code=400, message=v1 API cannot be used to access GKE regional clusters 通过 Sendgrid API 发送电子邮件时出错 - Error when sending email through Sendgrid API 如何为 firebase 获取 HTTP V1 访问令牌? - how to get HTTP V1 Access Token for firebase? Firebase:发送通知 REST API - Firebase : Send notification with REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM