【发布时间】:2016-03-09 05:25:44
【问题描述】:
我正在开发一个 android 即时聊天应用程序。我已经使用 socket.io-client 库实现了它。这里一切正常。当应用程序将由不同设备上的不同用户使用时,可以使用套接字将消息从一个用户发送到另一个用户。但这只有在应用程序处于活动状态。如果应用程序在另一端未处于活动状态,则它无法正常工作。我想像 WatsApp 一样通知用户消息已到达。我如何使用套接字来实现这一点。
以下是我的 Listener 的代码:
mSocket.on("send:notice", onReceive); // Listening event for receiving messages
// Event Listener for receiving messages
private Emitter.Listener onReceive = new Emitter.Listener() {
@Override
public void call(final Object... args) {
Log.e("Receive", "Bean_Message received");
runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
Log.e("DATA", String.valueOf(data));
try {
JSONArray ops = data.getJSONArray("ops");
Log.e("JSONArray", ops.toString());
for (int i = 0; i < ops.length(); i++) {
JSONObject object = ops.getJSONObject(i);
String roomID = object.getString("room_id");
Log.e("RoomID", roomID); // Getting room ID from JSON array
Log.e("Md5RoomID", md5StringRoomID); // Getting room id which we have created using logged in user ID and room id of the user through which chat has to be done
//Comparing the room IDs
if (md5StringRoomID.equals(roomID)) {
String senderName = object.getString("user");
Log.e("Sender Name", senderName);
String senderID = object.getString("id");
Log.e("SenderID", senderID);
String date = object.getString("date"); // Getting timestamp value in the form of string
long time = Long.parseLong(date); //Converting String into long
String t = getDateFromTimestamp(time); // Getting date and time from timestamp
Log.e("DateReceived", date);
// JSONObject message = object.getJSONObject("message");
String unicodeMessageReceived = object.getString("message");
Log.e("UnicodeMessageReceived", unicodeMessageReceived);
//Decoding unicode characters for emoji
String messageReceived = StringEscapeUtils.unescapeJava(unicodeMessageReceived);
Log.e("DecodedMessageReceived", messageReceived);
String loggedInUSerNAme = sharedPreferences.getString(Prefs_Registration.get_user_name, null);
//String loggedInUSerNAme = almaChatDatabase.getUserName();
//If the message is sent by the owner to other from webapp ,then we need to check whether the sender is the loggedinUSer in the App or not and we will right align the messages .
if (loggedInUSerNAme.equalsIgnoreCase(senderName)) {
isSelf = true;
msg = new Bean_Message(senderName, messageReceived, isSelf);
msg.setMessageStatus(Status.SENT);
msg.setTime(t);
listBeanMessages.add(msg);
// Log.e("List Elements", String.valueOf(listBeanMessages));
adapter_message.notifyDataSetChanged();
playBeep();
} else {
isSelf = false;
msg = new Bean_Message(senderName, messageReceived, isSelf);
msg.setTime(t);
listBeanMessages.add(msg);
Log.e("List Elements", String.valueOf(listBeanMessages));
adapter_message.notifyDataSetChanged();
playBeep();
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
// Playing sound when the message is sent by other
public void playBeep() {
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
};
我必须处理两种通知情况: 1.当应用程序在后台时 2. 当应用程序在前台,但是当用户C的消息到达时,用户A正忙于用户B。
请帮我解决我的问题。
【问题讨论】:
-
要么你创建了一个后台服务并保持与套接字的连接,要么你应该使用推送通知,即如果其他用户没有连接到套接字,服务器应该发送一个推送消息。
-
感谢您的回复。可以给我一些学习的链接吗?
-
我已经使用推送通知实现了同样的功能。
-
请详细说明。
-
按照此处developer.android.com/training/run-background-service/… 的描述创建一个后台服务,写入此服务中的套接字连接代码并从启动器活动启动该服务,并通过从其他活动绑定到该服务来使用该服务。