【问题标题】:how to send push notifications from php to IOS using FCM如何使用 FCM 将推送通知从 php 发送到 IOS
【发布时间】:2018-05-05 17:58:33
【问题描述】:

我正在尝试使用 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,"success":0,"failure":7,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"error":"NotRegistered "},{"error":"NotRegistered"},{"error":"NotRegistered"},{"error":"InvalidRegistration"},{"error":"InvalidRegistration"},{"error":"NotRegistered "}]}

【问题讨论】:

  • 这意味着您的代码运行良好,但是应该接收消息的手机在其应用程序中没有正确的 SENDER ID。请让他们更正。
  • 他们有 SENDER ID。但是我如何检查 SENDER ID 是否正确?感谢您的回复。
  • 您不能检查,如果 SENDER ID 与您或您的团队从 FCM 控制台提供的内容匹配,请让他们检查 logcat

标签: php web-services codeigniter


【解决方案1】:

试试那个代码:

<?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;
     }

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 2021-09-24
    相关资源
    最近更新 更多