Ramesh Sangili
Ramesh Sangili

Reputation: 1631

AppEngine User Service Email Address returns null always

I am trying to build a job site using App Engine using GCM. Where people can adverstise and others will be notified. When some one post a message, I want append their email id along with the message to make sure that message is appropriate and it doesn't have any harm, so it might be easier to find out that person incase if someone tries to play around with that. I am forcing the user to login to gmail account, before they do anything.

I was trying to use UserService to retrieve the email Id and name, but always giving me null. Not sure where I am doing mistake.

@ApiMethod(name = "sendMessage")
  public void sendMessage(@Named("message") String message)
      throws IOException {
    Sender sender = new Sender(API_KEY);
    // create a MessageData entity with a timestamp of when it was
    // received, and persist it
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    MessageData messageObj = new MessageData();
    if(user != null)
       messageObj.setMessage(message + " User : " + user.getUserId() + "Email " + user.getEmail());
    else 
       messageObj.setMessage(message);

Any help is greatly appreciated!

Upvotes: 0

Views: 306

Answers (1)

Robert Parker
Robert Parker

Reputation: 605

UserService.getCurrentUser() will return the a User object if the user is logged in, and null otherwise.

You can call UserService.isUserLoggedIn() to test if the user is logged in.

https://developers.google.com/appengine/docs/java/javadoc/com/google/appengine/api/users/UserService#getCurrentUser()

Upvotes: 1

Related Questions