"this site has been updated in the background" chrome 使用 php 进行 firebase 云消息传递时的通知

"this site has been updated in the background" chrome notification when using php for firebase cloud messaging

我想通过 firebase 向用户(Web 和移动)发送通知,因为我正在开发一个渐进式网络应用程序。

我按照他们的教程在 Firebase 中创建了一个项目。一切正常,我还测试了使用 Curl 发送通知,它在计算机和移动设备上都能正常工作。

curl --header "Content-Type: application/json" \
     --header "Authorization: key=<MY-FIREBASE-SERVER-KEY>" \
     -d '{
           "notification": {
             "title": "New Message",
             "body": "Hello, How are you ?",
             "icon": "/images/profile_placeholder.png",
             "click_action": "http://localhost:5000"
           },
           "to": "MY-CLIENT-FCM-TOKEN"
         }' \
     https://fcm.googleapis.com/fcm/send

它工作正常并显示通知。

但是当我使用 PHP 发送通知时,它会发送通知,但消息为 "this site has been updated in the background"。

这是 php 文件:pwa_demo2.php

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', '<MY-FIREBASE-SERVER-KEY>' );

// prep the bundle
$msg = array
(
    'message'   => 'Hello, How are you ?',
    'title'     => 'New Message'

);
$fields = array
(
    'to'    => 'MY-CLIENT-FCM-TOKEN',
    'data'  => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );

if($result === false)
{
    die('Curl failed: '.curl_error($ch));
}
curl_close( $ch );
echo $result;

?>

所以请帮助我,因为我被困在这个问题上好几个小时了...

您的 PHP 代码没有发送与 CURL 命令行相同的消息。

尝试打印 json_encode( $fields ) 的输出并检查它是否与您使用 -d 选项

传递给 CURL 命令行的有效负载相同