dongsu3654 2013-03-06 18:35
浏览 32
已采纳

推送通知的Php脚本不返回任何内容

I have this script for sending a push notification on my android app :

<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyAATjQOVDdOUiHra0H-EfxpobQcqOsdwqa";

// Replace with real client registration IDs. Put the correct registration id you are getting from app. If you are using eclipse check your logcat you will get it there.
$registrationIDs = array("APA91bEmxWS9bsXLsixaYQ6zuByM0SzFWe5DEKVLO68923hW3Mo2qB6bIH2LarP5WgzKasMtFAdVyy8YQuwv0YbrRNwdGFORh1wQvQ9uKBkC3jH6uXBYAQOv5xfzrsjYjqcQK8syikrQ6dq6oRrp9XUnimdj_4oBbw" );

// Message to be sent
$message = "Push working!";


// Set POST variables.
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array( "message" => $message ) /*Make sure that message is the key you are using in GCMIntentService.java onMessage() ->  extras.getString("message");*/
);

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

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $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_POSTFIELDS, json_encode( $fields ) );


// Execute post
$result = curl_exec($ch);


// Close connection
curl_close($ch);

// Echo success or failure
echo $result;

?>

I never get any push notification on my phone. The thing is that actually i never get any output from the script like the echo $result; returns nothing.

Is this even possible?

Any ideas?

Solution


<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyAATjQOVDdOUiHra0H-EfxpobQcqOsdwqa";

// Replace with real client registration IDs. Put the correct registration id you are getting from app. If you are using eclipse check your logcat you will get it there.
$registrationIDs = array("APA91bEmxWS9bsXLsixaYQ6zuByM0SzFWe5DEKVLO68923hW3Mo2qB6bIH2LarP5WgzKasMtFAdVyy8YQuwv0YbrRNwdGFORh1wQvQ9uKBkC3jH6uXBYAQOv5xfzrsjYjqcQK8syikrQ6dq6oRrp9XUnimdj_4oBbw" );

// Message to be sent
$message = "Push working!";


// Set POST variables.
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array( "message" => $message ) /*Make sure that message is the key you are using in GCMIntentService.java onMessage() ->  extras.getString("message");*/
);

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

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $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, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );


// Execute post
$result = curl_exec($ch);


// Close connection
curl_close($ch);

// Echo success or failure
echo $result;

?>

Now it works perfectly.

  • 写回答

1条回答 默认 最新

  • dongyu3967 2013-03-07 10:21
    关注

    Found the error.

    Replace line curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); with line curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

    I corrected the code on the question so now it works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法