// Path: rx_gcm/src/main/java/rx_gcm/GcmReceiverData.java
// public interface GcmReceiverData {
//     /**
//      * @return return the new instance observable after applying doOnNext operator.
//      */
//     Observable<Message> onNotification(Observable<Message> oMessage);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/GcmReceiverUIBackground.java
// public interface GcmReceiverUIBackground {
//     void onNotification(Observable<Message> oMessage);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/GcmRefreshTokenReceiver.java
// public interface GcmRefreshTokenReceiver {
//     void onTokenReceive(Observable<TokenUpdate> oTokenUpdate);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/Message.java
// public class Message {
//     private final String from;
//     private final Bundle payload;
//     private final String target;
//     private final Application application;
// 
//     public Message(String from, Bundle payload, String target, Application application) {
//         this.from = from;
//         this.payload = payload;
//         this.target = target;
//         this.application = application;
//     }
// 
//     public String from() {
//         return from;
//     }
// 
//     @Nullable public String target() {
//         return target;
//     }
// 
//     public Bundle payload() {
//         return payload;
//     }
// 
//     public Application application() {
//         return application;
//     }
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/TokenUpdate.java
// public class TokenUpdate {
//     private final String token;
//     private final Application application;
// 
//     public TokenUpdate(String token, Application application) {
//         this.token = token;
//         this.application = application;
//     }
// 
//     public String getToken() {
//         return token;
//     }
// 
//     public Application getApplication() {
//         return application;
//     }
// }


import rx_gcm.GcmReceiverUIBackground;
import rx_gcm.GcmRefreshTokenReceiver;
import rx_gcm.Message;
import rx_gcm.TokenUpdate;
import victoralbertos.io.rx_gcm.R;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Actions;
import rx.schedulers.Schedulers;
import rx_gcm.GcmReceiverData;

    void onNotificationReceived(String from, Bundle payload) {
        Application application = activitiesLifecycle.getApplication();
        String target = payload != null ? payload.getString(RX_GCM_KEY_TARGET, null) : "";

        Observable<Message> oMessage = Observable.just(new Message(from, payload, target, application));

        String className = persistence.getClassNameGcmReceiver(activitiesLifecycle.getApplication());
        GcmReceiverData gcmReceiverData = getInstanceClassByName(className);

        gcmReceiverData.onNotification(oMessage)
                .doOnNext(new Action1<Message>() {
                    @Override public void call(Message message) {
                        if (activitiesLifecycle.isAppOnBackground()) {
                            notifyGcmReceiverBackgroundMessage(message);
                        } else {
                            notifyGcmReceiverForegroundMessage(message);
                        }
                    }
                })
                .subscribe(Actions.empty(), new Action1<Throwable>() {
                    @Override public void call(Throwable throwable) {
                        String message = "Error thrown from GcmReceiverData subscription. Cause exception: " + throwable.getMessage();
                        Log.e("RxGcm", message);
                    }
                });
    }

    private void notifyGcmReceiverBackgroundMessage(Message message) {
        String className = persistence.getClassNameGcmReceiverUIBackground(activitiesLifecycle.getApplication());
// Path: rx_gcm/src/main/java/rx_gcm/GcmReceiverData.java
// public interface GcmReceiverData {
//     /**
//      * @return return the new instance observable after applying doOnNext operator.
//      */
//     Observable<Message> onNotification(Observable<Message> oMessage);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/GcmReceiverUIBackground.java
// public interface GcmReceiverUIBackground {
//     void onNotification(Observable<Message> oMessage);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/GcmRefreshTokenReceiver.java
// public interface GcmRefreshTokenReceiver {
//     void onTokenReceive(Observable<TokenUpdate> oTokenUpdate);
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/Message.java
// public class Message {
//     private final String from;
//     private final Bundle payload;
//     private final String target;
//     private final Application application;
// 
//     public Message(String from, Bundle payload, String target, Application application) {
//         this.from = from;
//         this.payload = payload;
//         this.target = target;
//         this.application = application;
//     }
// 
//     public String from() {
//         return from;
//     }
// 
//     @Nullable public String target() {
//         return target;
//     }
// 
//     public Bundle payload() {
//         return payload;
//     }
// 
//     public Application application() {
//         return application;
//     }
// }
// 
// Path: rx_gcm/src/main/java/rx_gcm/TokenUpdate.java
// public class TokenUpdate {
//     private final String token;
//     private final Application application;
// 
//     public TokenUpdate(String token, Application application) {
//         this.token = token;
//         this.application = application;
//     }
// 
//     public String getToken() {
//         return token;
//     }
// 
//     public Application getApplication() {
//         return application;
//     }
// }


// Path: rx_gcm/src/main/java/rx_gcm/internal/RxGcm.java
import rx_gcm.GcmReceiverUIBackground;
import rx_gcm.GcmRefreshTokenReceiver;
import rx_gcm.Message;
import rx_gcm.TokenUpdate;
import victoralbertos.io.rx_gcm.R;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Actions;
import rx.schedulers.Schedulers;
import rx_gcm.GcmReceiverData;


    void onNotificationReceived(String from, Bundle payload) {
        Application application = activitiesLifecycle.getApplication();
        String target = payload != null ? payload.getString(RX_GCM_KEY_TARGET, null) : "";

        Observable<Message> oMessage = Observable.just(new Message(from, payload, target, application));

        String className = persistence.getClassNameGcmReceiver(activitiesLifecycle.getApplication());
        GcmReceiverData gcmReceiverData = getInstanceClassByName(className);

        gcmReceiverData.onNotification(oMessage)
                .doOnNext(new Action1<Message>() {
                    @Override public void call(Message message) {
                        if (activitiesLifecycle.isAppOnBackground()) {
                            notifyGcmReceiverBackgroundMessage(message);
                        } else {
                            notifyGcmReceiverForegroundMessage(message);
                        }
                    }
                })
                .subscribe(Actions.empty(), new Action1<Throwable>() {
                    @Override public void call(Throwable throwable) {
                        String message = "Error thrown from GcmReceiverData subscription. Cause exception: " + throwable.getMessage();
                        Log.e("RxGcm", message);
                    }
                });
    }

    private void notifyGcmReceiverBackgroundMessage(Message message) {
        String className = persistence.getClassNameGcmReceiverUIBackground(activitiesLifecycle.getApplication());
        final GcmReceiverUIBackground gcmReceiverUIBackground = getInstanceClassByName(className);