How to get the number of unread gmail mails (on android)
Here's some code snippet. Not sure it works and can't test it. But I hope it will help you to continue the investigation.
public static final class LabelColumns {
public static final String CANONICAL_NAME = "canonicalName";
public static final String NAME = "name";
public static final String NUM_CONVERSATIONS = "numConversations";
public static final String NUM_UNREAD_CONVERSATIONS = "numUnreadConversations";
}
public void queryLabels(){
String account="email@company.com";
Uri LABELS_URI = Uri.parse("content://gmail-ls/labels/");
Uri ACCOUNT_URI = Uri.withAppendedPath(LABELS_URI, account);
ContentResolver contentResolver=myActivity.getContentResolver();
Cursor cursor = contentResolver.query(ACCOUNT_URI, null, null, null, null);
//iterate over all labels in the account
if (cursor.moveToFirst()) {
int unreadColumn = cursor.getColumnIndex(LabelColumns.NUM_UNREAD_CONVERSATIONS);
int nameColumn = cursor.getColumnIndex(LabelColumns.NAME);
do {
String name = cursor.getString(nameColumn);
String unread = cursor.getString(unreadColumn);//here's the value you need
} while (cursor.moveToNext());
}
}
Requires permission
<uses-permission android:name="com.google.android.gm.permission.READ_GMAIL"/>
How to get Number of Unread mails count from Gmail account
I never tried this but can you try this.
public class MainActivity extends AppCompatActivity {
AccountManager accountManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
accountManager = AccountManager.get(this);
Account account= getAccount(accountManager);
Log.d("MainActivity","UnreadCount-----> "+getUnreadCount(account.name));
}
public Account getAccount(AccountManager accountManager) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
Account[] accounts = accountManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
public int getUnreadCount(String accountName) {
Cursor cursor = getContentResolver().query(
GmailContract.Labels.getLabelsUri(accountName),
UnreadQuery.PROJECTION, null, null, null
);
try {
if (cursor == null || cursor.isAfterLast()) {
return 0;
}
int unread = 0;
while (cursor.moveToNext()) {
String canonicalName = cursor.getString(UnreadQuery.CANONICAL_NAME);
int unreadInLabel = cursor.getInt(UnreadQuery.NUM_UNREAD_CONVERSATIONS);
if (GmailContract.Labels.LabelCanonicalNames.CANONICAL_NAME_INBOX_CATEGORY_PRIMARY.equals(canonicalName)) {
unread = Math.max(unread, unreadInLabel);
}
}
return unread;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
private interface UnreadQuery {
String[] PROJECTION = {
GmailContract.Labels.NUM_UNREAD_CONVERSATIONS,
GmailContract.Labels.CANONICAL_NAME,
};
int NUM_UNREAD_CONVERSATIONS = 0;
int CANONICAL_NAME = 1;
}
}
Android - How can I find out how many unread email the user has?
I'm the author of Gmail Unread Count. Check out Gmail.java, it's what I use. There are a couple of ways to do it. I read the unread count directly from the label. You should be able to figure out how to use it by reading the source.
Getting the unread count from Email is afaik not possible so you'll have to do polling there.
How to get number of unread gmail emails, not unread conversations
So I finally found possible solution, this worked for me:
turn off message threading
Now NUM_UNREAD_CONVERSATIONS flag gives me the number of unread emails
How to get the number of unread threads in INBOX with Gmail API?
The Gmail API now provides total and unread counts for messages and threads on each Label using the Labels.Get() method. See:
https://developers.google.com/gmail/api/release-notes
and
https://developers.google.com/gmail/api/v1/reference/users/labels
Get the number of unread mails on an Android Device
This question was already answered on How to get the number of unread gmail mails (on android)
gmail-api to check how many mails are unread and user has not replied to
You need to use Users.messages: list
method since it only return messages that match the specified query. It supports the same query format as the Gmail search box. For example, from:someuser@example.com rfc822msgid: is:unread
.
Based also from this related SO ticket, the q
parameter (query) can be all kinds of stuff and it is the same as the gmail search bar on the top of the web interface. You can use this search method to find unread messages, for example likeList<Message> unreadMessageIDs = ListMessages(service, "me", "is:unread");
.
Hope this helps! :)
Related Topics
How to Change Bitmap Image Color in Android
How to Execute the Dex File in Android with Command
Android App Bundle with In-App Locale Change
Error:Cause: Unable to Find Valid Certification Path to Requested Target
Videoview Getdrawingcache Is Returning Black
Autoincrement Versioncode with Gradle Extra Properties
Custom Event Listener on Android App
How to Set "Android:Layout_Below" at Runtime Programmatically
Accessing UI Thread Handler from a Service
Intent - If Activity Is Running, Bring It to Front, Else Start a New One (From Notification)
Getting a Photo from a Contact
Disable Sound from Notificationchannel
Android Studio Stuck on "Gradle: Resolve Dependencies '_Debugcompile'" or 'Detachedconfiguration1'
Datepicker Shows Wrong Value of Month
How to Obtain All Details of a Contact in Android
ASP.NET Web API Authentication