moezbhatti / qksms

The most beautiful SMS messenger for Android
https://qklabs.com/download
GNU General Public License v3.0
4.47k stars 1.06k forks source link

Message From new phone number will now show in sms list #1200

Closed KruziikG closed 6 years ago

KruziikG commented 6 years ago

DESCRIPTION

I noticed that sometimes QKSMS will not show new message in sms list and also there in no notification in status bar. Only if I chose Settings->Sync messages then these messages will show in QKSMS. I tried several different situations and finally know it only happens when you haven't have a conversion with the sender. device: Huawei P9 Plus, running Android 8.0

STEPS

  1. Delete previous conversion with the sender if exist
  2. Send a message to your phone using the number

EXPECTED

The phone should produce a sound ringtone, and the new message should show in both notification bar and the QKSMS app.

OBSERVATIONS

  1. there is no ringtone, no notification , and you cannot find the message in QKSMS. But if you open another Messaging app, the message is displayed in that app.
  2. return to QKSMS app, click ’Settings->Sync messages‘
  3. finally the message will show in QKSMS

I will attach two logs, the first one is captured in this situation: Screen is locked. I send three messages in this consequence :1. phone number A(new number) 2. phone number B(I have had conversion with this number privously)3.phone number C(another new number). Message 1 & 3 are now shown in QKSMS, but message 2 did. the second log is captured when phone is Waked up and QKSMS is in foreground. I Send a message using phone number A. This time the message is also now shown in QKSMS. qksms_log1.txt qksms_log2.txt

this gif shows that when I finished send there 4 messages, I can only manual re-sync message. gif5

moezbhatti commented 6 years ago

Fixed in 52c99faed130c3dcce899983e24cb6354da06e98

moezbhatti commented 6 years ago

@KruziikG If you delete the conversation and then receive a message from it again, does it show up in the app normally or do you need to sync it?

KruziikG commented 6 years ago

@moezbhatti I need to sync it gif

moezbhatti commented 6 years ago

@KruziikG QKSMS will only be able to stay in sync if it's set as the default SMS app

On Xiaomi devices when you open the default messaging app, it automatically assigns itself as the default SMS app, which means QKSMS won't receive your messages

I just tested this on my personal Xiaomi device using two different phones, and I can confirm that while it didn't work on 3.2.2, it works fine on 3.3.3

KruziikG commented 6 years ago

@moezbhatti QKSMS is always the default SMS app. I think this only happen on some HUAWEI devices.

I added a log here: 01

when add message to existing conversation, everything is OK 02

but when start a new conversation, threadId is 0, and this is an error: (error message means : W/System.err: java.io.IOException: Do not send same content again.) 03

Then I run this version using Android emulator. This app can start new conversation , and the difference is that the threadId for new conversation is not 0. like this 04

moezbhatti commented 6 years ago

@KruziikG thank you for the detail! I'll make sure this gets fixed

moezbhatti commented 6 years ago

Weirdly, this issue doesn't happen on my P9 Lite. If I delete the conversation and then receive a new message from it, then the conversation appears as normal

moezbhatti commented 6 years ago

@KruziikG @gkomarnicki would you mind trying this APK and seeing if it resolves the issue for you?

presentation-noAnalytics-universal-release.apk.zip

gabkomarnicki commented 6 years ago

@moezbhatti The bug's still there. Android Messages and Pulse both got the SMS, while QKSMS didn't. QKSMS is the default SMS app

moezbhatti commented 6 years ago

@gkomarnicki hmmm, perhaps this one?

presentation-noAnalytics-universal-release.apk.zip

KruziikG commented 6 years ago

@moezbhatti Yes, so weird. In fact, The android 8.0 of my P9 Plus is still a beta version. So I'm not sure whether it is a system bug. Nova 2i got android 8.0 beta 2 months before P9, and huawei has released Final version On 1 Jun. Maybe this problem is caused by huawei, and only happens in its EMUI 8.0 systems, who knows.

Another wired thing may be relatied. When using AOSP messaging app, All messages display as 'unknown sender'. But google messages works fine. screenshot_20180621-222749

and today I tried to debug qksms, and find that not only the 'threadId' is 0, but also the conversation is null, default


and I tried both apks just now, this problen is still there. Here are the logs

log06211.log log06212.log

