回答
这是完全正常推送应用程序代码,它可能是帮助你实现推送通知。
public class push_Main {
/**
* Entry point for this application
* @param args Command line arguments (not used)
*/
private static final String REGISTER_SUCCESSFUL = "rc=200";
private static final String DEREGISTER_SUCCESSFUL = REGISTER_SUCCESSFUL;
private static final String USER_ALREADY_SUBSCRIBED = "rc=10003";
private static final String ALREADY_UNSUSCRIBED_BY_USER = "rc=10004";
private static final String ALREADY_UNSUSCRIBED_BY_PROVIDER = "rc=10005";
private static final String PUSH_PORT = ""; //push port
private static final String BPAS_URL = "http://pushapi.eval.blackberry.com";
private static final String APP_ID = ""; // add application id
// private static final String CONNECTION_SUFFIX = ";deviceside=false;ConnectionType=seekrit string";
private static String URL = "http://:100"; // PORT 100 add your posh port.
private static final int CHUNK_SIZE = 256;
public static ListeningThread _listeningThread;
public static StreamConnectionNotifier _notify;
private static final long ID = 0x954a603c0dee81e0L;
public push_Main() {
// TODO Auto-generated constructor stub
NotificationsManager.registerSource(ID, theSource, NotificationsConstants.IMPORTANT);
if(_listeningThread==null)
{
System.out.println("msg on listening thread 1");
_listeningThread = new ListeningThread();
System.out.println("msg on listening thread 2");
_listeningThread.start();
System.out.println("msg on listhning thread 3 ");
}
}
public static class ListeningThread extends Thread
{
private boolean _stop = false;
/**
* Stops the thread from listening.
*/
private synchronized void stop()
{
_stop = true;
try
{
// Close the connection so the thread will return.
_notify.close();
}
catch (Exception e)
{
}
}
/**
* Listen for data from the HTTP url. After the data has been read,
* render the data onto the screen.
* @see java.lang.Runnable#run()
*/
public void run()
{
StreamConnection stream = null;
InputStream input = null;
MDSPushInputStream pushInputStream=null;
while (!_stop)
{
try
{
// Synchronize here so that we don't end up creating a connection that is never closed.
synchronized(this)
{
// Open the connection once (or re-open after an IOException), so we don't end up
// in a race condition, where a push is lost if it comes in before the connection
// is open again. We open the url with a parameter that indicates that we should
// always use MDS when attempting to connect.
System.out.println("\n\n msg connection 1");
_notify = (StreamConnectionNotifier)Connector.open(URL);
System.out.println("\n\n msg connection 2");
}
while (!_stop)
{
// NOTE: the following will block until data is received.
System.out.println("\n\n msg notify 1");
stream = _notify.acceptAndOpen();
System.out.println("\n\n msg 1 ");
try
{
System.out.println("\n\n msg 2");
input = stream.openInputStream();
System.out.println("\n\n msg 3 ");
pushInputStream= new MDSPushInputStream((HttpServerConnection)stream, input);
System.out.println("\n\n msg 4");
// Extract the data from the input stream.
DataBuffer db = new DataBuffer();
byte[] data = new byte[CHUNK_SIZE];
int chunk = 0;
while (-1 != (chunk = input.read(data)))
{
db.write(data, 0, chunk);
}
updateMessage(data);
// This method is called to accept the push.
pushInputStream.accept();
data = db.getArray();
}
catch (IOException e1)
{
// A problem occurred with the input stream , however, the original
// StreamConnectionNotifier is still valid.
// errorDialog(e1.toString());
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (IOException e2)
{
}
}
if (stream != null)
{
try
{
stream.close();
}
catch (IOException e2)
{
}
}
}
}
}
catch (IOException ioe)
{
// Likely the stream was closed. Catches the exception thrown by
// _notify.acceptAndOpen() when this program exits.
errorDialog(ioe.toString());
}
finally
{
/*
if (_notify != null)
{
try
{
_notify.close();
_notify = null;
}
catch (IOException e)
{
}
}
*/
}
}
}
}
private static void updateMessage(final byte[] data)
{
System.out.println("\n\n msg 6");
Application.getApplication().invokeLater(new Runnable()
{
public void run()
{
// Query the user to load the received message.
// Dialog.alert(new String(data));
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run()
{
NotificationsManager.triggerImmediateEvent(ID, 0, null, null);
Dialog d = new Dialog(Dialog.D_OK, new String(data) ,0, null, Screen.DEFAULT_CLOSE);
// _dialogShowing = true;
UiApplication.getUiApplication().pushGlobalScreen(d, 10, UiApplication.GLOBAL_MODAL);
// Dialog is closed at this point, so we cancel the event.
}
});
}
});
}
public static void registerBpas() {
/**
* As the connection suffix is fixed I just use a Thread to call the connection code
*
**/
new Thread() {
public void run() {
try {
final String registerUrl = formRegisterRequest(BPAS_URL, APP_ID, null) + Conn.getConnectionParameters();
System.out.println("\n\n\n msg registerBPAS URL is: "+ registerUrl);
HttpConnection httpConnection = (HttpConnection) Connector.open(registerUrl);
InputStream is = httpConnection.openInputStream();
String response = new String(IOUtilities.streamToBytes(is));
System.out.println("\n\n\n\n\n\n msg RESPOSE CODE : " + response);
close(httpConnection, is, null);
String nextUrl = formRegisterRequest(BPAS_URL, APP_ID, response) + Conn.getConnectionParameters();
System.out.println("\n\n\n\n\n\n msg nextUrl : " + nextUrl);
HttpConnection nextHttpConnection = (HttpConnection) Connector.open(nextUrl);
InputStream nextInputStream = nextHttpConnection.openInputStream();
response = new String(IOUtilities.streamToBytes(nextInputStream));
System.out.println("\n\n\n\n\n\n msg RESPOSE CODE 1: " + response);
close(nextHttpConnection, is, null);
if (REGISTER_SUCCESSFUL.equals(response) || USER_ALREADY_SUBSCRIBED.equals(response)) {
System.out.println("msg Registered successfully for BIS push");
} else {
System.out.println("msg BPAS rejected registration");
}
} catch (final IOException e) {
System.out.println("msg IOException on register() " + e + " " + e.getMessage());
}
}
}.start();
}
public static void close(Connection conn, InputStream is, OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
if (conn != null) {
try {
conn.close();
} catch (IOException e) {
}
}
}
public static void errorDialog(final String message)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(message);
}
});
}
private static String formRegisterRequest(String bpasUrl, String appId, String token) {
StringBuffer sb = new StringBuffer(bpasUrl);
sb.append("/mss/PD_subReg?");
sb.append("serviceid=").append(appId);
sb.append("&osversion=").append(DeviceInfo.getSoftwareVersion());
sb.append("&model=").append(DeviceInfo.getDeviceName());
if (token != null && token.length() > 0) {
sb.append("&").append(token);
}
return sb.toString();
}
}
谢谢你们!顺便说一句,我可以实现** PushApplication **接口在我的应用程序中接收推送?我正在开发我的5.0和6.0版手机的应用程序。 – user1330929 2012-04-27 06:12:45
是的,你在应用程序中接受按摩。它也在5.0和6.0上工作。 – Rajkiran 2012-04-27 06:40:05
Source @ NotificationsManager.registerSource(ID,theSource,NotificationsConstants.IMPORTANT)的值是多少? – 2012-05-17 07:46:22
- 1. 黑莓推送通知客户端
- 2. 黑莓推特客户端
- 3. 黑莓推送客户端应用程序的黑莓注册失败
- 4. 黑莓VOIP客户端
- 5. 推动样品服务客户端应用程序代码:黑莓
- 6. 黑莓报警代码示例
- 7. 黑莓为黑莓邮件客户端插件开发..?
- 8. 显示黑莓推送通知
- 9. Jira有没有RIM黑莓客户端?
- 10. 推送通知:黑莓
- 11. 黑莓推送通知
- 12. 如何与.NET中的黑莓推送服务集成
- 13. 在RESTAPI与我们的服务器端代码集成Ember.js客户端代码
- 14. 黑莓facebook集成错误
- 15. 贝宝集成在黑莓?
- 16. 麻烦黑莓JSON解析代码示例工作
- 17. 从android wsdl客户端代码生成
- 18. netbeans wsdl客户端代码生成
- 19. 解析迁移 - 客户端到客户端推送通知
- 20. 黑莓推送屏幕没有弹出
- 21. 黑莓推送服务错误
- 22. 黑莓推送通知实施
- 23. 印度的黑莓推送支持
- 24. 黑莓推送通知(本地通知)
- 25. 黑莓推送通知注册时间
- 26. 黑莓推送通知教程
- 27. 服务器端问题:PHP推送消息给黑莓
- 28. 将Angular2客户端与.NET C#服务器代码集成
- 29. C#REST客户端示例?
- 30. FreeOPCUA python客户端示例
参考http://stackoverflow.com/questions/7481122/push-notification-in-blackberry和http://stackoverflow.com/questions/8396978/push-notification-with-黑莓客户端服务器 – Signare 2012-04-26 12:43:47