2013-04-29 39 views
0

我在BlackBerry中通過推送消息傳遞而苦苦掙扎。我能夠成功註冊推送消息,返回代碼爲rc=200。但是,當我運行服務器端代碼時,無論是PHP還是.Net,我都會得到一個成功的返回代碼,但設備永遠不會收到消息。在設備中未收到推送消息

我的PHP代碼如下:

<?php 
ini_set('display_errors','1'); 
error_reporting(E_ALL); 

    // APP ID provided by RIM 
$appid = '3385-xxxxxxxxxxx'; 
// Password provided by RIM 
$password = 'xxxxxx'; 

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

//An array of address must be in PIN format or "push_all" 
$addresstosendto[] = 'xxxx'; 

$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('r') . "\r\n" . 
'--mPsbVQo0a68eIL3OAxnm--' . "\n\r"; 

// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, "https://pushapi.eval.blackberry.com/mss/PD_pushRequest");//"https://cp3385.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"; 
} 


?> 

我的.NET代碼如下:

public bool pushToWidget(string pushedMessage, string pushPin) 
     { 
      String BESAddress = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"; 
      String BESWebserverListenPort = "33074"; 
      String widgetNotificationUrl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest"; 
      String pushUserName = "[email protected]"; 
      String pushPassword = "[email protected]"; 
      String pushPort = "33074 "; 
      string Boundary = "Boundary "; 
      String ID = "3385-xxxxxxxxxxxxxxxxxxxxx"; 
      //String pushPin = "xxxxxxxx"; 
      string DeliverBefore = DateTime.UtcNow.AddMinutes(60).ToString("s", 

System.Globalization.CultureInfo.InvariantCulture) + "Z"; 
      Response.Write(DeliverBefore); 
      bool success = true; 
      StringBuilder Data = new StringBuilder(); 
      Data.AppendLine("--" + Boundary); 
      Data.AppendLine("Content-Type: application/xml; charset=utf-8"); 
      Data.AppendLine(""); 
      Data.AppendLine("<?xml version=\"1.0\"?>"); 
      Data.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\">"); 
      Data.AppendLine("<pap>"); 
      Data.AppendLine("<push-message push-id=" + (char)34 + ID + (char)34 + " deliver-before-timestamp=" + 

(char)34 + DeliverBefore + (char)34 + " source-reference=" + (char)34 + pushUserName + (char)34 + ">"); 
      Data.AppendLine("<address address-value=\"" + pushPin + "\"/>"); 
      Data.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>"); 
      Data.AppendLine("</push-message>"); 
      Data.AppendLine("</pap>"); 
      Data.AppendLine("--" + Boundary); 
      Data.AppendLine("Content-Type: text/plain"); 
      Data.AppendLine("Push-Message-ID: " + ID); 
      Data.AppendLine(""); 
      Data.AppendLine(pushedMessage); 
      Data.AppendLine("--" + Boundary + "--"); 
      Data.AppendLine(""); 
      byte[] bytes = Encoding.ASCII.GetBytes(Data.ToString()); 

      Stream requestStream = null; 
      HttpWebResponse HttpWRes = null; 
      HttpWebRequest HttpWReq = null; 

      try 
      { 
       //http://<BESName>:<BESPort>/push? 

//String DESTINATTION="<299A7C32/EMAIL>&PORT=<PushPort>&REQUESTURI=/" 
       // Build the URL to define our connection to the BES. 
       string httpURL = BESAddress + ":" + BESWebserverListenPort 
        + "/push?DESTINATION=" + pushPin + "&PORT=" + pushPort 
        + "&REQUESTURI=/"; 

       //make the connection 
       HttpWReq = (HttpWebRequest)WebRequest.Create(httpURL); 
       HttpWReq.Method = ("POST"); 
       //add the headers nessecary for the push 
       HttpWReq.ContentType = "text/plain"; 
       HttpWReq.ContentLength = bytes.Length; 
       // ******* Test this ******* 
       HttpWReq.Headers.Add("X-Rim-Push-Id", pushPin + "~" + DateTime.Now); //"~" +pushedMessage + 
       HttpWReq.Headers.Add("X-Rim-Push-Reliability", "application-preferred"); 
       HttpWReq.Headers.Add("X-Rim-Push-NotifyURL", (widgetNotificationUrl + pushPin + "~" + pushedMessage 

+ "~" + DateTime.Now).Replace(" ", "")); 

       // ************************* 

       HttpWReq.Credentials = new NetworkCredential(pushUserName, pushPassword); 
       Console.WriteLine(pushedMessage); 
       requestStream = HttpWReq.GetRequestStream(); 
       //Write the data from the source 
       requestStream.Write(bytes, 0, bytes.Length); 
       requestStream.Close(); 
       //get the response 
       HttpWRes = (HttpWebResponse)HttpWReq.GetResponse(); 
       String response = HttpWRes.ToString(); 

       //if the MDS received the push parameters correctly it will either respond with okay or accepted 
       if (HttpWRes.StatusCode == HttpStatusCode.OK || HttpWRes.StatusCode == HttpStatusCode.Accepted) 
       { 
        success = true; 
       } 
       else 
       { 
        success = false; 
       } 
       //Close the streams 

       HttpWRes.Close(); 
       requestStream.Close(); 
      } 
      catch (System.Exception e) 
      { 
       success = false; 
      } 

      return success; 
     } 