moezbhatti commented 6 years ago

@KruziikG can you try replacing the insertReceivedSms method with this

    override fun insertReceivedSms(subId: Int, address: String, body: String, sentTime: Long): Message {

        // Insert the message to the native content provider
        val uri = contentValuesOf(
                Pair(Telephony.Sms.ADDRESS, address),
                Pair(Telephony.Sms.BODY, body),
                Pair(Telephony.Sms.DATE_SENT, sentTime),
                Pair(Telephony.Sms.SUBSCRIPTION_ID, subId))
                .let { values -> context.contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }

        // Insert the message to Realm
        return Message().apply {
            this.address = address
            this.body = body
            this.dateSent = sentTime
            this.date = System.currentTimeMillis()
            this.subId = subId

            id = messageIds.newId()
            threadId = TelephonyCompat.getOrCreateThreadId(context, address)
            contentId = uri?.lastPathSegment?.toLong() ?: 0L
            boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
            type = "sms"
            insertOrUpdate()
        }
    }

And can you let me know the value of uri.toString() and the value of threadId

KruziikG commented 6 years ago

I recall that my workmate has a huawei Nova young running EMUI 8.0 too, I can use his phone to test if his phone be affected. I know little about android app development, so I can only help to test. Just tell me if I can help.

I see your last message just now,, let me have a try.

moezbhatti commented 6 years ago

@KruziikG no worries, I assume that all of the affected devices will be affected in the same way. I'll send you code snippets and ask you to obtain certain values for me, this should help us resolve the issue by the end of the day :)

gabkomarnicki commented 6 years ago

@moezbhatti Working on my end now. Thanks!

@KruziikG Your P9 Plus is running 8.0 Oreo beta? I guess it's a weird bug on Huawei's end.

moezbhatti commented 6 years ago

Oh hm, that's interesting that it worked for you @gkomarnicki but not @KruziikG

@KruziikG let me know what the results are when you try that code snippet!

KruziikG commented 6 years ago

OK. And while syncing gradle files, I tried the second apk again, and I'm sure it not work for me. @gkomarnicki did you delete previous messages before test?

KruziikG commented 6 years ago

@moezbhatti both 'contentValuesOf' and 'values ' are not defined,don't know how to fix this.

123123

moezbhatti commented 6 years ago

@KruziikG you'll need to update to the latest version of the dev branch first

gabkomarnicki commented 6 years ago

@KruziikG I deleted most of the conversations from unknown numbers before testing, but not all of them.

screenshot_20180622-060950

In this thread where I received 2FA verification codes, there was no notification for the first two messages, but I got a notification for the third message after installing the most recent APK linked above.

moezbhatti commented 6 years ago

@KruziikG which version of android studio are you using? You may need to be on 3.2

KruziikG commented 6 years ago

@moezbhatti problem solved, I tried to imported 2 packages using alt+enter. But I think this project cannot be build on windows. I will check this later.

moezbhatti commented 6 years ago

@KruziikG thanks. let me know what you end up getting for those values

KruziikG commented 6 years ago

06-22 10:37:12.460 9566-9676/com.moez.QKSMS D/QKSMS: ------>content://sms/inbox//871 06-22 10:37:12.581 9566-9676/com.moez.QKSMS D/QKSMS: threadId-->522

now threadid below also has a value. but this message still not displayed.

moezbhatti commented 6 years ago

@KruziikG can you try this and share the logs

constructor

