1

これが私のフルプッシュ通知リスナーです。

public class MyApp extends UiApplication {
    public static void main(String[] args) {
        PushAgent pa = new PushAgent();
        pa.enterEventDispatcher();
    }
}

このクラスは正しく拡張されていますか?

public class PushAgent extends Application {
private static final String PUSH_PORT = "32023";
private static final String BPAS_URL = "http://pushapi.eval.blackberry.com";
private static final String APP_ID = "2727-c55087eR3001rr475448i013212a56shss2";
private static final String CONNECTION_SUFFIX = ";deviceside=false;ConnectionType=mds-public";

public static final long ID = 0x749cb23a75c60e2dL;

private MessageReadingThread messageReadingThread;

public PushAgent() {
    if (!CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) {
        return;
    }
    if (DeviceInfo.isSimulator()) {
        return;
    }
    messageReadingThread = new MessageReadingThread();
    messageReadingThread.start();
    registerBpas();
}

private static class MessageReadingThread extends Thread {

    private boolean running;
    private ServerSocketConnection socket;
    private HttpServerConnection conn;
    private InputStream inputStream;
    private PushInputStream pushInputStream;

    public MessageReadingThread() {
        this.running = true;
    }

    public void run() {
        String url = "http://:" + PUSH_PORT + CONNECTION_SUFFIX;

        try {
            socket = (ServerSocketConnection) Connector.open(url);
        } catch (IOException ex) {
        }

        while (running) {
            try {
                Object o = socket.acceptAndOpen();
                conn = (HttpServerConnection) o;
                inputStream = conn.openInputStream();
                pushInputStream = new MDSPushInputStream(conn, inputStream);
                PushMessageReader.process(pushInputStream, conn);
            } catch (Exception e) {
                if (running) {
                    running = false;
                }
            } finally {
                close(conn, pushInputStream, null);
            }
        }
    }
}

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) {
        }
    }
}

private 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();
}

private void registerBpas() {
    final String registerUrl = formRegisterRequest(BPAS_URL, APP_ID, null)
            + CONNECTION_SUFFIX;
    Object theSource = new Object() {
        public String toString() {
            return "Oriental Daily";
        }
    };

    NotificationsManager.registerSource(ID, theSource,
            NotificationsConstants.IMPORTANT);

    new Thread() {
        public void run() {
            try {
                HttpConnection httpConnection = (HttpConnection) Connector
                        .open(registerUrl);
                InputStream is = httpConnection.openInputStream();
                String response = new String(IOUtilities.streamToBytes(is));
                close(httpConnection, is, null);

                String nextUrl = formRegisterRequest(BPAS_URL, APP_ID,
                        response) + CONNECTION_SUFFIX;
                HttpConnection nextHttpConnection = (HttpConnection) Connector
                        .open(nextUrl);
                InputStream nextInputStream = nextHttpConnection
                        .openInputStream();
                response = new String(
                        IOUtilities.streamToBytes(nextInputStream));
                close(nextHttpConnection, is, null);
            } catch (IOException e) {
            }
        }
    }.start();
}
}
}

これはprocess;です。

public class PushMessageReader {
private static final String MESSAGE_ID_HEADER = "Push-Message-ID";
private static final String MESSAGE_TYPE_TEXT = "text";
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];

public static final long ID = 0x749cb23a75c60e2dL;
public static Bitmap popup = Bitmap.getBitmapResource("icon_24.png");

private PushMessageReader() {
}

public static void process(PushInputStream pis, Connection conn) {
    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();

        if (!alreadyReceived(msgId)) {
            byte[] binaryData;

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

            if (msgType.indexOf(MESSAGE_TYPE_TEXT) >= 0) {
                int size = pis.read(buffer);
                binaryData = new byte[size];
                System.arraycopy(buffer, 0, binaryData, 0, size);

                NotificationsManager.triggerImmediateEvent(ID, 0, null,
                        null);
                processTextMessage(buffer);
            } else if (msgType.indexOf(MESSAGE_TYPE_IMAGE) >= 0) {
                int size = pis.read(buffer);

                if (encoding != null && encoding.equalsIgnoreCase("base64")) {
                    Base64InputStream bis = new Base64InputStream(
                            new ByteArrayInputStream(buffer, 0, size));
                    size = bis.read(imageBuffer);
                }

                binaryData = new byte[size];
                System.arraycopy(buffer, 0, binaryData, 0, size);
            }
        }
        pis.accept();
    } catch (Exception e) {

    } finally {
        PushAgent.close(conn, pis, null);
    }
}

private static boolean alreadyReceived(String id) {
    if (id == null) {
        return false;
    }

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

    messageIdHistory[historyIndex++] = id;

    if (historyIndex >= MESSAGE_ID_HISTORY_LENGTH) {
        historyIndex = 0;
    }
    return false;
}

private static void processTextMessage(final byte[] data) {
    synchronized (Application.getEventLock()) {

        UiEngine ui = Ui.getUiEngine();

        GlobalDialog screen = new GlobalDialog("New Notification",
                "Article ID : " + new String(data), new String(data));

        ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
    }
}

static class GlobalDialog extends PopupScreen implements
        FieldChangeListener {
    ButtonField mOKButton = new ButtonField("OK", ButtonField.CONSUME_CLICK
            | FIELD_HCENTER);
    String data = "";

    public GlobalDialog(String title, String text, String data) {
        super(new VerticalFieldManager());
        this.data = data;

        add(new LabelField(title));
        add(new SeparatorField(SeparatorField.LINE_HORIZONTAL));
        add(new LabelField(text, DrawStyle.HCENTER));

        mOKButton.setChangeListener(this);
        add(mOKButton);
    }

    public void fieldChanged(Field field, int context) {
        if (mOKButton == field) {
            try {
                ApplicationManager.getApplicationManager().launch(
                        "OrientalDailyBB");
                ApplicationManager.getApplicationManager().postGlobalEvent(
                        ID, 0, 0, data, null);

                ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
                        .getInstance();
                reg.unregister();

                close();
            } catch (ApplicationManagerException e) {
            }
        }
    }
}
}

このプロジェクトでは、このバックグラウンドアプリリスナーが常に実行され、BB App Descriptorで開始層が7に設定されるように、起動時に自動実行をチェックしました。

ただし、ポップアップDialogを表示することはできませんが、デバイスがプッシュ通知を受信したことはわかります。

ダイアログポップアップが表示され、ユーザーが[ OK ]をクリックした後、OrientalDailyBBプロジェクトが開始され、特定のが表示されますMainScreen

参考OrientalDailyBB:バックグラウンドアプリケーションであるため、リスナーの優先度はプロジェクトよりも高くなります。したがって、をインストールするOrientalDailyBBと、このリスナーもインストールされます。これは、このリスナーがOrientalDailyBBプロジェクトフォルダーではなく、別のフォルダーにあるために発生しました。ユーザーがを終了したときにバックグラウンドアプリケーションが終了しないように、それらを分離しましたOrientalDailyBB

4

1 に答える 1

1

これはスレッドの問題だと思います。pushGlobalScreenで使用できるメッセージですinvokeLater

UiApplication.getUiApplication().invokeLater(new Runnable() {
  public void run() {
    ui.pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
  }
});

アプリケーションをフォアグラウンドにプッシュすることもできます。

于 2012-07-31T10:56:03.937 に答える