GCM with PHP (Google Cloud Messaging)

后端 未结 13 1457
轮回少年
轮回少年 2020-11-22 04:48

Update: GCM is deprecated, use FCM

How can I integrate the new Google Cloud Messaging in a PHP backend?

相关标签:
13条回答
  • 2020-11-22 05:29

    Also you can try this piece of code, source:

    <?php
        define("GOOGLE_API_KEY", "AIzaSyCJiVkatisdQ44rEM353PFGbia29mBVscA");
        define("GOOGLE_GCM_URL", "https://android.googleapis.com/gcm/send");
    
        function send_gcm_notify($reg_id, $message) {
            $fields = array(
                'registration_ids'  => array( $reg_id ),
                'data'              => array( "message" => $message ),
            );
    
            $headers = array(
                'Authorization: key=' . GOOGLE_API_KEY,
                'Content-Type: application/json'
            );
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, GOOGLE_GCM_URL);
            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);
            if ($result === FALSE) {
                die('Problem occurred: ' . curl_error($ch));
            }
    
            curl_close($ch);
            echo $result;
        }
    
        $reg_id = "APA91bHuSGES.....nn5pWrrSz0dV63pg";
        $msg = "Google Cloud Messaging working well";
    
        send_gcm_notify($reg_id, $msg);
    
    0 讨论(0)
提交回复
热议问题