How to receive missed messages on resuming connect

2019-06-01 08:42发布

问题:

I tried https://github.com/pubnub/java/tree/master/android#connection-durability-reconnecting--resuming-when-a-connection-is-lost-or-changed but still I'm not able to figure out how to use setResumeOnReconnect() . I'm running android-service and added following code snippet inside onCreate() of the service.

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

Don't know what I'm missing here.

回答1:

As per PubNub sample chat, you need to implement code to get the last n messages(n: number of messages preferred).

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