class MessageRepositoryImpl @Inject constructor(
        private val context: Context,
        private val cursorToConversation: CursorToConversation,
        private val messageIds: KeyManager,
        private val imageRepository: ImageRepository,
        private val prefs: Preferences) : MessageRepository {

insertReceivedSms

    override fun insertReceivedSms(subId: Int, address: String, body: String, sentTime: Long): Message {

        // Insert the message to the native content provider
        val uri = contentValuesOf(
                Pair(Telephony.Sms.ADDRESS, address),
                Pair(Telephony.Sms.BODY, body),
                Pair(Telephony.Sms.DATE_SENT, sentTime),
                Pair(Telephony.Sms.SUBSCRIPTION_ID, subId))
                .let { values -> context.contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }

        val threadId = TelephonyCompat.getOrCreateThreadId(context, address)

        val conversation1 = cursorToConversation.getConversationCursor(threadId)
                ?.map(cursorToConversation::map)
                ?.firstOrNull()

        val conversation2 = cursorToConversation.getConversationsCursor()
                ?.map(cursorToConversation::map)
                ?.firstOrNull { it.id == threadId }

        Timber.d("threadId: $threadId")
        Timber.d("conversation found via method 1: ${conversation1 != null}")
        Timber.d("conversation found via method 2: ${conversation2 != null}")

        // Insert the message to Realm
        return Message().apply {
            this.threadId = threadId
            this.address = address
            this.body = body
            this.dateSent = sentTime
            this.date = System.currentTimeMillis()
            this.subId = subId

            id = messageIds.newId()
            contentId = uri?.lastPathSegment?.toLong() ?: 0L
            boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
            type = "sms"
            insertOrUpdate()
        }
    }
KruziikG commented 6 years ago

OK, here is the result 06-22 11:15:47.161 12557-12646/com.moez.QKSMS D/MessageRepositoryImpl: threadId: 522 06-22 11:15:47.162 12557-12646/com.moez.QKSMS D/MessageRepositoryImpl: conversation found via method 1: false conversation found via method 2: true


and i found something before,in this function, the paremeter is valid, but it return null. And this issue is quite same as the comment said. screenshot from 2018-06-22 11-03-23

moezbhatti commented 6 years ago

@KruziikG okay, I believe this should fix the issue!

MessageRepositoryImpl.insertReceivedSms()

    override fun insertReceivedSms(subId: Int, address: String, body: String, sentTime: Long): Message {

        // Insert the message to the native content provider
        val uri = contentValuesOf(
                Pair(Telephony.Sms.ADDRESS, address),
                Pair(Telephony.Sms.BODY, body),
                Pair(Telephony.Sms.DATE_SENT, sentTime),
                Pair(Telephony.Sms.SUBSCRIPTION_ID, subId))
                .let { values -> context.contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }

        // Insert the message to Realm
        return Message().apply {
            this.address = address
            this.body = body
            this.dateSent = sentTime
            this.date = System.currentTimeMillis()
            this.subId = subId

            id = messageIds.newId()
            threadId = TelephonyCompat.getOrCreateThreadId(context, address)
            contentId = uri?.lastPathSegment?.toLong() ?: 0L
            boxId = Telephony.Sms.MESSAGE_TYPE_INBOX
            type = "sms"
            insertOrUpdate()
        }
    }

ConversationRepositoryImpl.getConversationFromCp()

    private fun getConversationFromCp(threadId: Long): Conversation? {
        return cursorToConversation.getConversationsCursor()
                ?.map(cursorToConversation::map)
                ?.firstOrNull { it.id == threadId }
                ?.let { conversation ->
                    val realm = Realm.getDefaultInstance()
                    val contacts = realm.copyFromRealm(realm.where(Contact::class.java).findAll())

                    val recipients = conversation.recipients
                            .map { recipient -> recipient.id }
                            .map { id -> cursorToRecipient.getRecipientCursor(id) }
                            .mapNotNull { recipientCursor ->
                                // Map the recipient cursor to a list of recipients
                                recipientCursor?.use { recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
                            }
                            .flatten()
                            .map { recipient ->
                                recipient.apply {
                                    contact = contacts.firstOrNull {
                                        it.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
                                    }
                                }
                            }

                    conversation.recipients.clear()
                    conversation.recipients.addAll(recipients)
                    realm.executeTransaction { it.insertOrUpdate(conversation) }
                    realm.close()

                    conversation
                }
    }
KruziikG commented 6 years ago

It Work! Great! But.....Let me come up with a new issue about display, hahaha

fazlerabbi37 commented 6 years ago

Hi there, I can see here that this issue has been closed with version 3.3.4 but I didn't find any update in F-Droid. When I chceked the vsersion info in F-Droid wiki, it say's they couldn't build the vsersion 3.3.4. Can you please check the issue?

moezbhatti commented 6 years ago

@fazlerabbi37 it gives the following reason, Reason: uses com.android.tools.build:gradle:3.2.0-alpha18

In 3.3.6 I've updated to 3.2.0-beta01. Hopefully this helps, though I'm not sure what requirements they have for their automated builds