Android Open Source - javocsoft-toolbox G C M Intent Service






From Project

Back to project page javocsoft-toolbox.

License

The source code is released under:

GNU General Public License

If you think the Android project javocsoft-toolbox 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

/*
 * Copyright (C) 2010-2014 - JavocSoft - Javier Gonzalez Serrano
 * http://javocsoft.es/proyectos/code-libs/android/javocsoft-toolbox-android-library
 * //from  ww  w  .  j a  v a2 s  . c  o m
 * This file is part of JavocSoft Android Toolbox library.
 *
 * JavocSoft Android Toolbox library is free software: you can redistribute it 
 * and/or modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation, either version 3 of the License, 
 * or (at your option) any later version.
 *
 * JavocSoft Android Toolbox library is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JavocSoft Android Toolbox library.  If not, see 
 * <http://www.gnu.org/licenses/>.
 * 
 */
package es.javocsoft.android.lib.toolbox.gcm.core;

import java.lang.reflect.Constructor;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import es.javocsoft.android.lib.toolbox.ToolBox;
import es.javocsoft.android.lib.toolbox.gcm.NotificationModule;

/**
 * Service responsible for handling GCM messages.
 * 
 * It will process and create a notification in the 
 * system status bar.
 * 
 * 
 * @author JavocSoft 2014
 * @since  2014
 * 
 */
public class GCMIntentService extends IntentService {
  
  
  public GCMIntentService() {
        super(NotificationModule.SENDER_ID);        
    }
  
  
  
  
  @Override
  protected void onHandleIntent(Intent intent) {    
    if(NotificationModule.LOG_ENABLE)
        Log.i(NotificationModule.TAG, "Received a new notification message");
      
    //1.- Generate the notification in the task bar.
    generateNotification(super.getApplicationContext().getApplicationContext(), intent);
        
    //2.- Run the runnable set for a new notification received event.        
    launchOnNewNotificationEventRunnable(intent);
        
        Log.i("SimpleWakefulReceiver", "Completed service @ " + SystemClock.elapsedRealtime());
        CustomGCMBroadcastReceiver.completeWakefulIntent(intent);
  }
    
    
    
    //AUXILIAR
    
  /*
   * Try to launch the configured runnable (if set) fro the
   * GCM OnNewNotification received event.
   *  
   * @param intent
   */
  private void launchOnNewNotificationEventRunnable(Intent intent) {
    //2.- Run the runnable set for a new notification received event.        
    if(NotificationModule.doWhenNotificationRunnable==null) {
      //...if null, we try to get the runnable from the saved configuration
      //in the SharedPreferences. This could happen if the app was closed because
      //the service does not know nothing when is called in this case.
      String notificationOnNotReceivedThreadToCall = (String)ToolBox.prefs_readPreference(super.getApplicationContext().getApplicationContext(), 
                              NotificationModule.GCM_PREF_NAME, 
                              NotificationModule.GCM_PREF_KEY_APP_NOTIFICATION_ONNOTRECEIVEDTHREAD_TO_CALL, String.class);
      Log.i(NotificationModule.TAG, "Runnable to run when a new notification is received: " + notificationOnNotReceivedThreadToCall);
      
      if(notificationOnNotReceivedThreadToCall!=null) {
        //Get a new instance from the class with reflection.
        try{        
          Class<?> c = Class.forName(notificationOnNotReceivedThreadToCall);
          Constructor<?> cons = c.getConstructor();
          Object onNewNotificationRunnableObject = cons.newInstance();
          NotificationModule.doWhenNotificationRunnable = (OnNewNotificationCallback)onNewNotificationRunnableObject;              
          
        }catch(Exception e) {
          if(NotificationModule.LOG_ENABLE)
            Log.e(NotificationModule.TAG,"Runnable for a new notification received could not be run. " +
                "Class could not be found/get (" + e.getMessage() + ").", e);
        }
      }else{
        if(NotificationModule.LOG_ENABLE)
          Log.i(NotificationModule.TAG,"No Runnable(NULL in SharedPreferences) specified for a new notification received event.");
      }
    }
    
    //Do something when new notification arrives.
        if(NotificationModule.doWhenNotificationRunnable!=null &&
       !NotificationModule.doWhenNotificationRunnable.isAlive()){
          //Set the intent extras
          NotificationModule.doWhenNotificationRunnable.setNotificationBundle(intent.getExtras());
          //Launch the thread
      Thread t = new Thread(NotificationModule.doWhenNotificationRunnable);      
      t.start();  
    }
  }
  
