I try to get only mails from Outbox:
String user = "me";
ListMessagesResponse response = mService.users().messages().list(user).execute();
//set selected labels
//[CATEGORY_PERSONAL, CATEGORY_SOCIAL, Регистрации, CATEGORY_FORUMS, IMPORTANT, CATEGORY_UPDATES, CHAT, SENT, INBOX, TRASH, CATEGORY_PROMOTIONS, DRAFT, SPAM, STARRED, UNREAD]
List<String> labelIds = new ArrayList<>();
labelIds.add("SENT");
List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
messages.addAll(response.getMessages());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = mService.users().messages().list(user).setLabelIds(labelIds).setPageToken(pageToken).execute();
} else {
break;
}
}
but I get Inbox messages too
I need to set filter on the first response line.
This code work:
String user = "me";
//set selected labels
//[CATEGORY_PERSONAL, CATEGORY_SOCIAL, Регистрации, CATEGORY_FORUMS, IMPORTANT, CATEGORY_UPDATES, CHAT, SENT, INBOX, TRASH, CATEGORY_PROMOTIONS, DRAFT, SPAM, STARRED, UNREAD]
List<String> labelIds = new ArrayList<>();
labelIds.add("SENT");
ListMessagesResponse response = mService.users().messages().list(user).setLabelIds(labelIds).execute();
List<Message> messages = new ArrayList<Message>();
while (response.getMessages() != null) {
messages.addAll(response.getMessages());
if (response.getNextPageToken() != null) {
String pageToken = response.getNextPageToken();
response = mService.users().messages().list(user).setPageToken(pageToken).execute();
} else {
break;
}
}
Related
I have tried sending a Sinch message in my Android app and it works perfectly when sending to a single recipient
MyClass
String userId= "abc";
CustomReplyMessageBuild customMessage = new CustomReplyMessageBuild();
customMessage.setRecepientIDs(userId);
Map<String, String> m1 = new HashMap<String, String>();
m1.put(“param1", "YES");
m1.put(“param2”, 123);
m1.put(“param3", time);
customMessage.setHeaders(m1);
messageService.sendMessage(customMessage);
MyBean
private List<String> RecepientIds = new ArrayList<String>();
private Map map;
private void setHeaders(Map map) {
this.map = map;}
private void setRecepientIDs(String id) {
this.RecepientIds.add(id);
}
MyService
private MessageClient messageClient = client.getMessageClient();
public void sendMessage(MyClass.CustomReplyMessageBuild conf_message){`
if(messageClient != null){
WritableMessage message = new WritableMessage(conf_message);
messageClient.send(message);
}
}
But when i try passing a list of recipient ids (for a broadcast sort of feature) , i am unable to receive the message at the other end .
MyBroadcastClass
List<String> broadcastIdList;
String recepId = "a,b,c";
CustomReplyMessageBuild customMessage = new CustomReplyMessageBuild();
broadcastIdList = Arrays.asList(recepId.split(","));
customMessage.setRecepientIdList(broadcastIdList);
Map<String, String> m1 = new HashMap<String, String>();
m1.put(“param1", "YES");
m1.put(“param2”, 123);
m1.put(“param3", time);
customMessage.setHeaders(m1);
messageService.sendMessage(customMessage);
MyBroadcastBean
private List<String> RecepientIds = new ArrayList<String>();
private Map map;
private void setHeaders(Map map) {
this.map = map;}
private void setRecepientIdList(List<String> idList) {
this.RecepientIds.addAll(idList);
}
Please let me know if you spot an error or can post a sample snippet or link to solve this problem.
Thanks a ton!
To send messages to multiple recipients try the following constructor:
// Create a WritableMessage and send to multiple recipients
WritableMessage message = new WritableMessage(
{"recipient user id 1", "recipient user id 2"},
"Hello recipients! How are you?");
// Send it
messageClient.send(message);
Using gmail-api I am trying to mak a message as "read" as below but it s not working.
ModifyMessageRequest mods = new ModifyMessageRequest()
.setRemoveLabelIds(new ArrayList<String>(Arrays.asList("UNREAD")));
com.google.api.services.gmail.model.Message message = null;
try {
message = mService.users().messages().modify(acct.sEmail, gmsailID, mods).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
The argument for setRemoveLabelIds() is not the Label String but the Label ID.
So get the label ID from the code below and use that in your function
List<String> labels = new ArrayList<String>();
ListLabelsResponse listResponse = service.users().labels().list(userId).execute();
for (Label label : listResponse.getLabels()) {
Label countLabel = service.users().labels().get(userId, label.getId()).execute();
if (countLabel.getName().equalsIgnoreCase("UNREAD"))
unreadId = countLabel.getId();
}
List<String> add = new ArrayList<String>();
List<String> remove = new ArrayList<String>();
remove.add(unreadId);
modifyMessage(service, userId, message.getId(), add, remove);
public static void modifyMessage(Gmail service, String userId, String messageId,
List<String> labelsToAdd, List<String> labelsToRemove) throws IOException {
ModifyMessageRequest mods = new ModifyMessageRequest().setAddLabelIds(labelsToAdd)
.setRemoveLabelIds(labelsToRemove);
Message message = service.users().messages().modify(userId, messageId, mods).execute();
}
Here is one that I use (after getting messageId with ListMessagesResponse):
public void modifyMessage(String userId, String messageId) throws IOException {
List<String> lblIDRemove=new ArrayList<String>();
lblIDRemove.add("UNREAD");
ModifyMessageRequest mods = new ModifyMessageRequest()
.setAddLabelIds(null)
.setRemoveLabelIds(lblIDRemove);
Message message = mService.users().messages().modify(userId, messageId, mods).execute();
}
This is how to get label information for Gmail accounts: http://android-developers.blogspot.ca/2012/04/gmail-public-labels-api.html
Recently, Google announced Gmail 5.0 with exchange feature. How can we see label information for exchange accounts?
For getAccountsByTypeAndFeatures() method, I had to change ACCOUNT_TYPE_GOOGLE to ACCOUNT_TYPE_EXCHANGE and features=null in order to get exchange account.
private static final String ACCOUNT_TYPE_GOOGLE = "com.google";
private static final String ACCOUNT_TYPE_IMAP = "com.google.android.gm.legacyimap";
private static final String ACCOUNT_TYPE_EXCHANGE = "com.google.android.gm.exchange";
AccountManager.get(this).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_EXCHANGE, null,
new AccountManagerCallback<Account[]>() {
#Override
public void run(AccountManagerFuture<Account[]> future) {
Account[] accounts = null;
try {
accounts = future.getResult();
} catch (Exception e) {
Log.e(TAG, "Got OperationCanceledException", e);
}
}
}, null /* handler */);
After getting Account[] information, here's the code to get label information:
final Bundle args = new Bundle();
args.putString("account", sAccount);
getSupportLoaderManager().restartLoader(0, args, this);
Here's the code to get label information:
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data != null) {
data.moveToFirst();
String name = data.getString(data.getColumnIndex(GmailContract.Labels.NAME));
String unread = data.getString(data.getColumnIndex(GmailContract.Labels.NUM_UNREAD_CONVERSATIONS));
String total = data.getString(data.getColumnIndex(GmailContract.Labels.NUM_CONVERSATIONS));
String uri = data.getString(data.getColumnIndex(GmailContract.Labels.URI));
}
}
For Gmail accounts, I get label information back (name, unread, total, uri, etc). However, for exchange accounts, I don't get any information back.
Is there a way?
I am creating webDialog for sending friend request on facebook.I am able to create web-dialog and send friend request but I don't know to parse bundle date.Once request is send and if there is no error I am getting the response of facebook in the following manner Bundle[{to[0]=100005695389624, to[1]=100002812207673, request=333965433373671}].I want to parse this data.How can I do this.
I am able to get request from the above data but how can i get the to parameter from it.If any one is having any idea then please let me know.
I tried in the following manner.
final String requestId = values.getString("request"); // This value retrieved properly.
char at[] = values.getString("to").toCharArray(); // returns null
String str[] = values.getStringArray("to"); // returns null
String s = values.getString("to"); // return null
I am creating WebDialog for inviting friends of facebook.In response of that I am getting the values in bundle in following format.
Bundle[{to[0]=10045667789624, to[1]=1353002812207673, request=1234555}]
So I was having issues in parsing data of the bundle.I resolved it in the following manner.
Bundle params = new Bundle();
params.putString("message", "Message from Android App.");
WebDialog requestsDialog = (
new WebDialog.RequestsDialogBuilder(ChatRoom.this,
Session.getActiveSession(),
params))
.setOnCompleteListener(new OnCompleteListener() {
#Override
public void onComplete(Bundle values,FacebookException error) {
if( values != null)
{
final String requestId = values.getString("request");
ArrayList<String> friendsId = new ArrayList<String>();
int i = 0;
String to;
do {
to = values.getString("to[" +i + "]");
if(!TextUtils.isEmpty(to))
{
friendsId.add(to);
}
i++;
} while (to != null);
if (requestId != null) {
Toast.makeText(ChatRoom.this.getApplicationContext(),"Request sent",Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(ChatRoom.this.getApplicationContext(),"Request cancelled",Toast.LENGTH_SHORT).show();
}
}
toggle();
}
})
.build();
requestsDialog.show();
Hope this could help someone.
I don't know whether it will work, but try viewing the to array as just a String.
final String requestId = values.getString("request");
final String to0 = values.getString("to[0]");
final String to1 = values.getString("to[1]");
If you don't know how many of these to strings you have, you could create a simple while loop and continue until it returns null. It's not an elegant solution, but it's the only one I can come up with right now. If you know more about the bundle, you can probably find a better solution.
ArrayList<String> to = new ArrayList<String>();
int i = 0;
while (true) {
String x = values.getString("to["+i+"]");
if (x == null) {
break;
} else {
to.add(x);
i++;
}
}
I am building an app which needs to support Hebrew letters (שלום). I am sending a message thru GCM (Google Cloud Messaging). Whenever my OnMessage is called the string (Hebrew) that I am receiving is empty.
Is there any easy workaround for this? Thanks in advance!
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra(GCM_COMMAND);
String action = intent.getAction();
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
message = intent.getStringExtra(GCM_COMMAND);
String s = message.toString();
ItemBean ib =parseJson(s,context);
}
private ItemBean parseJson(String text,Context con) {
ItemBean item = null;
if (text != null) {
JSONObject temp;
try {
temp = new JSONObject(text);
String itemMessage = temp.get(GCM_MESSAGE).toString();
//This String is Empty!
String itemDate = temp.get(GCM_DATE).toString();
long currentTime = System.currentTimeMillis();
item = new ItemBean(currentTime,itemMessage,itemDate);
sendNotification(con,itemMessage,itemDate);
}
}
return item;
}
Server code from NetBeans
JSONObject json = new JSONObject();
System.out.println("******גגגגגגשלום*********");
json.put(GCM_MESSAGE, ib.getmMessage());
//This is hebrew no problem!
json.put(GCM_DATE, ib.getmDate());
Message.Builder builder = new Message.Builder();
Message message = builder.build();
MulticastResult result = sender.send(message, listOfRegisteredDevices, 5);
int success = result.getSuccess();
Can you try :
String itemMessage = temp.getString(GCM_MESSAGE);
and also
json.put(GCM_MESSAGE, JSonObject.quote( ib.getmMessage() ) );