Package net.yura.lobby.server

Source Code of net.yura.lobby.server.AndroidGCM

package net.yura.lobby.server;

import com.google.android.gcm.server.Constants;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import net.yura.lobby.database.Database;
import net.yura.lobby.database.User;

/**
* @author Yura Mamyrin
*/
public class AndroidGCM {
   
    public static final String myApiKey = "AIzaSyASA1k2nEQ6DOlw7BtNrMtnNYvTZZP6tSs";

    public static void send(GameLobby lobby, Database database, String username, String msg, Integer gameId) {
        Map<String, String> data = new HashMap();
        if (msg != null) {
            data.put("message", msg);
        }
        if (gameId != null) {
            data.put("gameId", String.valueOf(gameId));
        }
        send(lobby, database, username, data);
    }

    public static void send(GameLobby lobby, Database database, String username, Map<String, String> data) {
       
        boolean resignFromAllGames = false;
       
        try {
            database.startTransaction();
            User user = database.getUser(username);
            if (user==null) {
                throw new IllegalStateException("can not find user "+username);
            }
            String registrationId = user.getAndroidId();
           
            if (registrationId!=null) {
                Sender sender = new Sender(myApiKey);
                Message.Builder message = new Message.Builder();
                for (Map.Entry<String, String> entry : data.entrySet()) {
                    message.addData(entry.getKey(), entry.getValue());
                }
                Result result = sender.send(message.build(), registrationId, 5);

                if (result.getMessageId() != null) {
                    String canonicalRegId = result.getCanonicalRegistrationId();
                    if (canonicalRegId != null) {
                        // same device has more than on registration ID: update database
                        user.setAndroidId(canonicalRegId);
                        database.saveUser(user);
                    }
                }
                else {
                    String error = result.getErrorCodeName();
                    if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                        // application has been removed from device - unregister database
                        user.setAndroidId(null);
                        database.saveUser(user);
                       
                        resignFromAllGames = true;
                    }
                }
            }
            else {
          LobbyServer.logger.info("no GCM id for user "+username);
            }
        }
        catch (Exception ex) {
            LobbyServer.logger.log(Level.WARNING, "error GCM to: "+username+" data: "+data, ex);
        }
        finally {
            database.endTransaction();
        }
       
        if (resignFromAllGames) {
            lobby.resignFromAllGames( username );
        }
       
    }
   
}
TOP

Related Classes of net.yura.lobby.server.AndroidGCM

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.