    /*
     * Creates the Android system notification to alert the user.
     *  
     * @param context
     * @param i
     */
    private void generateNotification(Context context, Intent i) {
        
      String message = (String)i.getExtras().get(NotificationModule.ANDROID_MESSAGE_KEY);
      
      //This service must be isolated from the possible status of the app to be able to
      //do its job even if the app is closed. This is the reason we get the required stuff
      //from the previously saved information in SharedPreferences.
      String notificationActToCall = (String)ToolBox.prefs_readPreference(context, NotificationModule.GCM_PREF_NAME, 
          NotificationModule.GCM_PREF_KEY_APP_NOTIFICATION_ACTIVITY_TO_CALL, String.class);     
      NotificationModule.NOTIFICATION_TITLE = (String)ToolBox.prefs_readPreference(context, NotificationModule.GCM_PREF_NAME, 
          NotificationModule.GCM_PREF_KEY_APP_NOTIFICATION_TITLE, String.class);
      NotificationModule.multipleNot = (Boolean)ToolBox.prefs_readPreference(context, NotificationModule.GCM_PREF_NAME, 
          NotificationModule.GCM_PREF_KEY_APP_NOTIFICATION_MULTIPLENOT, Boolean.class);
    NotificationModule.groupMultipleNotKey = (String)ToolBox.prefs_readPreference(context, NotificationModule.GCM_PREF_NAME, 
          NotificationModule.GCM_PREF_KEY_APP_NOTIFICATION_GROUPMULTIPLENOTKEY, String.class);
      
      try{
        //Get the Activity class to call when notification is opened.
      Class<?> clazz = Class.forName(notificationActToCall);      
      NotificationModule.NOTIFICATION_ACTIVITY_TO_CALL = clazz;
      
        ToolBox.notification_generate(context, 
            true, -1, 
            NotificationModule.multipleNot, NotificationModule.groupMultipleNotKey, 
            NotificationModule.NOTIFICATION_ACTION_KEY, 
            NotificationModule.NOTIFICATION_TITLE, message, 
            NotificationModule.NOTIFICATION_ACTIVITY_TO_CALL, i.getExtras(), 
            false);
        
        if(NotificationModule.LOG_ENABLE)
          Log.d(NotificationModule.TAG, "Notification created for the recieve PUSH message.");
        
      
    }catch(Exception e) {
      if(NotificationModule.LOG_ENABLE)
        Log.e(NotificationModule.TAG,"Notification could not be created for the received PUSH message. " +
            "Notification activity to open class could not be found (" + e.getMessage() + ").", e);
      
    }     
    }
    
    
    //AUXILIAR CLASSES
    
    /**
   * This class allows to do something with the received notification.
   * 
   * @author JavocSoft 2013.
   * @since 2013
   */
  public static abstract class OnNewNotificationCallback extends Thread implements Runnable {
    
    private Bundle notificationBundle;
    
    public OnNewNotificationCallback() {}
    
    @Override
    public void run() {
      pre_task();
      task();
      post_task();
    }
          
    protected abstract void pre_task();
    protected abstract void task();
    protected abstract void post_task();
    
    
    public void setNotificationBundle(Bundle notificationBundle) {
      this.notificationBundle = notificationBundle;
    }
    
    /**
     * Gets the notification extras.
     * 
     * @return
     */
    protected Bundle getExtras() {
      return notificationBundle;
    }
    
    /**
     * Gets the context.
     * 
     * @return
     */
    protected Context getContext(){
      return NotificationModule.APPLICATION_CONTEXT;
    }
  }
    
