Wednesday, December 7, 2011

BlackBerry Push Notification Server Code in J2SE

Hello,
       
           In BlackBerry Push Notification three type entity use.

1.Server
2.Mobile
3.BIS

Here i clarify the Server Code.


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;
import javax.net.ssl.HttpsURLConnection;


public class Pusher {
 private static String _appid = "xxx-xxxxxxxxxxxxxxxx";
 private static String _spacers = "mPsbVQo0a68eIL3OAxnm";
 private static String _auth = "Basic <base64token>"; // Replace <base64token> with your userid:password encode, http://bit.ly/diYQUr enter APPID:PASS and encode
 private static String _pushurl = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
 private static String _uagent = "Hallgren Networks BB Push Server/1.0";
 private static boolean _output = false;

 public static boolean pushMessage(Vector<String> pins, String msg) {
  if (pins.isEmpty())
   return false;
  if (msg.equals(""))
   return false;

  String pushid = "" + System.currentTimeMillis();
  String delivebefore = getDeliveryTime();
  StringBuffer dataToSend = new StringBuffer();
  dataToSend.append("--" + _spacers + "\r\n");
  dataToSend.append("Content-Type: application/xml; charset=UTF-8\r\n\r\n");
  dataToSend.append("<?xml version=\"1.0\"?>\r\n");
  dataToSend.append("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\">\r\n");
  dataToSend.append("<pap>\r\n");
  dataToSend.append("<push-message push-id=\"" + pushid + "\" ");
  dataToSend.append("deliver-before-timestamp=\"" + delivebefore + "\" ");
  dataToSend.append("source-reference=\"" + _appid + "\">");
  for (int i = 0; i < pins.size(); ++i)
   dataToSend.append("<address address-value=\"" + pins.elementAt(i) + "\"/>");
  dataToSend.append("<quality-of-service delivery-method=\"unconfirmed\"/>\r\n");
  dataToSend.append("</push-message>\r\n");
  dataToSend.append("</pap>\r\n");
  dataToSend.append("--" + _spacers + "\r\n");
  dataToSend.append("Content-Type: text/plain\r\n");
  dataToSend.append("Push-Message-ID: " + pushid + "\r\n");
  dataToSend.append("\r\n");
  dataToSend.append(msg + "\r\n");
  dataToSend.append("--" + _spacers + "--");
  printer("-------------------------------------------------------------------");
  printer(dataToSend.toString());
  printer("-------------------------------------------------------------------");

  URL url;
  HttpsURLConnection connection = null;
  try {
   url = new URL(_pushurl);
   connection = (HttpsURLConnection) url.openConnection();
   connection.setRequestMethod("POST");
   connection.setRequestProperty("Content-Type", "multipart/related; boundary=" + _spacers + "; type=application/xml");
   connection.setRequestProperty("User-Agent", _uagent);
   connection.setRequestProperty("Authorization", _auth);
   connection.setDoInput(true);
   connection.setDoOutput(true);

   DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
   wr.writeBytes(dataToSend.toString());
   wr.flush();
   wr.close();
   BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
   StringBuilder sb = new StringBuilder();
   String line = null;
   while ((line = rd.readLine()) != null) {
    sb.append(line + '\n');
   }
   printer(connection.getResponseCode() + " | " + connection.getResponseMessage());
   printer(sb.toString());
   if (sb.toString().contains("code=\"1001\""))
    return true;
   return false;
  } catch (Exception e) {
   printer(e.getMessage());
   return false;
  } finally {
   if (connection != null) {
    connection.disconnect();
   }
  }

 }

 public static String getDeliveryTime() {
  Date now = new Date(System.currentTimeMillis() + 300000);
  SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  d.setTimeZone(TimeZone.getTimeZone("GMT"));
  return d.format(now);
 }

 private static void printer(Object response) {
  if (_output)
   System.out.println(response);
 }
 /*
 public static void main(String[] args) {
  Vector<String> v = new Vector<String>();
  v.add("12345678");
  if (pushMessage(v, "message"))
   System.out.println("Sent Ok");
  else {
   System.out.println("Returned False");
  }
  /////////////////////////// Or send to all 
  Vector<String> v1 = new Vector<String>();
  v1.add("push_all");
  if (pushMessage(v, "message"))
   System.out.println("Sent Ok");
  else {
   System.out.println("Returned False");
  }
  
 }
 */
}

1 comment:

  1. Thanks for provide the information.
    For more information please visit here blackberry push notifications.

    ReplyDelete