android - 获取 Firebase 推送通知 Android 的问题

标签 android firebase-cloud-messaging

我刚刚使用 Firebase 在我的应用程序中集成了推送通知。我已经阅读了 FCM docs 中的全部说明。 .

它只是自动生成服务器 key 并为我提供了 google-services.json 文件,我将其放在应用程序的根文件夹中。以下是 key 的屏幕截图。

然后我在 Android 应用程序的 key 限制下添加了我的包名称和 SHA1-Fingerprints 选项。我还在控制台中启用了 GCM。

让我发疯的是当我从控制台发送通知时,我收到了它,但是当我尝试从我的服务器发送它时,它显示错误 401 Unauthorized

那么我需要在后端更改我的代码吗?我在后端使用服务器 key 发送推送通知,我是否也需要将发件人 ID 发送到后端? Google 是否更改了它的机制?

我的 FCM 后端代码:

$fields = array (
            'registration_ids' => array (
                    $id
            ),
            'data' => array (
                    "message" => $message
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "YOUR_KEY_HERE",
            '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_POSTFIELDS, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );

对于 GCM:

static function send_notifications($registatoin_ids, $messages) { 

        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $messages,
        );
        $apiKey='AIzaSyDzto66Dq0neU3AKjujr0eCEn2glWuk0-E';

        $headers = array(
            //'Authorization: key = AIzaSyBpZ3t68mtXRa4oRft9fmbaHaXFb-H_k-8',
             'Authorization: key=' . $apiKey,
            'Content-Type: application/json'
        );

        $ch = curl_init();


        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        //echo "<pre>";
//print_r($result);die;
        if ($result === FALSE) {
           die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);

        return $result;
    }

谢谢。

enter image description here

最佳答案

你提到

Then I just added my package name and SHA-Fingerprints with selection on Android app under Key Restrictions. I have also enabled the GCM in the console.

在 Google Developers Console 的新界面中,似乎不再有选择要生成的 API key 类型的选项,但是,您仍然可以提供一些限制。根据您所说的,由于您为 Android 选择了限制,因此 API key 将被视为 Android key 。

关于 FCM(甚至 GCM)的事情是必须使用的 API key 是服务器 key 。我的建议是尝试更改对 IP 服务器地址的限制,添加您的服务器 IP 地址,然后测试推送通知。

关于android - 获取 Firebase 推送通知 Android 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39483285/

相关文章:

firebase - 带有推送通知的深层链接 - FCM - Android

android - 如何从 Firebase 云消息中注销电话

android - 在模拟器中写入/系统/框架

android - 您的 APP_BUILD_SCRIPT 使用 Android ndk-build 指向一个未知文件

java - 计算缺失数字的数量

java - 如何修复响应空的集成 Firebase 应用程序(云消息)?

android - 类 'MyFirebaseMessagingService' 不是抽象的,也没有实现抽象基类成员 public abstract fun zzd(p0 : Intent! )

java - 在超时时重试 Android Socket 连接

Android:按下后退按钮时刷新以前的 fragment 数据

android - Firebase 云消息传递错误 : Couldn't resolve host 'fcm.googleapis.com'