    /**
   * This class allows to do something when registration is done.
   * 
   * @author JavocSoft 2013.
   * @since 2013
   */
  public static abstract class OnRegistrationCallback extends Thread implements Runnable {
    
    
    protected OnRegistrationCallback() {}
    
    @Override
    public void run() {
      pre_task();
      task();
      post_task();
      
    }
          
    protected abstract void pre_task();
    protected abstract void task();
    protected abstract void post_task();
    
    
    /**
     * Gets the GCM registration token.
     * 
     * @return
     */
    protected String getGCMRegistrationToken() {
      if(getContext()!=null)
        return NotificationModule.getRegistrationId(getContext());
      else
        return null;
    }
    
    /**
     * Gets the context.
     * 
     * @return
     */
    protected Context getContext(){
      return NotificationModule.APPLICATION_CONTEXT;
    }
  }
  
  /**
   * This class allows to do something when unregistration is done.
   * 
   * @author JavocSoft 2013.
   * @since 2013
   */
  public static abstract class OnUnregistrationCallback extends Thread implements Runnable {
    
    
    protected OnUnregistrationCallback() {}
    
    @Override
    public void run() {
      pre_task();
      task();
      post_task();
    }
          
    protected abstract void pre_task();
    protected abstract void task();
    protected abstract void post_task();
      
    
    /**
     * Gets the context.
     * 
     * @return
     */
    protected Context getContext(){
      return NotificationModule.APPLICATION_CONTEXT;
    }
  }

  
}




Java Source Code List

es.javocsoft.android.lib.toolbox.ToolBox.java
es.javocsoft.android.lib.toolbox.ads.AdBase.java
es.javocsoft.android.lib.toolbox.ads.AdFragment.java
es.javocsoft.android.lib.toolbox.ads.AdInterstitial.java
es.javocsoft.android.lib.toolbox.ads.InterstitialAdsListener.java
es.javocsoft.android.lib.toolbox.analytics.CampaignInfo.java
es.javocsoft.android.lib.toolbox.analytics.CustomCampaignTrackingReceiver.java
es.javocsoft.android.lib.toolbox.encoding.Base64DecodingException.java
es.javocsoft.android.lib.toolbox.encoding.Base64.java
es.javocsoft.android.lib.toolbox.encoding.FileHelper.java
es.javocsoft.android.lib.toolbox.facebook.FacebookLoginFragment.java
es.javocsoft.android.lib.toolbox.facebook.FacebookShareFragment.java
es.javocsoft.android.lib.toolbox.facebook.FbTools.java
es.javocsoft.android.lib.toolbox.facebook.beans.AppRequestBean.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestDeleteSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestReceivedErrorActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnAppRequestSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLoginActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnLogoutActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareCancelledActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareFailActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.callback.OnShareSuccessActionCallback.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBException.java
es.javocsoft.android.lib.toolbox.facebook.exception.FBSessionException.java
es.javocsoft.android.lib.toolbox.gcm.EnvironmentType.java
es.javocsoft.android.lib.toolbox.gcm.NotificationModule.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomGCMBroadcastReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.CustomNotificationReceiver.java
es.javocsoft.android.lib.toolbox.gcm.core.GCMIntentService.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResponse.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMDeliveryResultItem.java
es.javocsoft.android.lib.toolbox.gcm.core.beans.GCMMessage.java
es.javocsoft.android.lib.toolbox.gcm.exception.GCMException.java
es.javocsoft.android.lib.toolbox.gcm.send.GCMHttpDelivery.java
es.javocsoft.android.lib.toolbox.io.IOUtils.java
es.javocsoft.android.lib.toolbox.io.Unzipper.java
es.javocsoft.android.lib.toolbox.media.MediaScannerNotifier.java
es.javocsoft.android.lib.toolbox.net.HttpOperations.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTInfoHelper.java
es.javocsoft.android.lib.toolbox.sms.cmt.CMTShortNumberInformation.java
es.javocsoft.android.lib.toolbox.sms.observer.SMSObserver.java