Android Open Source - RealtimeMessaging-Android Gcm Ortc Intent Service






From Project

Back to project page RealtimeMessaging-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeMessaging-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ibt.ortc.extensibility;
//from w  w  w  .j  av a 2 s.c  o  m
import ibt.ortc.api.Ortc;
import ibt.ortc.plugins.IbtRealtimeSJ.OrtcMessage;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;


public class GcmOrtcIntentService extends Service {  
  int mStartMode;
  HashMap<Integer, WeakReference<OrtcClient>> ortcs;
  private int clientsCount;
  private JSONParser parser;
  
  @Override
    public void onCreate() {
        ortcs = new HashMap<Integer, WeakReference<OrtcClient>>();
        clientsCount = 0;
        parser = new JSONParser();
    }
    @SuppressWarnings("unchecked")
  @Override
    public int onStartCommand(Intent intent, int flags, int startId) { 
      //System.out.println("got start command");
      if(intent != null){
        Bundle extras = intent.getExtras();
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
            String messageType = gcm.getMessageType(intent);        
            if(extras != null){
              if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
                if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    String message = extras.getString("M");
                    String channel = extras.getString("C");
                    String appkey  = extras.getString("A");
                    Map<String, Object> payload = null;
                    
                    if(message == null || channel == null)
                      return mStartMode;
                    try {
                      String fstPass = JSONValue.toJSONString(message);
                      fstPass = fstPass.substring(1, fstPass.length()-1);
                      String sndPass = JSONValue.toJSONString(fstPass);
                      sndPass = sndPass.substring(1, sndPass.length()-1);
                      String messageForOrtc = String.format("a[\"{\\\"ch\\\":\\\"%s\\\",\\\"m\\\":\\\"%s\\\"}\"]", channel, sndPass);

                OrtcMessage ortcMessage = OrtcMessage.parseMessage(messageForOrtc);
                if(extras.containsKey("P")){
                  String strPayload = extras.getString("P");
                  //System.out.println("[GCM] Payload: " + strPayload);
                  try {                    
                  payload = (Map<String, Object>)parser.parse(strPayload);  
                } catch (ParseException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                }
                        } else {
                          //System.out.println("[GCM] Notification without payload");
                        }
                
                 List<Integer> clientsToRemove = new ArrayList<Integer>();
                 Boolean pushNotificationHandlerExecuted = false;
                 
                for(Map.Entry<Integer, WeakReference<OrtcClient>> entry : ortcs.entrySet()){
                  Integer clientId = entry.getKey();
                  OrtcClient oc = entry.getValue().get();
                  if(oc == null){
                    clientsToRemove.add(clientId);
                  } else {
                    if(oc.applicationKey.equals(appkey)){
                      ChannelSubscription subscription = oc.subscribedChannels.get(channel);
                      if(subscription!=null && ( Ortc.getOnPushNotification() == null || (oc.isConnected && !oc.isReconnecting))){
                        if(subscription.isWithNotification()){
                          String messId = ortcMessage.getMessageId();
                          if(messId!=null && oc.multiPartMessagesBuffer.containsKey(messId)){
                            continue;
                          }
                          oc.raiseOrtcEvent(EventEnum.OnReceived, ortcMessage.getMessageChannel(),
                                ortcMessage.getMessage(), messId,
                                ortcMessage.getMessagePart(), ortcMessage.getMessageTotalParts(), payload);
                        }
                      }else if(!pushNotificationHandlerExecuted && Ortc.getOnPushNotification() != null){
                        pushNotificationHandlerExecuted = true;
                        Ortc.getOnPushNotification().run(null, channel, message);                        
                      } 
                    }
                  }
                }
                for(Integer clientId : clientsToRemove){
                  ortcs.remove(clientId);
                }

              } catch (IOException e) {
                e.printStackTrace();            
              }
                  }
              }
            }            
      }
      return mStartMode;      
    }
  
    public class ServiceBinder extends Binder {
      GcmOrtcIntentService getService() {
        return GcmOrtcIntentService.this;
      }
    }
    private IBinder mBinder = new ServiceBinder();
    
  @Override
  public IBinder onBind(Intent arg0) {
    return mBinder;
  }
  
  @Override
  public void onDestroy(){    
  }
  
  public void setServiceOrtcClient(OrtcClient ortcClient){
    ortcs.put(clientsCount, new WeakReference<OrtcClient>(ortcClient));
    ortcClient.gcmServiceId = clientsCount;
    clientsCount++;
  }
  
  public void removeServiceOrtcClient(OrtcClient ortcClient){  
    ortcs.remove(ortcClient.gcmServiceId);
  }
}




