Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 44 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

package com.cesams.twogetskills.service;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import com.cesams.twogetskills.Configuration;
import com.cesams.twogetskills.Constants;
import com.cesams.twogetskills.dao.AppDatabase;
import com.cesams.twogetskills.dao.DatabaseHelper;
import com.cesams.twogetskills.dao.SyncDao;
import com.cesams.twogetskills.entity.Sync;
import com.cesams.twogetskills.library.Functions;
import com.cesams.twogetskills.library.Http;
import com.cesams.twogetskills.library.UniqueID;
import com.cesams.twogetskills.preference.Preference;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class FcmMessagingService extends FirebaseMessagingService {


    private static final String TAG = "C2GS - FcmMessageServ";


    @Override
    public void onCreate() {

        super.onCreate();
        Log.e("BUG Token",  "FcmMessagingService - onCreate");
    }

    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.d("FcmMessagingService", "onMessageReceived");

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            if(remoteMessage.getData().containsKey("command")) {
                String command = remoteMessage.getData().get("command").trim();

                Intent intent = new Intent(Constants.BROADCAST_TYPE_COMMAND);
                intent.putExtra("command", command);
                getApplicationContext().sendBroadcast(intent);
            }
        }
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();


            String command = "", url="";
            int new_capsules = 0;
            if (remoteMessage.getData().size() > 0) {
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());

                if(remoteMessage.getData().containsKey("command")) {
                    command = remoteMessage.getData().get("command").trim();
                }

                if(remoteMessage.getData().containsKey("new_capsules")) {
                   new_capsules = Functions.Numero2Int(remoteMessage.getData().get("new_capsules"));
                }

                if(remoteMessage.getData().containsKey("url")) {
                    url = remoteMessage.getData().get("url");
                }
            }

            Log.d(TAG, "title = " + title);
            Log.d(TAG, "body = " + body);
            Log.d(TAG, "command = " + command);
            Log.d(TAG, "new_capsules = " + new_capsules);

            Intent intent = new Intent(Constants.BROADCAST_TYPE_NOTIFICATION);
            intent.putExtra("title", title);
            intent.putExtra("body", body);
            intent.putExtra("command", command);
            intent.putExtra("new_capsules", new_capsules);
            intent.putExtra("url",url);





            if(command.equals("content-refresh")){
                intent.putExtra("content_refresh",true);
            } else {
                intent.putExtra("content_refresh",false);
            }

            getApplicationContext().sendBroadcast(intent);
        }
    }




    public void onNewToken(String fcm_token)
    {
        Log.e("BUG Token", "Token 2 :  " + fcm_token);

        Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
        intent.putExtra("token", Functions.trimNull(fcm_token));
        getApplicationContext().sendBroadcast(intent);


    }

}