我正在尝试在我的 BB OS 5 应用程序中发送推送通知。当我使用 RIM 提供的低级样本时,一切正常,我得到了推动。
但是我正在使用 php 构建一个 Apache 服务器,所以使用 Tomcat 的 java 示例不适合我。
我搜索了很多以找到一个简单的 php 脚本来发送推送通知。最后我找到了 3 个,我将在这里提供,但由于不同类型的错误,它们都不起作用。
也许有人可以帮我修复其中一个,或者为我提供另一种解决方案。
脚本 1
<?php
//PAP URL
$papURL = "https://pushapi.eval.blackberry.com";
//$papURL = "https://pushapi.eval.blackberry.com/mss/PD_pushReq?uest";
// APP ID provided by RIM
$appid = '3582-M4687r9k9k836r980kO2d95i32i67y10n34';
// Password provided by RIM
$password = '7cZUUVhG';
//Deliver before timestamp
$deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+5 minutes'));
$BBPin = "321EF989";
$Msg = "Hi, This is Blackberry Push Message Test from PHP Script";
//boundry
$boundry = "asdlfkjiudrgdgdgdrwghasf";
//An array of address must be in PIN format or "push_all" Commented for testing single mobile device
//$addresstosendto[] = '';
//$addresses = '';
//foreach ($addresstosendto as $value) {
//$addresses .= '<address address-value="' . $value . '"/>';
//}
//$addresses = '<address address-value="' . $BBPin . '"/>';
$addresses = '<address address-value="WAPPUSH='. $BBPin .'%3A100/TYPE=USER@rim.net"/>';
// create a new cURL resource
$err = false;
$ch = curl_init();
$messageid = microtime(true);
$data = '--' . $boundry . "\r\n" .
'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
'<?xml version="1.0"?>' .
'<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1 //EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd" [<?wap-pap-ver supported-versions="2.1,2.0,1.*"?>]>' .
'<pap>' .
'<push-message push-id="' . $messageid . '" deliver-before- timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
. $addresses .
'<quality-of-service delivery-method="unconfirmed"/>' .
'</push-message>' .
'</pap>' . "\r\n" .
'--' . $boundry . "\r\n" .
'Content-Type: text/plain' . "\r\n" .
'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
stripslashes($Msg) . "\r\n" .
'--' . $boundry . '--' . "\r\n";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $papURL);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "HashMe BB Push Server/1.0");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=" . $boundry . "; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// grab URL and pass it to the browser
$xmldata = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
//Start parsing response into XML data that we can read and output
$p = xml_parser_create();
xml_parse_into_struct($p, $xmldata, $vals);
$errorcode = xml_get_error_code($p);
if ($errorcode > 0) {
echo '<h4>An error has occured</h4>' . "\n";
echo '<strong>' . xml_error_string($errorcode) . '</strong>';
echo '<br \>';
$err = true;
}
xml_parser_free($p);
if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
} else {
echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
echo 'DeliveryTimeSchedule ' . $deliverbefore . "<br \>\n";
$dispdata = str_replace(">","><br />",str_replace("<","<",$data));
echo 'Data to be sent : <br/> ' . $dispdata . "<br \>\n";
}
?>
脚本 no1 的错误
An error has occured
Invalid document end
Our PUSH-ID: 1368441279.3452
Notice: Undefined offset: 1 in C:\xampp\htdocs\PushToBlackBerryServer\BBpushing.php on line 97
Error CODE:
Notice: Undefined offset: 1 in C:\xampp\htdocs\PushToBlackBerryServer\BBpushing.php on line 98
Error DESC:
DeliveryTimeSchedule 2013-05-13T10:39:39Z
Data to be sent :
--asdlfkjiudrgdgdgdrwghasf Content-Type: application/xml; charset=UTF-8 <?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1 //EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd" [<?wap-pap-ver supported-versions="2.1,2.0,1.*"?>
]>
<pap>
<push-message push-id="1368441279.3452" deliver-before- timestamp="2013-05-13T10:39:39Z" source-reference="3582-M4687r9k9k836r980kO2d95i32i67y10n34">
<address address-value="WAPPUSH=321EF989%3A100/TYPE=USER@rim.net"/>
<quality-of-service delivery-method="unconfirmed"/>
</push-message>
</pap>
--asdlfkjiudrgdgdgdrwghasf Content-Type: text/plain Push-Message-ID: 1368441279.3452 Hi, This is Blackberry Push Message Test from PHP Script --asdlfkjiudrgdgdgdrwghasf--
-------------------------------------------------- ------------------------------------------
脚本 2
<?php
ini_set('display_errors','1');
error_reporting(E_ALL);
// APP ID provided by RIM
$appid = '3582-M4687r9k9k836r980kO2d95i32i67y10n34';
// Password provided by RIM
$password = '7cZUUVhG';
//Deliver before timestamp
$deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+15 minutes'));
//An array of address must be in PIN format or "push_all"
$addresstosendto[] = '321EF989';
$addresses = '';
foreach ($addresstosendto as $value) {
$addresses .= '<address address-value="' . $value . '"/>';
}
// create a new cURL resource
$err = false;
$ch = curl_init();
$messageid = microtime(true);
$data = '--mPsbVQo0a68eIL3OAxnm'. "\r\n" .
'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n" .
'<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
<push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
. $addresses .
'<quality-of-service delivery-method="confirmed"/>
</push-message>
</pap>' . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm' . "\r\n" .
'Content-Type: text/plain' . "\r\n" .
'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
stripslashes('This is my message') . "\r\n" .
'--mPsbVQo0a68eIL3OAxnm--' . "\n\r";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Hallgren Networks BB Push Server/1.0");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=mPsbVQo0a68eIL3OAxnm; type=application/xml", "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2", "Connection: keep-alive"));
// grab URL and pass it to the browser
echo $xmldata = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
//Start parsing response into XML data that we can read and output
$p = xml_parser_create();
xml_parse_into_struct($p, $xmldata, $vals);
$errorcode = xml_get_error_code($p);
if ($errorcode > 0) {
echo xml_error_string($errorcode);
$err = true;
}
xml_parser_free($p);
echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
} else {
echo '<p>An error has occured</p>' . "\n";
echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
}
?>
脚本 No2 错误
Invalid document endOur PUSH-ID: 1368441579.0673
An error has occured
Notice: Undefined offset: 1 in C:\xampp\htdocs\PushToBlackBerryServer\newPush.php on line 78
Error CODE:
Notice: Undefined offset: 1 in C:\xampp\htdocs\PushToBlackBerryServer\newPush.php on line 79
Error DESC:
-------------------------------------------------- ------------------------------------------
脚本 3
<?php
// APP ID provided by RIM
$appid = "3582-M4687r9k9k836r980kO2d95i32i67y10n34";
// Password provided by RIM
$password = "7cZUCDgG";
// Application device port
$appport = "33387";
try {
// Message to send :
$message = "testing";
//Deliver before timestamp
$deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+1 minutes'));
// An array of address must be in PIN format or "push_all"
// Format = WAPPUSH=PIN%3APORT/TYPE=USER@rim.com
//$addresstosendto[] = 'WAPPUSH=21E1106x%3A30018/TYPE=USER@rim.com';
$addresses = '320EF999';
//foreach ($addresstosendto as $value) {
$addresses .= '<address address-value="21E1106x" />';
//}
// create a new cURL resource
$err = false;
$ch = curl_init();
$messageid = microtime(true);
$data = '--asdwewe'. "\r\n" .'Content-Type: application/xml; charset=UTF-8' . "\r\n\r\n".
'<?xml version="1.0"?>
<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 2.1//EN" "http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd">
<pap>
<push-message push-id="' . $messageid . '" deliver-before-timestamp="' . $deliverbefore . '" source-reference="' . $appid . '">'
. $addresses .
'<quality-of-service delivery-method="unconfirmed"/>
</push-message>
</pap>' . "\r\n" .
'--asdwewe' . "\r\n" .
'Content-Type: text/plain' . "\r\n" .
//'Content-Encoding: binary'. "\r\n" .
'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
stripslashes($message) . "\r\n" .
'--asdwewe--' . "\r\n";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_CAINFO, getcwd()."\cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "My BB Push Server\1.0");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $appid . ':' . $password);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$__extra_Headers = array(
"Content-Type: multipart/related; boundary=asdwewe; type=application/xml",
"Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2",
"Connection: keep-alive",
"X-Rim-Push-Dest-Port: ".$appport,
"X-RIM-PUSH-ID: ".$messageid,
"X-RIM-Push-Reliability-Mode: APPLICATION"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $__extra_Headers);
// grab URL and pass it to the browser
$xmldata = curl_exec($ch);
if($xmldata === false){
echo 'Error pada CURL : ' . curl_error($ch)."\n";
}else{
echo 'Operasi push berhasil'."\n";
}
// close cURL resource, and free up system resources
curl_close($ch);
//Start parsing response into XML data that we can read and output
$p = xml_parser_create();
xml_parse_into_struct($p, $xmldata, $vals);
$errorcode = xml_get_error_code($p);
if ($errorcode > 0) {
echo xml_error_string($errorcode)."\n";
$err = true;
}
xml_parser_free($p);
echo 'Our PUSH-ID: ' . $messageid . "<br \>\n";
$cek = var_dump($vals);
echo $cek;
if (!$err && $vals[1]['tag'] == 'PUSH-RESPONSE') {
echo 'PUSH-ID: ' . $vals[1]['attributes']['PUSH-ID'] . "<br \>\n";
echo 'REPLY-TIME: ' . $vals[1]['attributes']['REPLY-TIME'] . "<br \>\n";
echo 'Response CODE: ' . $vals[2]['attributes']['CODE'] . "<br \>\n";
echo 'Response DESC: ' . $vals[2]['attributes']['DESC'] . "<br \> \n";
} else {
echo '<p>An error has occured</p>' . "\n";
echo 'Error CODE: ' . $vals[1]['attributes']['CODE'] . "<br \>\n";
echo 'Error DESC: ' . $vals[1]['attributes']['DESC'] . "<br \>\n";
}
} catch (Exception $e) {
var_dump($e->getMessage());
}
exit();
// ---------------------------------------------------------------------------------------------------------------
?>
脚本 no3 的日志
Operasi push berhasil Our PUSH-ID: 1368441801.8001
array(5) { [0]=> array(3) { ["tag"]=> string(3) "PAP" ["type"]=> string(4) "open" ["level"]=> int(1) } [1]=> array(4) { ["tag"]=> string(13) "PUSH-RESPONSE" ["type"]=> string(4) "open" ["level"]=> int(2) ["attributes"]=> array(4) { ["PUSH-ID"]=> string(15) "1368441801.8001" ["SENDER-ADDRESS"]=> string(53) "http://pushapi.eval.blackberry.com/mss/PD_pushRequest" ["SENDER-NAME"]=> string(21) "RIM Push-Data Service" ["REPLY-TIME"]=> string(20) "2013-05-13T10:43:21Z" } } [2]=> array(4) { ["tag"]=> string(15) "RESPONSE-RESULT" ["type"]=> string(8) "complete" ["level"]=> int(3) ["attributes"]=> array(2) { ["CODE"]=> string(4) "1001" ["DESC"]=> string(45) "The request has been accepted for processing." } } [3]=> array(3) { ["tag"]=> string(13) "PUSH-RESPONSE" ["type"]=> string(5) "close" ["level"]=> int(2) } [4]=> array(3) { ["tag"]=> string(3) "PAP" ["type"]=> string(5) "close" ["level"]=> int(1) } } PUSH-ID: 1368441801.8001
REPLY-TIME: 2013-05-13T10:43:21Z
Response CODE: 1001
Response DESC: The request has been accepted for processing.
观察
首先,似乎唯一可行的脚本是 no3。但是我从来没有得到设备上的推动。也是唯一使用RIM 在我收到的凭据邮件中提供的apport参数的人。
*当我使用java服务器时,设备正常接收通知,这意味着BIS工作正常,因此问题出在代码而不是设备上。
任何帮助将不胜感激。