无法在服务器上发送推送消息(黑莓 - Android 运行时)

Posted

技术标签:

【中文标题】无法在服务器上发送推送消息(黑莓 - Android 运行时)【英文标题】:Unable to send push message on server (Blackberry - Android Runtime) 【发布时间】:2014-05-27 09:32:50 【问题描述】:

我已成功从我的设备获取注册 ID(pin),但我卡在服务器端开发上,希望有人能帮助我:Helpsmilie:

我遵循了这 3 个参考:

    http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/Problem-with-SEND-Push-Notificati...

    http://supportforums.blackberry.com/t5/android-Runtime-Development/Not-receiving-push-notification-o...

    server-side problemt: php Push message to Blackberry

但我没有得到预期的结果,而是我总是得到这样的错误代码 2000:

Our PUSH-ID: 1401179949.8267

An error has occured
Error CODE: 2000
Error DESC: 2000

string(217) " "

字符串(217)“”包含:

<pap>
    <badmessage-response bad-message-fragment="2000" desc="2000" code="2000"></badmessage-response>
</pap>

我使用的代码:

    $appid = '4746-6135ee38Dm11ro8094c98i7900xxxxxxxxx';
    $password = 'xxxxxxxx';
    $boundary = "mPsbVQo0a68eIL3OAxnm";

    $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+2 minutes'));
    //An array of address must be in PIN format or "push_all"
    $addresses = '';
    foreach ($registatoin_ids as $value) 
        $addresses .= '<address address-value=' . $value . '/>';
    

    // Open connection
    $ch = curl_init();
    $err = false;
    $messageid = microtime(true);

    $data =
        '--' . $boundary . "\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" .
        '--' . $boundary . "\r\n" .
        // 'Content-Encoding: binary' . "\r\n" .
        'Content-Type: text/plain' . "\r\n" .
        'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
        '
            "key":"value",
            "key2":"value2"
        '. "\r\n" .
        '--' . $boundary . '--' . "\n\r";

    $url = 'https://cp4746.pushapi.eval.blackberry.com/mss/PD_pushRequest';
    // Set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, "SAA push application");
    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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/related; boundary=" . $boundary . "; type=application/xml", "Accept: text/html", "Connection: keep-alive"));

    // 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) 
        $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";
     elseif ($err) 
        echo '<p>An XML parser error has occured</p>' . "\n";
        echo '<pre>' . xml_error_string($errorcode) ."</pre>\n";
        echo '<p>Response</p>' . "\n";
        echo '<pre>' . $xmldata . '</pre>' . "\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";
        echo '<pre>' . $xmldata . '</pre>' . "\n";
        var_dump($xmldata);
    
    var_dump($vals);
    return $xmldata;

我得到的注册 id 已经是正确的形式,没有前缀,我做错了什么?

【问题讨论】:

【参考方案1】:

我终于从这里找到了适合我的答案:

http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/BB-BIS-push-PHP-server-works-on-p...

感谢 marcboo 的回答。

然后我更改下面的代码

stripslashes($message)

到:

json_encode($message)

下面的完整代码,有必要的更改:

<?php
class BBPUSH 

    //put your code here
    // constructor
    function __construct() 

    

    public function send_notification($addresstosendto, $message) 
    // APP ID provided by RIM
        $appid = 'XXXX-XXXXXXXXXXXXXXXXX';
        // Password provided by RIM
        $password = 'XXXXXX';
        // Application device port
        $appport = XXXXXX;

        try 
            // Message to send :
            // $message = "testing testing";

            //Deliver before timestamp
            $deliverbefore = gmdate('Y-m-d\TH:i:s\Z', strtotime('+10 minutes'));

            // An array of address must be in PIN format or "push_all"
            // Format = WAPPUSH=PIN%3APORT/TYPE=USER@rim.com

            // $addresstosendto[] = 'push_all';

            $addresses = '';
            foreach ($addresstosendto as $value) 
                $addresses .= '<address address-value="' . $value . '" />';
            

            // 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: application/json' . "\r\n" .
                    //'Content-Encoding: binary'. "\r\n" .
            'Push-Message-ID: ' . $messageid . "\r\n\r\n" .
            json_encode($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";
            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();
    

?>

干杯

【讨论】:

以上是关于无法在服务器上发送推送消息(黑莓 - Android 运行时)的主要内容,如果未能解决你的问题,请参考以下文章

黑莓中的推送消息监听器

黑莓推送实现并等待确认

黑莓多线程问题

Android解析推送通知不会在状态栏上显示消息

无法使用服务器 API 发送推送通知

无法从 Parse 仪表板发送推送通知