PHP FireBase(FCM)脚本显示如何发送Android推送通知

时间:2017-10-11 13:18:54

标签: wordpress

简单的PHP FireBase(FCM)脚本,显示如何发送Android推送通知。请务必使用Google API控制台页面中的正确替换$ apikey。

1 个答案:

答案 0 :(得分:0)

<?php
#API access key from Google API's Console
    $apikey= 'YOUR-SERVER-API-ACCESS-KEY-GOES-HERE';
    $registrationIds = $_GET['id'];
#prep the bundle
     $msg = array
          (
                'body'  => 'Body  Of Notification',
                'title' => 'Title Of Notification',
                'icon'  => 'myicon',/*Default Icon*/
                'sound' => 'mySound'/*Default sound*/
          );
    $fields = array
            (
                'to'        => $registrationIds,
                'notification'  => $msg
            );


    $headers = array
            (
                'Authorization: key=' . $apikey,
                'Content-Type: application/json'
            );
#Send Reponse To FireBase Server    
        $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_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );
#Echo Result Of FireBase Server
echo $result;