Android FCM推送通知的关键在哪里?

发布于 2025-02-03 09:10:19 字数 1838 浏览 3 评论 0原文

我安装了Firebase,如果我通过Firebase Console发送手动推动通知,我的应用程序会收到它。

我正在尝试使用PHP脚本将通知发送到FCM。

问题是我不知道我应该使用什么密钥?

如果我在firebase控制台中使用一个,我会遇到此401错误:“无效键”

”在此处输入图像说明“

如果我在API&服务,我遇到了这个401错误“ project_not_permitting”

”在此处输入图像描述

这是我正在使用的PHP脚本:

$server_key = ""
$token = ""
$title = ""
$body = ""
$curl = curl_init();
    $authKey = "key=" . $server_key;
    $registration_ids = $token;
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => '{
                    "registration_ids": ' . $registration_ids . ',
                    "notification": {
                        "title": ' . $title . ',
                        "body": ' . $body . '
                    }
                }',
    CURLOPT_HTTPHEADER => array(
        "Authorization: " . $authKey,
        "Content-Type: application/json",
        "cache-control: no-cache"
    ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo "no error: " . $response;
    }

当然,它在Else语句中打印我的回声:

“ no Orror:401 ...

”可以帮我吗?

I installed firebase, and if I send manually push notification through firebase console, my app receives it.

I'm trying to send a notification to FCM using a php script.

The problem is I don't know what key I'm supposed to use ?

If I use the one in firebase console, I got this 401 error: "Invalid Key"

enter image description here

If I use the one in Api & Services, I got this 401 error "PROJECT_NOT_PERMITTED"

enter image description here

This is the php script I'm using:

$server_key = ""
$token = ""
$title = ""
$body = ""
$curl = curl_init();
    $authKey = "key=" . $server_key;
    $registration_ids = $token;
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => '{
                    "registration_ids": ' . $registration_ids . ',
                    "notification": {
                        "title": ' . $title . ',
                        "body": ' . $body . '
                    }
                }',
    CURLOPT_HTTPHEADER => array(
        "Authorization: " . $authKey,
        "Content-Type: application/json",
        "cache-control: no-cache"
    ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo "no error: " . $response;
    }

Of course, it's printing my echo in else statement:

"no error: 401 ..."

Does someone can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2025-02-10 09:10:19

尝试在Firebase Console项目设置中启用云消息传递API(旧)。

Try to enable Cloud Messaging API (Legacy) in Firebase Console project settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文