如何在pubnub中收到关于恢复连接丢失的错过消息?

时间:2015-02-27 06:41:20

标签: android push-notification android-service pubnub

我尝试了https://github.com/pubnub/java/tree/master/android#connection-durability-reconnecting--resuming-when-a-connection-is-lost-or-changed,但我仍然无法弄清楚如何使用 setResumeOnReconnect()。 我正在运行android-service并在服务的onCreate()中添加了以下代码片段。

pubnubBroadcastReceiver =new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {                          
                ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                int networkType = intent.getExtras().getInt(ConnectivityManager.EXTRA_NETWORK_TYPE);
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networkType);
                boolean isConnected = networkInfo.isConnected();
                if(isConnected) {
                    ApplicationLoader.getPubnub().disconnectAndResubscribe();
                   // ApplicationLoader.getPubnub().setResumeOnReconnect(true);
                }
            }
        };
        registerReceiver(pubnubBroadcastReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
    }

不知道我在这里缺少什么。

1 个答案:

答案 0 :(得分:0)

根据PubNub示例聊天,您需要实现代码以获取最后n条消息(n:首选消息数)。

/**
 * Get last 100 messages sent on current channel from history.
 */
public void history() {
    this.mPubNub.history(this.channel, 100, false, new Callback() {
        @Override
        public void successCallback(String channel, final Object message) {
            try {
                JSONArray json = (JSONArray) message;
                Log.d("History", json.toString());
                final JSONArray messages = json.getJSONArray(0);
                final List<ChatMessage> chatMsgs = new ArrayList<ChatMessage>();
                for (int i = 0; i < messages.length(); i++) {
                    try {
                        if (!messages.getJSONObject(i).has("data"))
                            continue;
                        JSONObject jsonMsg = messages.getJSONObject(i)
                                .getJSONObject("data");
                        String name = jsonMsg
                                .getString(Constants.JSON_USER);
                        String msg = jsonMsg.getString(Constants.JSON_MSG);
                        long time = jsonMsg.getLong(Constants.JSON_TIME);
                        ChatMessage chatMsg = new ChatMessage(name, msg,
                                time);
                        chatMsgs.add(chatMsg);
                    } catch (JSONException e) { // Handle errors silently
                        e.printStackTrace();
                    }
                }

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "RUNNIN",
                                Toast.LENGTH_SHORT).show();
                        mChatAdapter.setMessages(chatMsgs);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void errorCallback(String channel, PubnubError error) {
            Log.d("History", error.toString());
        }
    });
}