如何从网络向应用程序发送FCM通知

Rit*_*itu 13 php firebase firebase-realtime-database firebase-cloud-messaging firebase-storage

我正在开发基于Firebase数据库和存储的聊天应用程序.一切都运行正常,但现在我需要实施FCM,以便在应用程序处于后台或前台时接收应用程序通知.我找不到在PHP中实现的方法来监听firebase数据库中的任何更改,如果有任何更改,则将推送通知发送到应用程序.

有很多代码可以从PHP发送通知,但没有一个基于Firebase数据库,甚至官方文档都有我的共享主机不支持的Node.js指南.

我已经在我的应用程序端实现了FCM代码,该代码已经过Firebase控制台测试.

这是我的Firebase数据库结构 Firebase数据库结构

med*_*eda 26

发送推送通知只是向FCM服务器发送发布请求的问题.

这是工作示例:

$data = json_encode($json_data);
//FCM API end-point
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'YOUR_KEY';
//header with content_type api key
$headers = array(
    'Content-Type:application/json',
    'Authorization:key='.$server_key
);
//CURL request to route notification to FCM connection server (provided by Google)
$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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ($result === FALSE) {
    die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

JSON工资负载示例:

[
    "to" => 'DEVICE_TOKEN',
    "notification" => [
        "body" => "SOMETHING",
        "title" => "SOMETHING",
        "icon" => "ic_launcher"
    ],
    "data" => [
        "ANYTHING EXTRA HERE"
    ]
]
Run Code Online (Sandbox Code Playgroud)

  • 你好,我已经关注了很多像你这样的例子,但我没有收到任何通知,即使 CURL 响应是肯定的,通知也不会列在 Firebase 控制台中.. status: 200 string(143) "{"multicast_id" :8573812091209374332,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1550852694105682%0762e9f8f9f9f9f9fd}当我通过控制台将它发送到firebase"时它工作的相同设备令牌,知道吗?从 Firebase Cloud Firestore 获取的设备令牌 (2认同)

Ria*_*lam 18

尝试这个代码它适用于我的Android以及iOS

<?php
    $url = "https://fcm.googleapis.com/fcm/send";
    $token = "your device token";
    $serverKey = 'your server token of FCM project';
    $title = "Notification title";
    $body = "Hello I am from Your php server";
    $notification = array('title' =>$title , 'body' => $body, 'sound' => 'default', 'badge' => '1');
    $arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
    $json = json_encode($arrayToSend);
    $headers = array();
    $headers[] = 'Content-Type: application/json';
    $headers[] = 'Authorization: key='. $serverKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    //Send the request
    $response = curl_exec($ch);
    //Close request
    if ($response === FALSE) {
    die('FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
?>
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回复,我们需要将参数更改为"registration_ids",否则会显示错误 (2认同)

Rut*_*wik 5

您可以改用 Postman。在 chrome 中打开 Postman 扩展并使用 POST url https://fcm.googleapis.com/fcm/send

在此处输入图片说明

在此处输入图片说明


Amb*_*óth 5

您可以发送没有卷曲的帖子请求(我的服务器上没有)

sendNotification("New post!", "How to send a simple FCM notification in php", ["new_post_id" => "605"], "YOUR_SERVER_KEY");

function sendNotification($title = "", $body = "", $customData = [], $serverKey = ""){
    if($serverKey != ""){
        ini_set("allow_url_fopen", "On");
        $data = 
        [
            "to" => '/topics/new_post',
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => $customData
        ];

        $options = array(
            'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $data ),
                'header'=>  "Content-Type: application/json\r\n" .
                            "Accept: application/json\r\n" . 
                            "Authorization:key=".$serverKey
            )
        );

        $context  = stream_context_create( $options );
        $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
        return json_decode( $result );
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)