如何使用FCM将推送通知从PHP发送到IOS

时间:2017-11-22 05:07:16

标签: php web-services codeigniter

我正在尝试使用FCM将通知从php推送到IOS。 这是我的代码。

   $serverKey = 'MYKEY';

    define('API_ACCESS_KEY', $serverKey);
    $registrationIds = ($vals['registration_ids']);
    $msg=$vals['data'];

    // prep the bundle

    $fields = array
    (
        'registration_ids'  => $registrationIds,
        'data'              => $msg
    );

    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

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

但它总是说:

  

{ “multicast_id”:9213457463518601541, “成功”:0, “失败”:7, “canonical_ids”:0 “结果”:[{ “错误”: “NotRegistered”},{ “错误”:“NotRegistered “},{” 错误 “:” NotRegistered “},{” 错误 “:” NotRegistered “},{” 错误 “:” InvalidRegistration “},{” 错误 “:” InvalidRegistration “},{” 错误 “:” NotRegistered “}]}

1 个答案:

答案 0 :(得分:0)

尝试该代码:

<?php



     public function notification($registrationIds,$body,$title)
     {

    $body;
    $title;
    $registrationIds; 


        //#API access key from Google API's Console
         if(!defined('AIdfSyCd8ha2wop84LKtpQvRmLpeTg2CEiY8Z')){
            define( 'AIdfSyCd8ha2wop84LKtpQvRmLpeTg2CEiY8Z', 'AAAA61DTuxA:APA93BGY4CXhKXJ2tPjoh3dnhbkmn1bGmfFj6Q7JJaBrdqKI106NFc3LZQTHy5i8WO5JoQBalrCIW_Cs2pGUz5mp1OL9SVcx0oH-hRGGpaeqV6hF-UDqJAlWAjGwyQM-oSl98ye_zMDi' );

          }
         $registrationIds = $registrationIds;
        //#prep the bundle
             $msg = array
                  (
                'body'  => $body,
                'title' => $title,
                'icon'  => 'myicon',/*Default Icon*/
                'sound' => 'mySound'/*Default sound*/
                  );
            $fields = array
                    (
                        'to'        => $registrationIds,
                        'notification'  => $msg
                    );


            $headers = array
                    (
                        'Authorization: key=AAAA61DTuxA:APA93BGY4CXhKXJ2tPjoh3dnhbkmn1bGmfFj6Q7JJaBrdqKI106NFc3LZQTHy5i8WO5JoQBalrCIW_Cs2pGUz5mp1OL9SVcx0oH-hRGGpaeqV6hF-UDqJAlWAjGwyQM-oSl98ye_zMDi',
                        '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
         $result;
     }

?>