Java Source Code List

ibt.ortc.api.ApplicationTest.java
ibt.ortc.api.Authentication.java
ibt.ortc.api.Balancer.java
ibt.ortc.api.ChannelPermissions.java
ibt.ortc.api.InvalidBalancerServerException.java
ibt.ortc.api.OnDisablePresence.java
ibt.ortc.api.OnEnablePresence.java
ibt.ortc.api.OnPresence.java
ibt.ortc.api.OnRestWebserviceResponse.java
ibt.ortc.api.OrtcAuthenticationNotAuthorizedException.java
ibt.ortc.api.Ortc.java
ibt.ortc.api.Pair.java
ibt.ortc.api.Presence.java
ibt.ortc.api.RestWebservice.java
ibt.ortc.api.SecureWebConnections.java
ibt.ortc.api.Strings.java
ibt.ortc.extensibility.ChannelSubscription.java
ibt.ortc.extensibility.CharEscaper.java
ibt.ortc.extensibility.ConnectionProtocol.java
ibt.ortc.extensibility.DispatchedMessages.java
ibt.ortc.extensibility.EventEnum.java
ibt.ortc.extensibility.GcmOrtcBroadcastReceiver.java
ibt.ortc.extensibility.GcmOrtcIntentService.java
ibt.ortc.extensibility.GcmRegistration.java
ibt.ortc.extensibility.HeartbeatSender.java
ibt.ortc.extensibility.OnConnected.java
ibt.ortc.extensibility.OnDisconnected.java
ibt.ortc.extensibility.OnException.java
ibt.ortc.extensibility.OnMessageWithPayload.java
ibt.ortc.extensibility.OnMessage.java
ibt.ortc.extensibility.OnReconnected.java
ibt.ortc.extensibility.OnReconnecting.java
ibt.ortc.extensibility.OnSubscribed.java
ibt.ortc.extensibility.OnUnsubscribed.java
ibt.ortc.extensibility.OrtcClient.java
ibt.ortc.extensibility.OrtcFactory.java
ibt.ortc.extensibility.exception.OrtcAlreadyConnectedException.java
ibt.ortc.extensibility.exception.OrtcDoesNotHavePermissionException.java
ibt.ortc.extensibility.exception.OrtcEmptyFieldException.java
ibt.ortc.extensibility.exception.OrtcGcmException.java
ibt.ortc.extensibility.exception.OrtcInvalidCharactersException.java
ibt.ortc.extensibility.exception.OrtcMaxLengthException.java
ibt.ortc.extensibility.exception.OrtcNotConnectedException.java
ibt.ortc.extensibility.exception.OrtcNotSubscribedException.java
ibt.ortc.extensibility.exception.OrtcSubscribedException.java
ibt.ortc.ortclib.ApplicationTest.java
ibt.ortc.ortclib.MainActivity.java
ibt.ortc.ortclib.SettingsActivity.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJClient.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJFactory.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcMessage.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcOperation.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcServerErrorException.java
ibt.ortc.plugins.websocket.WebSocketConnection.java
ibt.ortc.plugins.websocket.WebSocketEventHandler.java
ibt.ortc.plugins.websocket.WebSocketException.java
ibt.ortc.plugins.websocket.WebSocketHandshake.java
ibt.ortc.plugins.websocket.WebSocketMessage.java
ibt.ortc.plugins.websocket.WebSocketReceiver.java
ibt.ortc.plugins.websocket.WebSocketSender.java
ibt.ortc.plugins.websocket.WebSocket.java