In my application, a notification should be displayed when a chat message arrives.
There are some rules which decide what notification message is to be displayed.
I have created a helper method which takes a ChatMessage
object and returns the message to be displayed in the notification.
How can I refactor or improve this piece of code?
Any feedback will be appreciated.
private String getNotificationDisplayString(ChatMessage chat) {
String message;
boolean isUpdateMessage = (chat.getTag().equals(TAG_UPDATE_MSG));
if (isUpdateMessage) {
// for update message
UserRelationshipInfo relationshipInfo = UserRelationshipInfoManger.getInstance().getInfo(chat.getUserId());
boolean isUserMyFriend = (relationshipInfo != null && relationshipInfo.getSource() == SOURCE_FRIEND);
message = Resources.string(isUserMyFriend ? R.string.text_user_is_friend : R.string.text_user_is_stranger);
} else {
boolean isDirectMessage = (chat.getType() == DIRECT_MESSAGE);
boolean isTextMessage = (chat.getTag().equals(TAG_TEXT_MSG));
if (isDirectMessage) {
// for direct message (special case)
String messageName = Resources.string(getMessageType(chat.getTag()));
// message = "<name> sent you a direct <image/video/etc>"
message = String.format(Resources.string(R.string.text_new_direct_message), chat.getUserInfo().getDisplayName(), messageName);
} else if (isTextMessage) {
// for text (non-direct)
message = new TextInfo(chat).getText();
} else {
// for non-text (non-direct)
// message = "<name> sent you a message"
message = String.format(Resources.string(R.string.text_new_message), chat.getUserInfo().getDisplayName());
}
}
return message;
}