php - 通过 FCM google-php-api HTTPv1 API 发送推送消息

标签 php firebase firebase-cloud-messaging google-oauth google-api-php-client

我正在尝试使用 Google PHP API 客户端 https://github.com/googleapis/google-api-php-client/通过 Google HTTPv1 API 发送 FCM 推送通知。

require_once 'vendor/autoload.php'; //-- loading the google api client

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account/key/project-sfk28as8ff.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();


// Your Firebase project ID
$project = "push-test-5f923";
// Creates a notification for subscribers to the debug topic
$message = [
    "message" => [
        "token" => "cO5hrNMFKQI:APA91bFm.......6IYy1phlxIJx2ZNA1",
        "notification" => [
            "body" => "This is an FCM notification message!",
            "title" => "FCM Message",
        ]
    ]
];
// Send the Push Notification - use $response to inspect success or errors
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);

var_dump($response);

这是实际的响应:

object(GuzzleHttp\Psr7\Response)#61 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(9) "Forbidden" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(403) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["Vary"]=> array(3) { [0]=> string(8) "X-Origin" [1]=> string(7) "Referer" [2]=> string(22) "Origin,Accept-Encoding" } ["Content-Type"]=> array(1) { [0]=> string(31) "application/json; charset=UTF-8" } ["Date"]=> array(1) { [0]=> string(29) "Thu, 27 Sep 2018 13:24:52 GMT" } ["Server"]=> array(1) { [0]=> string(3) "ESF" } ["Cache-Control"]=> array(1) { [0]=> string(7) "private" } ["X-XSS-Protection"]=> array(1) { [0]=> string(13) "1; mode=block" } ["X-Frame-Options"]=> array(1) { [0]=> string(10) "SAMEORIGIN" } ["X-Content-Type-Options"]=> array(1) { [0]=> string(7) "nosniff" } ["Alt-Svc"]=> array(1) { [0]=> string(40) "quic=":443"; ma=2592000; v="44,43,39,35"" } ["Accept-Ranges"]=> array(1) { [0]=> string(4) "none" } ["Transfer-Encoding"]=> array(1) { [0]=> string(7) "chunked" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["vary"]=> string(4) "Vary" ["content-type"]=> string(12) "Content-Type" ["date"]=> string(4) "Date" ["server"]=> string(6) "Server" ["cache-control"]=> string(13) "Cache-Control" ["x-xss-protection"]=> string(16) "X-XSS-Protection" ["x-frame-options"]=> string(15) "X-Frame-Options" ["x-content-type-options"]=> string(22) "X-Content-Type-Options" ["alt-svc"]=> string(7) "Alt-Svc" ["accept-ranges"]=> string(13) "Accept-Ranges" ["transfer-encoding"]=> string(17) "Transfer-Encoding" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#49 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(31) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } }

我不断收到的响应基本上是 Forbidden 403。(我也可以在谷歌开发者控制台中看到它)

Firebase Cloud Messaging API 与 Google Cloud Messaging 一样在开发者控制台中启用(尽管它们不相关)。现在我认为发生这种情况是因为我没有执行 oAuth(没有获取访问 token )但我不确定如何获取它,因为 google-api-php-client 的文档不是很详细。

任何人都可以告诉我google-api-php-client 中有哪些函数可以用来获取一次性访问代码,然后可以使用该代码来获取< em>firebase 的访问 token ,以便我可以通过 cURL 进行发布? (如何为 Firebase 项目的服务帐户获取有效的 Oauth 2.0 token )

最佳答案

终于明白了。似乎是 Google Developer Console [https://console.developers.google.com]它在云服务方面的能力有限。 (或者至少有关此类服务的文档已过时)

正确的做法是只使用 Firebase 控制台中直接提供的 API 凭据 [https://console.firebase.google.com] .

enter image description here

或者,如果您喜欢冒险,可以使用 Google Cloud Console [https://console.cloud.google.com]只要您确保您创建的服务帐户具有此处 Google 文档中提到的编辑者或所有者权限 [https://firebase.google.com/docs/cloud-messaging/auth-server] .

我发现 Google Cloud Console 过于复杂,所以我建议坚持使用 Firebase Console。

关于php - 通过 FCM google-php-api HTTPv1 API 发送推送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538406/

相关文章:

c# - 使用 PHP 对 ASP.NET 成员身份中的用户进行身份验证

php - 显示 MySQL 值

php - 单元测试: Beginner Questions

unity3d - Google.JarResolver.ResolutionException : Cannot resolve com. google.firebase :firebase-analytics-unity:1. 0.0()

android - 如何在android studio中获取(FCM) token ?

php - 如何编写php代码来更新web服务

firebase - 如何绕过 Firebase Hosting 的默认 Vary on Authorization header

java - 删除节点的所有子节点,同时保留对 Firebase 中父节点的引用

android - Cordova-Plugin-Firebase 当应用程序从 OFF 变为 ACTIVE 时,onNotificationOpen() 在 Android 上不起作用

Android:应用程序服务器上的 GCM 推送通知错误 "SENDER_ID_MISMATCH"