Blackberry主推推送技术,但何为推送技术,客户端对推送技术是怎样支持的?
推送的过程有应用服务发起,由BES服务器执行,数据通过BB专线,无线网最终达到黑莓手机终端,数据推送的目标终端以email地址或PIN决定。BES服务器与应用服务器或email服务器之间有轮询交互,BES服务器轮循应用服务器查看数据更新,如有更新,应用服务器会发起推送请求。
对于客户端应用来说,不存在轮循的操作。客户端应用只需侦听指定的端口,当数据到达时,客户端应用会被通知来处理到达的数据。以下是客户端实现侦听的范例代码:
class ListenerThread extends Thread {
LISTEN_URL = "http://:911";
public void run() {
StreamConnectionNotifier notify = null;
StreamConnection stream = null;
InputStream input = null;
try{//wait for the device to load
sleep(1000);
}
catch(Exception e){}
for(;;) {//in case an exception is thrown, re try to get the data
try {
notify = (StreamConnectionNotifier)Connector.open(LISTEN_URL);
for(;;){
//NOTE: the following will block until data is received
stream = notify.acceptAndOpen();
input = stream.openInputStream();
//Extract the data from the input stream
StringBuffer sb = new StringBuffer();
int datum = -1;
while ( -1 != (datum = input.read()) )
{
sb.append((char)datum);
}
stream.close();
stream = null;
String contactData = sb.toString();
DataStore dataStore = new DataStore();
dataStore.saveData(contactData);
System.err.println("Push message received...("+contactData.length()+" bytes)/n");
System.err.println(contactData);
//triggering a notification event since data has been received
NotificationsManager.triggerImmediateEvent(ID_1,0,this,null);//notifing the device of new content being received
}
}
catch (IOException e){
//likely the stream was closed
System.err.println(e.toString());
}
finally {
try {
if (stream != null){
stream.close();
}
}
catch (Exception ex){}
try{
if (notify != null){
notify.close();
}
}
catch (Exception ex){}
}//end finally
}//end for
}//end run
}//end listenerthread
}//end pushdatalistener