Send FCM messages from server side to android device

前端 未结 7 1505
情话喂你
情话喂你 2020-12-03 05:45

With the new update, FCM is now going to be used.

I tried the sample app from git and it\'s working all fine. I can send notifications from the console.

But

相关标签:
7条回答
  • 2020-12-03 06:19

    You can use this complete code

    <?php
    
    function sendFCM($mess,$id) {
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array (
            'to' => $id,
            'notification' => array (
                    "body" => $mess,
                    "title" => "Title",
                    "icon" => "myicon"
            )
    );
    $fields = json_encode ( $fields );
    $headers = array (
            'Authorization: key=' . "AIzaSyA9vpL9OuX6moOYw-4n3YTSXpoSGQVGnyM",
            'Content-Type: application/json'
    );
    
    $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_POSTFIELDS, $fields );
    
    $result = curl_exec ( $ch );
    curl_close ( $ch );
    }
    
    ?>
    

    Pass message and token id as a parameter to the sendFCM($mess,$id) call.

    0 讨论(0)
提交回复
热议问题