Android 安卓Pubnub一对一聊天
我想使用android中的pubnub库实现在线/离线/ISTING功能。为此,我创建了Pubnub连接Android 安卓Pubnub一对一聊天,android,pubnub,Android,Pubnub,我想使用android中的pubnub库实现在线/离线/ISTING功能。为此,我创建了Pubnub连接 //create the pubnub connection PNConfiguration configuration = new PNConfiguration(); configuration.setPublishKey(PUBNUB_PUBLISH_KEY); configuration.setSubscribeKey(PUBNUB
//create the pubnub connection
PNConfiguration configuration = new PNConfiguration();
configuration.setPublishKey(PUBNUB_PUBLISH_KEY);
configuration.setSubscribeKey(PUBNUB_SUBSCRIBE_KEY);
configuration.setLogVerbosity(PNLogVerbosity.BODY);
configuration.setConnectTimeout(100000);
configuration.setSubscribeTimeout(31000);
configuration.setHeartbeatNotificationOptions(PNHeartbeatNotificationOptions.ALL);
configuration.setPresenceTimeoutWithCustomInterval(120,59);
configuration.setPresenceTimeout(120);
mPubNub = new PubNub(configuration);
然后,我将订户回调设置为侦听器
**strong text**
private SubscribeCallback subscribeCallback = new SubscribeCallback() {
@Override
public void status(PubNub pubnub, PNStatus status) {
Log.d("Chat", "status() called with: pubnub = [" + pubnub + "], status = [" + status + "]");
if (status.getOperation() != null) {
switch (status.getOperation()) {
// let's combine unsubscribe and subscribe handling for ease of use
case PNSubscribeOperation:
case PNUnsubscribeOperation:
Toast.makeText(ChatAppService.this, "Status : " + status.getOperation().name(), Toast.LENGTH_SHORT).show();
// note: subscribe statuses never have traditional
// errors, they just have categories to represent the
// different issues or successes that occur as part of subscribe
switch (status.getCategory()) {
case PNConnectedCategory:
// this is expected for a subscribe, this means there is no error or issue whatsoever
Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
break;
case PNReconnectedCategory:
// this usually occurs if subscribe temporarily fails but reconnects. This means
// there was an error but there is no longer any issue
Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
HashMap<String,String> map = new HashMap();
map.put("State","Online");
pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(map).uuid(pubnub.getConfiguration().getUuid());
break;
case PNDisconnectedCategory:
// this is the expected category for an unsubscribe. This means there
// was no error in unsubscribing from everything
Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
HashMap<String,String> mapOffline = new HashMap();
mapOffline.put("State","Offline");
pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(mapOffline).uuid(pubnub.getConfiguration().getUuid());
break;
case PNTimeoutCategory:
HashMap<String,String> mapTimeout = new HashMap();
mapTimeout.put("State","Offline");
pubnub.setPresenceState().channels(Arrays.asList(Constants.GLOBAL_CHANNEL)).state(mapTimeout).uuid(pubnub.getConfiguration().getUuid());
pubnub.reconnect();
break;
case PNUnexpectedDisconnectCategory:
// this is usually an issue with the internet connection, this is an error, handle appropriately
Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
pubnub.reconnect();
break;
case PNAccessDeniedCategory:
// this means that PAM does allow this client to subscribe to this
// channel and channel group configuration. This is another explicit error
Toast.makeText(ChatAppService.this, "Status : " + status.getCategory(), Toast.LENGTH_SHORT).show();
break;
default:
// More errors can be directly specified by creating explicit cases for other
// error categories of `PNStatusCategory` such as `PNTimeoutCategory` or `PNMalformedFilterExpressionCategory` or `PNDecryptionErrorCategory`
}
break;
case PNHeartbeatOperation:
// heartbeat operations can in fact have errors, so it is important to check first for an error.
// For more information on how to configure heartbeat notifications through the status
// PNObjectEventListener callback, consult <link to the PNCONFIGURATION heartbeart config>
if (status.isError()) {
// There was an error with the heartbeat operation, handle here
} else {
// heartbeat operation was successful
}
break;
default: {
// Encountered unknown status type
}
}
} else {
// After a reconnection see status.getCategory()
}
}
@Override
public void message(PubNub pubnub, PNMessageResult message) {
//TODO: If App open: Save to database and broadcast for database change else show Notification
}
@Override
public void presence(PubNub pubnub, PNPresenceEventResult presence) {
Log.d("chat", "presence() called with: pubnub = [" + pubnub + "], presence = [" + presence + "]");
}
};`
当我订阅频道时,状态回调调用三次
1.活动:加入
2.事件:状态更改
3.活动:休假
我不知道如何在Android中处理这种情况。请帮帮我,我最近三天一直在这个问题上结巴。我注意到类似的行为。你找到解决办法了吗?这两条线的作用是一样的。你可以消除其中的一个。setPresenceTimeoutWithCustomInterval(120,59);setPresenceTimeout(120);您是使用两个客户端进行测试还是使用一个客户端进行测试。如果有,那么你不能假设你会有自己的休假活动。但仍然不能确定你的情况。您是否可以启用日志记录并复制:您是否放弃了此功能?如果您仍然需要帮助,请告诉我们。尝试发送到
pubnub.unsubscribe().channels(Arrays.asList(channel)).execute()