推送通知 php 代码不适用于 Android
Posted
技术标签:
【中文标题】推送通知 php 代码不适用于 Android【英文标题】:Push Notification php code is not working for Android 【发布时间】:2017-05-01 10:29:27 【问题描述】:我正在使用单个 php 脚本来测试 android 的推送通知,但它无法正常工作,出现 curl 错误并且需要很长时间才能加载。我也看不到错误。请为我提供解决方案或任何带有示例代码的测试脚本,以便我可以检查我的 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;
【问题讨论】:
提供你试过的代码。 @Badshah 请检查我的代码 检查我的答案 【参考方案1】:我刚改了希望能解决你的问题
$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;
【讨论】:
【参考方案2】:如何在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;
【讨论】:
以上是关于推送通知 php 代码不适用于 Android的主要内容,如果未能解决你的问题,请参考以下文章
推送通知不适用于通过 Parse.com 发送的 Android 和 iOS 设备