我正在我的應用程序下面的代碼偵聽傳入消息:

public static void process(PushInputStream pis, Connection conn) { 
     System.out.println("Reading incoming push message ..."); 

     try { 

      HttpServerConnection httpConn; 
      if (conn instanceof HttpServerConnection) { 
       httpConn = (HttpServerConnection) conn; 
      } else { 
       throw new IllegalArgumentException("Can not process non-http pushes, expected HttpServerConnection but have " 
         + conn.getClass().getName()); 
      } 

      String msgId = httpConn.getHeaderField(MESSAGE_ID_HEADER); 
      String msgType = httpConn.getType(); 
      String encoding = httpConn.getEncoding(); 

      System.out.println("Message props: ID=" + msgId + ", Type=" + msgType + ", Encoding=" + encoding); 


      boolean accept = true; 
      if (!alreadyReceived(msgId)) { 
       byte[] binaryData; 

       if (msgId == null) { 
        msgId = String.valueOf(System.currentTimeMillis()); 
       } 

       if (msgType == null) { 
        System.out.println("Message content type is NULL"); 
        accept = false; 
       } else if (msgType.indexOf(MESSAGE_TYPE_TEXT) >= 0) { 
        // a string 
        int size = pis.read(buffer); 
        binaryData = new byte[size]; 
        System.arraycopy(buffer, 0, binaryData, 0, size); 

        PushMessage message = new PushMessage(msgId, System.currentTimeMillis(), binaryData, true, true); 
        String text = new String(message.getData(), "UTF-8"); 


        try{ 
          final Dialog screen = new Dialog(Dialog.D_OK_CANCEL, " "+text, 
            Dialog.OK, 
            //mImageGreen.getBitmap(), 
            null, Manager.VERTICAL_SCROLL); 
          final UiEngine ui = Ui.getUiEngine(); 
          Application.getApplication().invokeAndWait(new Runnable() { 
           public void run() { 
            NotificationsManager.triggerImmediateEvent(0x749cb23a76c66e2dL, 0, null, null); 
            ui.pushGlobalScreen(screen, 0, UiEngine.GLOBAL_QUEUE); 

           } 
          }); 
          screen.setDialogClosedListener(new MyDialogClosedListener()); 
          } 
          catch (Exception e) { 
           // TODO: handle exception 
          } 


        // TODO report message 
       } else { 
        System.out.println("Unknown message type " + msgType); 
        accept = false; 
       } 
      } else { 
       System.out.println("Received duplicate message with ID " + msgId); 
      } 
      pis.accept(); 
     } catch (Exception e) { 
      System.out.println("Failed to process push message: " + e); 
     } 
    } 

