GCM with PHP (Google Cloud Messaging)

后端 未结 13 1670
轮回少年
轮回少年 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:26

    I know this is a late Answer, but it may be useful for those who wants to develop similar apps with current FCM format (GCM has been deprecated).
    The following PHP code has been used to send topic-wise podcast. All the apps registered with the mentioned channel/topis would receive this Push Notification.

    ';
          $curl_post_body = array('to' => $channel,
            'content_available' => true,
            'notification' => array('click_action' => 'action_open',
                                'body'=> $contentTitle,
                                'title'=>'Title '.$contentCurrentCat. ' Updates' ,
                                'message'=>'44'),
            'data'=> array('click_action' => 'action_open',
                                'body'=>'test',
                                'title'=>'test',
                                'message'=>$catTitleId));
    
            $headers = array(
            'Content-Type:application/json',
            'Authorization:key='.$fcm_token);
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $service_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, json_encode($curl_post_body));
    
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('FCM Send Error: ' . curl_error($ch));
            echo 'failure';
        }else{
    
        echo 'success' .$result;
        }
        curl_close($ch);
        return $result;
    
    }
    catch(Exception $e){
    
        echo 'Message: ' .$e->getMessage();
    }
    ?>
    

提交回复
热议问题