推送通知PHP代码不适用于Android

时间:2017-05-01 10:29:27

标签: php android push-notification

我正在使用单个php脚本来测试Android的推送通知,但它无法正常工作,导致卷曲错误并花费大量时间加载。我也无法看到错误。请提供解决方案或任何带有示例代码的测试脚本,以便我可以检查我的API密钥和GCM是否正确。

enter image description here

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyDG3fYAj1uW7VB-wejaMJyJXiO5JagAsYI' );
// $registrationIds = array( "f-fAcroIrA8:APA91bFKyAmILhqC7itmq9W-ZsPJ09O908E8APZUavcLNpdGjxyihVPtFTR9wBnrMzDTYxQs3utuaFR_lPlBSkwpsSzV2W38NtZydf-S-aqOlq1cTj74eV3PcsDEM_8EVaKNqytrurvy");
$registrationIds = 'APA91bHdOmMHiRo5jJRM1jvxmGqhComcpVFDqBcPfLVvaieHeFI9WVrwoDeVVD1nPZ82rV2DxcyVv-oMMl5CJPhVXnLrzKiacR99eQ_irrYogy7typHQDb5sg4NB8zn6rFpiBuikNuwDQzr-2abV6Gl_VWDZlJOf4w';
// prep the bundle
$msg = array
(
    'message'   => 'hiiii',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);
$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $msg
);

// echo "<pre>";
// print_r($fields);exit;

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
 if($result === false)
 {
    echo curl_error($ch);
    die('Curl failed: ' . curl_error($ch));
 }


curl_close( $ch );
echo $result;

2 个答案:

答案 0 :(得分:0)

我刚刚改变了,我希望它能解决你的问题

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

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

立即尝试以下代码

<?php 
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyDG3fYAj1uW7VB-wejaMJyJXiO5JagAsYI' );
// $registrationIds = array( "f-fAcroIrA8:APA91bFKyAmILhqC7itmq9W-ZsPJ09O908E8APZUavcLNpdGjxyihVPtFTR9wBnrMzDTYxQs3utuaFR_lPlBSkwpsSzV2W38NtZydf-S-aqOlq1cTj74eV3PcsDEM_8EVaKNqytrurvy");
$registrationIds = 'APA91bHdOmMHiRo5jJRM1jvxmGqhComcpVFDqBcPfLVvaieHeFI9WVrwoDeVVD1nPZ82rV2DxcyVv-oMMl5CJPhVXnLrzKiacR99eQ_irrYogy7typHQDb5sg4NB8zn6rFpiBuikNuwDQzr-2abV6Gl_VWDZlJOf4w';
// prep the bundle
$msg = array
(
    'message'   => 'hiiii',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);


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

// echo "<pre>";
// print_r($fields);exit;

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
 if($result === false)
 {
    echo curl_error($ch);
    die('Curl failed: ' . curl_error($ch));
 }


curl_close( $ch );
echo $result;

答案 1 :(得分:0)

如何在php中发送多个用户的推送通知?

我使用这个脚本..

define( 'API_ACCESS_KEY', 'AIzaSyBpAw3JWL-IbwaPnVBLhXJrjMSrKpIJdjI' );
     $registrationIds = array($push);

    // prep the bundle
    $msg = array
    (
     'message'  => $_POST['msg'],
     'title'  => 'This is a title. title',
     'subtitle' => 'This is a subtitle. subtitle',
     'tickerText' => 'Ticker text here...Ticker text here...Ticker text here',
     'vibrate' => 1,
     'sound'  => 1,
     'largeIcon' => 'large_icon',
     'smallIcon' => 'small_icon'
    );
    $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_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/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 );
     if($result === false)
     {
      echo curl_error($ch);
      die('Curl failed: ' . curl_error($ch));
     }


    curl_close( $ch );
    echo $result;





 }