黑莓推送通知消息显示在弹出屏幕中

时间:2013-07-08 07:11:40

标签: blackberry java-me

已将此链接用于推送通知http://rincethomas.blogspot.in/2012/07/push-notification-in-blackberry.html

代码运行正常,我在对话框中接收推送通知,但是我没有显示默认对话框,而是创建了我自己的弹出屏幕以显示推送消息

但我在PushMessageReader.java中收到错误,因为您可以在链接中检查它。这是我的PushMessageReader.java代码,用于弹出屏幕以显示推送消息

public final class PushMessageReader {

    // HTTP header property that carries unique push message ID
    private static final String MESSAGE_ID_HEADER = "Push-Message-ID";
    // content type constant for text messages
    private static final String MESSAGE_TYPE_TEXT = "text";
    // content type constant for image messages
    private static final String MESSAGE_TYPE_IMAGE = "image";

    private static final int MESSAGE_ID_HISTORY_LENGTH = 10;
    private static String[] messageIdHistory = new String[MESSAGE_ID_HISTORY_LENGTH];
    private static byte historyIndex;

    private static byte[] buffer = new byte[15 * 1024];
    private static byte[] imageBuffer = new byte[10 * 1024];

    static Popupscreen popup;
    private static String text;


    /**
     * Utility classes should have a private constructor.
     */
    private PushMessageReader() {
        popup = new Popupscreen();
    }


    /**
     * Reads the incoming push message from the given streams in the current thread and notifies controller to display the information.
     * 
     * @param pis
     *            the pis
     * @param conn
     *            the conn
     */
    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 );
                    text = new String( message.getData(), "UTF-8" );

                    System.out.println("PUSH MESSAGE================ "+text);
                     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(popup, 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);
        }
    }

    /**
     * Check whether the message with this ID has been already received.
     * 
     * @param id
     *            the id
     * @return true, if successful
     */
    private static boolean alreadyReceived(String id) {
        if (id == null) {
            return false;
        }

        if (Arrays.contains(messageIdHistory, id)) {
            return true;
        }

        // new ID, append to the history (oldest element will be eliminated)
        messageIdHistory[historyIndex++] = id;
        if (historyIndex >= MESSAGE_ID_HISTORY_LENGTH) {
            historyIndex = 0;
        }
        return false;
    }

    class Popupscreen extends PopupScreen{

        LabelField label = new LabelField("");

        ButtonField Okbutn;

        public Popupscreen(){
            super(new VerticalFieldManager(), Screen.DEFAULT_CLOSE); 

            Okbutn = new ButtonField("ok",ButtonField.FIELD_HCENTER);

            LabelField lbl = new LabelField("SUNRAYS", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER){
                 protected void paintBackground(net.rim.device.api.ui.Graphics g)
                    {
                        g.clear();
                        g.getColor();
                        //g.setColor(Color.WHITESMOKE);
                        //g.fillRect(0, 0, Display.getWidth(), Display.getHeight());
                        g.setColor(Color.WHITESMOKE);               
                    }
             };


             add(lbl);
             add(label); 
             add(Okbutn); 

             label.setText(text);

             Okbutn.setChangeListener(new FieldChangeListener() {

                public void fieldChanged(Field field, int context) {
                    // TODO Auto-generated method stub

                    close();
                }
            });
             //this.setBackground(BackgroundFactory.createSolidTransparentBackground(Color.WHITE, 80));

            // setBorder(BorderFactory.createSimpleBorder(new XYEdges(),Border.STYLE_TRANSPARENT)); 

        }

    }
}

0 个答案:

没有答案