Problem 1: getId() might return null with your current implementation
Problem 2: I don't think you want to compare only the last message in history. Let's say I'm sending split messages to Contact1 (Vodafone) and Contact2 (T-mobile) simultaneously. The sms fragments from Contact1 and Contact2 will alternate as they are being saved. With your current implementation, they won't be concatenated.