1

I am trying to find the number of unread SMS messages I have received via Google Hangouts.I tried this answer (Using new Telephony content provider to read SMS), and modified the code to count unread messages. It returns 0 every time. If I try to get the read count, by switching "read = 0" to "read = 1", the count is still 0.

I realize this means I have an empty SMS inbox in Hangouts, but in reality I do not. Any ideas on what I might be doing wrong?

When I switch my default SMS app to Messenger, it gets the unread count without a problem.

Finally, this is the code I'm using:

 private int getUnreadMessageCount() {
        Cursor c = getContentResolver().query(Telephony.Sms.Inbox.CONTENT_URI, null, "read = 0", null, null);
        int unreadCount = c.getCount();
        c.close();
        return unreadCount;
    }

Any help is truly appreciated. Thank you!

Community
  • 1
  • 1
Miki P
  • 652
  • 1
  • 9
  • 22
  • Your link is from 2010, I am not sure this is still possible now. Also, just to be sure, [did you request the permission to read SMS to the user](https://developer.android.com/guide/topics/permissions/requesting.html) (API > 23) ? – maxoumime Jan 20 '17 at 00:08
  • I did, yes, (edited the question to include that now). Do you how to do this in 2017? – Miki P Jan 20 '17 at 00:39
  • 1
    `READ_SMS` is a `dangerous permission` now, you will need to [ask for the permission at *runtime*](https://developer.android.com/training/permissions/requesting.html). As for listing SMS _the 2017 way_, you will have to wait for someone else to answer :). But first try with this permission system, or manually grant the permission on the app settings. – maxoumime Jan 20 '17 at 00:44
  • That's still how you query the SMS Provider. If your inbox is empty, but Hangouts is showing messages, then I suspect your problem is Hangouts. It does something funky with messaging all around when it's the default. I don't use it, so I've never had a chance to pin down exactly what it does. To test, you might temporarily set your default to a different SMS app - the basic Messenger, if you have it - send yourself some texts, and do your query again. I'd also suggest querying the base `content://sms` URI, with a null selection, to see if you've got any kind of messages there at all. – Mike M. Jan 20 '17 at 15:47
  • Yep, so it seems to get the count when Messenger is the default. No such luck for Hangouts. – Miki P Jan 20 '17 at 17:04
  • @MikiP did you find a solution for this? Running into the same thing now. – TravisChambers Aug 04 '17 at 14:11
  • @TravisChambers I did not. I ended up using Messenger instead. I think it's the way Google wants devs to go. – Miki P Aug 04 '17 at 15:58

0 Answers0