以上是運行讀取傳入消息的應用程序代碼的一部分。由於我在推送信息方面經歷了很長時間,我已經詳細地發佈了服務器端代碼,並且希望有人能夠指引我朝着正確的方向發展。考慮到我從服務器端代碼獲得「成功」返回的代碼,問題是否可以在前端收聽和處理消息?我可以根據要求分享客戶端應用程序。請幫忙。

回答

0

您必須打開套接字連接才能收聽傳入的推送。這是可用於打開套接字連接的可運行類。一旦收到傳入的推送消息,就可以調用您的過程功能。

可以調用使用 -

MessageReadingThread messageReadingThread = new MessageReadingThread(); 
Thread oThread = new Thread(messageReadingThread); 
oThread.start(); 


/** 
* Thread that processes incoming connections through 
* {@link PushMessageReader}. 
*/ 
private class MessageReadingThread implements Runnable 
{ 
private final BIS_CONNECTION_SUFFIX = ";ConnectionType=mds-public"; 


private final String CONNECTION_SUFFIX = ";deviceside=false"; 

    /** Flag indicating Listening thread is running or not */ 
    private boolean bRunning; 

    /** ServerSocketConnection instance to Listen for incoming push message */ 
    private ServerSocketConnection oSocket; 


    private HttpServerConnection oHttpServerConnection; 


    private InputStream oInputStream; 


    private MDSPushInputStream oMDSPushInputStream; // PushInputStream 

    /** 
    * {@inheritDoc} 
    * 
    * @see java.lang.Thread#run() 
    */ 
    public void run() 
    { 
     String strUrl = "http://:" + PUSH_PORT + CONNECTION_SUFFIX + BIS_CONNECTION_SUFFIX; 
     try 
     { 
      oSocket = (ServerSocketConnection) Connector.open(strUrl); 
      m_bIsPortOpen = true; 
      bRunning = true; 
     } 
     catch (IOException ex) 
     { 
      // can't open the port, probably taken by another application 
      fnStopRunning(); 
     } 
     while (bRunning) 
     { 
      try 
      { 
       Object object = oSocket.acceptAndOpen(); 
       oHttpServerConnection = (HttpServerConnection) object; 
       oInputStream = oHttpServerConnection.openInputStream(); 
       oMDSPushInputStream = new MDSPushInputStream(
         oHttpServerConnection, oInputStream); 
       process(oMDSPushInputStream, 
         oHttpServerConnection, app); 
      } 
      catch (Exception e) 
      { 
       // Failed to read push message 
       bRunning = false; 
      } 
      finally 
      { 
       close(null, oInputStream, null); 
       close(oHttpServerConnection, oMDSPushInputStream, null); 
      } 
     } 
    } 

    /** 
    * Stop running. 
    */ 
    private void fnStopRunning() 
    { 
     bRunning = false; 
     m_bIsPortOpen = false; 
     close(oSocket, null, null); 
    } 

      /** 
* Safely closes connection and streams. 
*/ 
private void close(Connection oConnection, InputStream oInputStream, 
     OutputStream oOutputStream) 
{ 
    if (oOutputStream != null) 
    { 
     try 
     { 
      oOutputStream.close(); 
     } 
     catch (IOException e) 
     { 
     } 
    } 
    if (oInputStream != null) 
    { 
     try 
     { 
      oInputStream.close(); 
     } 
     catch (IOException e) 
     { 
     } 
    } 
    if (oConnection != null) 
    { 
     try 
     { 
      oConnection.close(); 
     } 
     catch (IOException e) 
     { 
     } 
    } 
} 
} 
相關問題