如何恢复在pubnub中丢失的有关恢复连接的丢失消息? [英] How to receive missed messages on resuming connection lost in pubnub?
本文介绍了如何恢复在pubnub中丢失的有关恢复连接的丢失消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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.
推荐答案
根据PubNub示例聊天,您需要实现代码以获取最后n条消息(n:首选消息数).
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());
}
});
}
这篇关于如何恢复在pubnub中丢失的有关恢复连接的丢失消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文