How to send FCM notification to app from web

后端 未结 5 597
鱼传尺愫
鱼传尺愫 2020-12-23 10:51

I am developing chat app which is based on Firebase Database and Storage. Everything is working fine, but now I need implementation of FCM to receive notification on app whe

5条回答
  •  再見小時候
    2020-12-23 11:19

    You can send the post request without curl (which was not available on my server)

    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;
    }
    

提交回复
热议问题