php - 使用PHP发送Firebase通知

标签 php firebase firebase-cloud-messaging

每个人!我在使用PHP发送FIrebase通知时遇到问题。从Firebase控制台发送时,会收到通知,但是从PHP发送时,不会收到任何通知。

你知道是什么问题吗?

这是我的PHP代码:

<?php
$message = 'ojlaasdasdasd';
$title = 'ojla';
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';    $server_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5LnDZO2BpC2aoPMshfKwRbJAJfZL8C33qRxxxxxxxxxxxxL6';
$key = 'eqnlxIQ1SWA:APA91bGf1COAZamVzT4onl66_lEdE1nWfY7rIADcnt9gtNYnw7iWTwa7AYPYTHESFholkZ89ydCQS3QeL-lCIuiWTXiqoDREO0xhNdEYboPvqg8QsBYkrQVRlrCLewC4N-hHUja1NG4f';
$headers = array(
                'Authorization:key=' .$server_key,
                'Content-Type: application/json');

$fields = array
            (
                'to'        => $key,
                'notification'  => array('title' => $title,'body' => $message)
            );


$payload = json_encode($fields);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '$path_to_fcm' );
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_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt( $ch,CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
echo $result;
curl_close($ch);


?>

我没有从服务器得到任何回音

最佳答案

试试这个代码,对我来说很好用。只需更改 key 和 token

<?php
define('API_ACCESS_KEY','Api key from Fcm add here');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='235zgagasd634sdgds46436';

     $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        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($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);


        echo $result;

关于php - 使用PHP发送Firebase通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454398/

相关文章:

php - Apache - htdocs 和/var/www/html

firebase - 如何在 Firestore 云功能中使用通配符?

php - 将 Firebase Cloud Messaging 发送到手机设备

PHP Foreach 循环打印所有表情符号

php - 在 PHP 中包含 XML 编码

firebase - 如何在Fire Base中存储图像并在Firestore中存储URL

firebase - 为小部件调用两个不同端点的最佳实践?

ios - 通过同一个 Firebase 项目向多个应用程序传送 PushNotification、FCM

php - TestFlight 中未收到 iOS FCM 推送通知

PHP Laravel 读取 csv