当前位置: 首页 > 知识库问答 >
问题:

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

陈昂熙
2023-03-14

我正在尝试使用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) { } } }

我一直得到的反应基本上是禁止的。(我也可以在谷歌开发者控制台中看到)

Firebase Cloud Messaging API与Google Cloud Messaging一样在开发人员控制台中启用(尽管不相关)。现在我想这是因为我没有做oAuth(没有获取访问令牌),但是我不确定如何获取它,因为GoogleAPI php客户端的文档不是很详细。

有人能告诉我google api php客户端中有哪些函数可以获取一次性访问代码,然后可以使用这些代码获取firebase的访问令牌,这样我就可以通过cURL进行发布了?(如何为Firebase项目的服务帐户获取有效的Oauth 2.0令牌)

共有1个答案

姬衡
2023-03-14

我终于明白了。它似乎是谷歌开发者控制台[https://console.developers.google.com]在云服务方面,它所能做的有些有限。(或至少有关此类服务的文件已过时)

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

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

我发现谷歌云控制台过于复杂,所以我建议坚持使用Firebase控制台。

 类似资料:
  • 我正在尝试使用Postman通过云消息服务发送一次推送通知。 这是一个用于相同目的的工作命令,我将其用作参考。 到目前为止我所做的。。 1-适当设置标题 2-在Body,我使用 执行时,我返回

  • 如何通过Azure从我的UWP-App向不同设备上的应用程序的其他实例发送推送通知? 以下是注册设备以接收推送的说明。(这是可行的)第二部分是关于如何在控制台应用程序上发送推送(这也是可行的)https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-star

  • 问题内容: 这是我的使用PARSE发送推送消息的应用程序代码 上面的代码在哪里传递我的请求参数(正文)?如何将我的请求构建为JSON格式?在此先感谢您。请帮助我解决此问题。 问题答案: 使用.net中的parse运行波纹管代码以进行推送通知。

  • 我正在开发一个NativeScript Android应用程序,其中我希望用户能够在按下一个按钮后打开WhatsApp联系人(只知道电话号码)。我目前使用Nativescript-open-app打开WhatsApp。是否也可以打开对话? 要打开WhatsApp,我使用以下代码(也许可以修改“com.WhatsApp”?):

  • 我想通过javascript代码发送gcm消息。为此,我们需要发布一个json对象。 gcm文档:http://developer.android.com/google/gcm/adv.html.中给出了url和json对象格式 出于测试目的,我编写了一个完美运行的Java代码。但是爪哇脚本代码不起作用。如果有人有一些示例工作代码(gcm的爪哇脚本),请发布。 脚本代码 :