Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
zmb #1
Member since Jun 2011 · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: gmail notifier
Hi,
I'm trying to implement gmail notifications ( per here: http://code.google.com/apis/talk/jep_extensions/gmail.html ).  I used a DiscoInfoIQ to query the server for the feature and verified in the response that it is supported.  Now I'm trying to query for new mail.  I need to construct an iq stanza similar to this:
  1. <iq type='get'
  2.     from='romeo@gmail.com/orchard'  
  3.    to='romeo@gmail.com'
  4.    id='mail-request-1'>
  5.  <query xmlns='google:mail:notify'
  6. />
  7. </iq>

My code looks like this:
  1.         private void queryForEmail()
  2.         {
  3.             IQ emailQuery = new IQ();
  4.             emailQuery.Type = IqType.get;
  5.             emailQuery.From = xmpp.MyJID;
  6.             emailQuery.To = xmpp.MyJID;
  7.             emailQuery.Id = "newMailQuery";
  8.             emailQuery.Query.SetNamespace("google:mail:notify"); //  null reference exception
  9.             xmpp.Send(emailQuery);
  10.         }
The line in red is throwing a null reference exception and I don't see why.
What are the steps to create and send a custom iq?  Am I completely off?

Thanks for any help.
This post was edited on 2011-06-13, 11:36 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
here is the code to build the Gmail notify packets:

  1. public class GmailNotify : Element
  2. {
  3.  
  4.     public GmailNotify ()
  5.     {
  6.         this.TagName    = "query";
  7.         this.Namespace  = "google:mail:notify";
  8.     }
  9. }
  10.  
  11. public class GmailNotifyIq : IQ
  12. {
  13.     public GmailNotifyIq ()
  14.     {      
  15.         base.Query = new GmailNotify();
  16.         this.GenerateId();
  17.     }
  18. }

And this is the code to send your query then:

  1. private void queryForEmail()
  2. {
  3.     IQ emailQuery = new GmailNotifyIq();
  4.     emailQuery.Type = IqType.get;
  5.     emailQuery.From = xmpp.MyJID;
  6.     emailQuery.To = xmpp.MyJID;    
  7.     xmpp.Send(emailQuery);
  8. }

Please let me know if this works for you.

Alex
Avatar
zmb #3
Member since Jun 2011 · 2 posts
Group memberships: Members
Show profile · Link to this post
That worked perfectly.  I don't understand how to deal with custom incoming packets though (forgive me, this is all new..).  So if I get the response back from the server, and its an iq with a <mailbox> child node [that is correct terminology, right?], how can I look further into the response without writing my own parser.  Is there a simple way to check the 'children' of the iq?  I'm thinking something like
if(iq.HasChild("mailbox")) {//do something..}
My thought is just to check the different types of IQ's in the On_iq handler and if it is a response to the mailbox query, extract the information and present it nicely to the user.  Your library does such a nice job of encapsulating the xml that I can't figure out how to deal with it on my own.
Thanks for your time.
Avatar
Alex #4
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
iq.HasTag() is what you are looking for.

Normally you should write a class for the mailbox tag like the classes I have posted above and register all classes in the ElementFactory for the parser on incoming packets. Then you can also check by type and use generics like HasTag<Mailbox>().

Alex
Avatar
AlexR #5
Member since May 2013 · 2 posts
Group memberships: Members
Show profile · Link to this post
Subject: Getting email OK, but not new notifications
Hi! I started a client in .NET that has to monitor the new email from your gmail account. I implemented the solution mentioned in this thread, and I am able to read the email when I call the server with the GmailNotify class (thanks Alex)
But I wanted to receive notifications from google when new email arrives. According to Google documentation (https://developers.google.com/talk/jep_extensions/gm…?hl=es) I have to wait for an Iq like this:
<iq type='set'
    from='romeo@gmail.com'
    to='romeo@gmail.com/orchard'
    id='mail-request-2'>
  <new-mail xmlns='google:mail:notify' />
</iq>
Well, the point is that this iq is never received :( Am I forgetting something? I overloaded the OnIq method and I never get the message. What am I doing wrong??
Thanks in advance
Avatar
Alex #6
Member since Feb 2003 · 4449 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
You have to tell the server first that you are interested at those notifications.
The documentation sais:

New Email Notifications

When the user's Gmail account receives new email, if a client has previously sent a new email query to the server, the server will send a notification to the client. This notification includes a <new-mail/> element qualified by the 'google:mail:notify' namespace, but it does not include any information about the email.


The documentation does not say exactly how the subscribe stanza must look. I assume that you have to send the stanza you posted above to the server. which replies with result then and starts sending the notifications.

Alex
Avatar
AlexR #7
Member since May 2013 · 2 posts
Group memberships: Members
Show profile · Link to this post
Thanks Alex. Luckily, I have found the solution. The Google documentation is correct: when you make a first mail petition, it subscribes you to the new mail notification. Unfortunately, there is one previous undocumented stanza you have to send. It is solved here:

http://productforums.google.com/forum/#!msg/chat/BmwnRsjQd…
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS