Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 31 | Rev 43 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 gabriel 1
package com.cesams.twogetskills.service;
2
import android.content.Intent;
28 gabriel 3
import android.graphics.drawable.Drawable;
4
import android.os.Build;
5
import android.text.TextUtils;
1 gabriel 6
import android.util.Log;
7
 
27 gabriel 8
import com.cesams.twogetskills.Configuration;
1 gabriel 9
import com.cesams.twogetskills.Constants;
28 gabriel 10
import com.cesams.twogetskills.dao.AppDatabase;
11
import com.cesams.twogetskills.dao.DatabaseHelper;
12
import com.cesams.twogetskills.dao.SyncDao;
13
import com.cesams.twogetskills.entity.Sync;
1 gabriel 14
import com.cesams.twogetskills.library.Functions;
27 gabriel 15
import com.cesams.twogetskills.library.Http;
28 gabriel 16
import com.cesams.twogetskills.library.UniqueID;
27 gabriel 17
import com.cesams.twogetskills.preference.Preference;
1 gabriel 18
import com.google.firebase.messaging.FirebaseMessagingService;
19
import com.google.firebase.messaging.RemoteMessage;
20
 
28 gabriel 21
import org.json.JSONException;
22
import org.json.JSONObject;
23
 
27 gabriel 24
import java.io.IOException;
28 gabriel 25
import java.util.List;
1 gabriel 26
 
27 gabriel 27
import okhttp3.Call;
28
import okhttp3.FormBody;
29
import okhttp3.OkHttpClient;
30
import okhttp3.Request;
31
import okhttp3.RequestBody;
32
import okhttp3.Response;
1 gabriel 33
 
27 gabriel 34
 
1 gabriel 35
public class FcmMessagingService extends FirebaseMessagingService {
36
 
37
 
38
    private static final String TAG = "C2GS - FcmMessageServ";
30 efrain 39
   // private AppDatabase mAppDatabase;
40
    //boolean isSyncDevice = false;
41
    //boolean isSyncToken = false;
42
    //Preference preference;
1 gabriel 43
 
27 gabriel 44
 
30 efrain 45
 
1 gabriel 46
    @Override
47
    public void onCreate() {
28 gabriel 48
 
30 efrain 49
        //preference = Preference.getInstance(getApplicationContext());
50
        //preference.load();
51
        //mAppDatabase = DatabaseHelper.getInstance(getApplicationContext()).getAppDatabase();
1 gabriel 52
        super.onCreate();
30 efrain 53
        Log.e("BUG Token",  "FcmMessagingService - onCreate");
1 gabriel 54
    }
55
 
56
    public void onMessageReceived(RemoteMessage remoteMessage) {
57
 
28 gabriel 58
 
1 gabriel 59
        Log.d("FcmMessagingService", "onMessageReceived");
60
 
61
        if (remoteMessage.getData().size() > 0) {
62
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
63
 
64
            if(remoteMessage.getData().containsKey("command")) {
65
                String command = remoteMessage.getData().get("command").trim();
66
 
67
                Intent intent = new Intent(Constants.BROADCAST_TYPE_COMMAND);
68
                intent.putExtra("command", command);
31 efrain 69
                getApplicationContext().sendBroadcast(intent);
1 gabriel 70
            }
71
        }
72
        if (remoteMessage.getNotification() != null) {
73
            String title = remoteMessage.getNotification().getTitle();
74
            String body = remoteMessage.getNotification().getBody();
75
 
76
 
39 gabriel 77
            String command = "", url="";
1 gabriel 78
            int new_capsules = 0;
79
            if (remoteMessage.getData().size() > 0) {
80
                Log.d(TAG, "Message data payload: " + remoteMessage.getData());
81
 
82
                if(remoteMessage.getData().containsKey("command")) {
83
                    command = remoteMessage.getData().get("command").trim();
84
                }
85
 
86
                if(remoteMessage.getData().containsKey("new_capsules")) {
87
                   new_capsules = Functions.Numero2Int(remoteMessage.getData().get("new_capsules"));
88
                }
39 gabriel 89
 
90
                if(remoteMessage.getData().containsKey("url")) {
91
                    url = remoteMessage.getData().get("url");
92
                }
1 gabriel 93
            }
94
 
95
            Log.d(TAG, "title = " + title);
96
            Log.d(TAG, "body = " + body);
97
            Log.d(TAG, "command = " + command);
98
            Log.d(TAG, "new_capsules = " + new_capsules);
99
 
100
            Intent intent = new Intent(Constants.BROADCAST_TYPE_NOTIFICATION);
101
            intent.putExtra("title", title);
102
            intent.putExtra("body", body);
103
            intent.putExtra("command", command);
8 gabriel 104
            intent.putExtra("new_capsules", new_capsules);
39 gabriel 105
            intent.putExtra("url",url);
1 gabriel 106
 
107
 
108
 
31 efrain 109
            getApplicationContext().sendBroadcast(intent);
1 gabriel 110
        }
111
    }
112
 
113
 
114
 
28 gabriel 115
 
1 gabriel 116
    public void onNewToken(String fcm_token)
117
    {
29 efrain 118
        Log.e("BUG Token", "Token 2 :  " + fcm_token);
119
        /*
27 gabriel 120
        Log.e("FcmMessagingService", "onNewToken token = " + fcm_token);
28 gabriel 121
 
122
        String uuid = UniqueID.id(getApplicationContext());
123
 
27 gabriel 124
        preference.setDeviceToken (fcm_token);
28 gabriel 125
        preference.setDeviceUuid(uuid);
27 gabriel 126
        preference.save();
1 gabriel 127
 
28 gabriel 128
        Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
129
        mAppDatabase.getSyncDao().insert(sync);
1 gabriel 130
 
28 gabriel 131
        sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, fcm_token);
132
        mAppDatabase.getSyncDao().insert(sync);
133
 
134
        syncTokenFMCTo2getSkillServer();
31 efrain 135
        */
29 efrain 136
 
31 efrain 137
        Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
138
        intent.putExtra("token", Functions.trimNull(fcm_token));
139
        getApplicationContext().sendBroadcast(intent);
28 gabriel 140
 
31 efrain 141
 
28 gabriel 142
    }
143
 
30 efrain 144
    /*
28 gabriel 145
    private void syncTokenFMCTo2getSkillServer(){
146
 
147
 
148
        SyncDao syncDao = mAppDatabase.getSyncDao();
149
        List<Sync> records = syncDao.selectBatch();
150
 
151
        if(records.size() > 0) {
152
            syncToServer(records);
27 gabriel 153
        }
28 gabriel 154
}
155
 
156
    public void syncToServer(List<Sync> records) {
157
 
158
        for (Sync record : records) {
159
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE && !isSyncDevice) {
160
 
161
 
162
                try {
163
                    Http http = new Http(this.getCacheDir());
164
                    OkHttpClient client = http.getHttpClient(false);
165
 
166
                    RequestBody formBody = new FormBody.Builder()
167
                            .add(Constants.POST_DEVICE_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
168
                            .add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
169
                            .add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
170
                            .add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
171
                            .add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
172
                            .add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
173
                            .add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
174
                            .add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
175
                            .build();
176
 
177
                    Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
178
                    Request request = new Request.Builder()
179
                            .url(Configuration.URL_DEVICE)
180
                            .post(formBody)
181
                            .build();
182
 
183
                    Call call = client.newCall(request);
184
                    call.enqueue(new okhttp3.Callback() {
185
                        public void onResponse(Call call, Response response)
186
                                throws IOException {
187
                           // isSyncDevice = false;
188
                            Log.d(TAG, "Response Device :  " + response.body().toString());
189
 
190
                            processResponseSyncToServer(response.body().string(), "device");
191
                        }
192
 
193
                        public void onFailure(Call call, IOException e) {
194
                            isSyncDevice = false;
195
                            Log.d(TAG, "Error :  " + e.getMessage());
196
                        }
197
                    });
198
                } catch (Exception e) {
199
                }
200
            }
201
            if(record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncDevice){
202
 
203
                //Sucede que intenta sincronizar primero el Token antes que el device.
204
                Log.e("Esta intentando","sync el token antes que device");
205
            }
206
 
207
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncToken && isSyncDevice) {
208
                isSyncToken = true;
209
                Log.d(TAG, "FCM");
210
                Log.e("Token a Sync", "" + record.getData());
211
 
212
                try {
213
                    Http http = new Http(this.getCacheDir());
214
                    OkHttpClient client = http.getHttpClient(false);
215
 
216
                    RequestBody formBody = new FormBody.Builder()
217
                            .add(Constants.POST_FCM_FIELD_DEVICE_UUID, preference.getDeviceUuid())
218
                            .add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
219
                            .add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
220
                            .build();
221
 
222
                    Log.d(TAG, "URL = " + Configuration.URL_FCM);
223
                    Request request = new Request.Builder()
224
                            .url(Configuration.URL_FCM)
225
                            .post(formBody)
226
                            .build();
227
 
228
                    Call call = client.newCall(request);
229
                    call.enqueue(new okhttp3.Callback() {
230
                        public void onResponse(Call call, Response response)
231
                                throws IOException {
232
                            isSyncToken = false;
233
                            Log.e("Send FCM Token", "Procesando respuesta..");
234
                            processResponseSyncToServer(response.body().string(), "fcmtoken");
235
                        }
236
 
237
                        public void onFailure(Call call, IOException e) {
238
                            Log.d(TAG, "Error :  " + e.getMessage());
239
                            isSyncToken = false;
240
                        }
241
                    });
242
                } catch (Exception e) {
243
                    e.printStackTrace();
244
 
245
                }
246
            }
247
        }
1 gabriel 248
    }
249
 
28 gabriel 250
    private void processResponseSyncToServer(String dataString, String origen)
251
    {
252
        boolean success = false;
253
        long sync_id = 0;
254
 
255
        Log.d(TAG, "processResponseServer = " + dataString);
256
        try {
257
            JSONObject objJSON = new JSONObject(dataString);
258
            success = objJSON.has("success") ? objJSON.getBoolean("success")  : false;
259
            if(success  && objJSON.has("data")) {
260
                JSONObject jsonObjectData = objJSON.getJSONObject("data");
261
 
262
 
263
                if(jsonObjectData.has("sync_id")) {
264
                    sync_id = jsonObjectData.getLong("sync_id");
265
                }
266
 
267
            }
268
            Log.d(TAG, "SyncID = " + sync_id);
269
            if(success && sync_id > 0) {
270
                Log.d(TAG, "DELETE SYNC RECORD : " + sync_id);
271
                mAppDatabase.getSyncDao().remove(sync_id);
272
 
273
                if(origen.equals("device"))
274
                {
275
                    Log.e("Sync","device exitoso, ahora sigo con TokenFCM");
276
                    isSyncDevice=true;
277
                    syncTokenFMCTo2getSkillServer();
278
                }
279
                if(origen.equals("fcmtoken")){
280
                    Log.e("Token", "termino de sync con exito");
281
                }
282
            }
283
        } catch (JSONException e) {
284
            e.printStackTrace();
285
        }
30 efrain 286
    }*/
28 gabriel 287
 
1 gabriel 288
}