Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

Rev 29 | Rev 31 | 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);
28 gabriel 69
                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
 
77
            String command = "";
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
                }
89
            }
90
 
91
            Log.d(TAG, "title = " + title);
92
            Log.d(TAG, "body = " + body);
93
            Log.d(TAG, "command = " + command);
94
            Log.d(TAG, "new_capsules = " + new_capsules);
95
 
96
            Intent intent = new Intent(Constants.BROADCAST_TYPE_NOTIFICATION);
97
            intent.putExtra("title", title);
98
            intent.putExtra("body", body);
99
            intent.putExtra("command", command);
8 gabriel 100
            intent.putExtra("new_capsules", new_capsules);
1 gabriel 101
 
102
 
103
 
28 gabriel 104
            sendBroadcast(intent);
1 gabriel 105
        }
106
    }
107
 
108
 
109
 
28 gabriel 110
 
1 gabriel 111
    public void onNewToken(String fcm_token)
112
    {
29 efrain 113
        Log.e("BUG Token", "Token 2 :  " + fcm_token);
114
        /*
27 gabriel 115
        Log.e("FcmMessagingService", "onNewToken token = " + fcm_token);
28 gabriel 116
 
117
        String uuid = UniqueID.id(getApplicationContext());
118
 
27 gabriel 119
        preference.setDeviceToken (fcm_token);
28 gabriel 120
        preference.setDeviceUuid(uuid);
27 gabriel 121
        preference.save();
1 gabriel 122
 
28 gabriel 123
        Sync sync = new Sync(Constants.SYNC_ADAPTER_TYPE_DEVICE, uuid);
124
        mAppDatabase.getSyncDao().insert(sync);
1 gabriel 125
 
28 gabriel 126
        sync = new Sync(Constants.SYNC_ADAPTER_TYPE_FCM, fcm_token);
127
        mAppDatabase.getSyncDao().insert(sync);
128
 
129
        syncTokenFMCTo2getSkillServer();
29 efrain 130
 
27 gabriel 131
            Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
132
            intent.putExtra("token", Functions.trimNull(fcm_token));
28 gabriel 133
            sendBroadcast(intent);
134
 */
135
 
136
    }
137
 
30 efrain 138
    /*
28 gabriel 139
    private void syncTokenFMCTo2getSkillServer(){
140
 
141
 
142
        SyncDao syncDao = mAppDatabase.getSyncDao();
143
        List<Sync> records = syncDao.selectBatch();
144
 
145
        if(records.size() > 0) {
146
            syncToServer(records);
27 gabriel 147
        }
28 gabriel 148
}
149
 
150
    public void syncToServer(List<Sync> records) {
151
 
152
        for (Sync record : records) {
153
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_DEVICE && !isSyncDevice) {
154
 
155
 
156
                try {
157
                    Http http = new Http(this.getCacheDir());
158
                    OkHttpClient client = http.getHttpClient(false);
159
 
160
                    RequestBody formBody = new FormBody.Builder()
161
                            .add(Constants.POST_DEVICE_FIELD_APPLICATION_ID, String.valueOf(Configuration.APPLICATION_ID))
162
                            .add(Constants.POST_DEVICE_FIELD_DEVICE_UUID, preference.getDeviceUuid())
163
                            .add(Constants.POST_DEVICE_FIELD_MANUFACTURER, Build.MANUFACTURER)
164
                            .add(Constants.POST_DEVICE_FIELD_BRAND, Build.BRAND)
165
                            .add(Constants.POST_DEVICE_FIELD_VERSION, Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName())
166
                            .add(Constants.POST_DEVICE_FIELD_MODEL, Build.MODEL)
167
                            .add(Constants.POST_DEVICE_FIELD_PLATFORM, "android")
168
                            .add(Constants.POST_DEVICE_FIELD_SYNC_ID, String.valueOf(record.getId()))
169
                            .build();
170
 
171
                    Log.d(TAG, "URL = " + Configuration.URL_DEVICE);
172
                    Request request = new Request.Builder()
173
                            .url(Configuration.URL_DEVICE)
174
                            .post(formBody)
175
                            .build();
176
 
177
                    Call call = client.newCall(request);
178
                    call.enqueue(new okhttp3.Callback() {
179
                        public void onResponse(Call call, Response response)
180
                                throws IOException {
181
                           // isSyncDevice = false;
182
                            Log.d(TAG, "Response Device :  " + response.body().toString());
183
 
184
                            processResponseSyncToServer(response.body().string(), "device");
185
                        }
186
 
187
                        public void onFailure(Call call, IOException e) {
188
                            isSyncDevice = false;
189
                            Log.d(TAG, "Error :  " + e.getMessage());
190
                        }
191
                    });
192
                } catch (Exception e) {
193
                }
194
            }
195
            if(record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncDevice){
196
 
197
                //Sucede que intenta sincronizar primero el Token antes que el device.
198
                Log.e("Esta intentando","sync el token antes que device");
199
            }
200
 
201
            if (record.getType() == Constants.SYNC_ADAPTER_TYPE_FCM && !isSyncToken && isSyncDevice) {
202
                isSyncToken = true;
203
                Log.d(TAG, "FCM");
204
                Log.e("Token a Sync", "" + record.getData());
205
 
206
                try {
207
                    Http http = new Http(this.getCacheDir());
208
                    OkHttpClient client = http.getHttpClient(false);
209
 
210
                    RequestBody formBody = new FormBody.Builder()
211
                            .add(Constants.POST_FCM_FIELD_DEVICE_UUID, preference.getDeviceUuid())
212
                            .add(Constants.POST_FCM_FIELD_TOKEN, record.getData())
213
                            .add(Constants.POST_FCM_FIELD_SYNC_ID, String.valueOf(record.getId()))
214
                            .build();
215
 
216
                    Log.d(TAG, "URL = " + Configuration.URL_FCM);
217
                    Request request = new Request.Builder()
218
                            .url(Configuration.URL_FCM)
219
                            .post(formBody)
220
                            .build();
221
 
222
                    Call call = client.newCall(request);
223
                    call.enqueue(new okhttp3.Callback() {
224
                        public void onResponse(Call call, Response response)
225
                                throws IOException {
226
                            isSyncToken = false;
227
                            Log.e("Send FCM Token", "Procesando respuesta..");
228
                            processResponseSyncToServer(response.body().string(), "fcmtoken");
229
                        }
230
 
231
                        public void onFailure(Call call, IOException e) {
232
                            Log.d(TAG, "Error :  " + e.getMessage());
233
                            isSyncToken = false;
234
                        }
235
                    });
236
                } catch (Exception e) {
237
                    e.printStackTrace();
238
 
239
                }
240
            }
241
        }
1 gabriel 242
    }
243
 
28 gabriel 244
    private void processResponseSyncToServer(String dataString, String origen)
245
    {
246
        boolean success = false;
247
        long sync_id = 0;
248
 
249
        Log.d(TAG, "processResponseServer = " + dataString);
250
        try {
251
            JSONObject objJSON = new JSONObject(dataString);
252
            success = objJSON.has("success") ? objJSON.getBoolean("success")  : false;
253
            if(success  && objJSON.has("data")) {
254
                JSONObject jsonObjectData = objJSON.getJSONObject("data");
255
 
256
 
257
                if(jsonObjectData.has("sync_id")) {
258
                    sync_id = jsonObjectData.getLong("sync_id");
259
                }
260
 
261
            }
262
            Log.d(TAG, "SyncID = " + sync_id);
263
            if(success && sync_id > 0) {
264
                Log.d(TAG, "DELETE SYNC RECORD : " + sync_id);
265
                mAppDatabase.getSyncDao().remove(sync_id);
266
 
267
                if(origen.equals("device"))
268
                {
269
                    Log.e("Sync","device exitoso, ahora sigo con TokenFCM");
270
                    isSyncDevice=true;
271
                    syncTokenFMCTo2getSkillServer();
272
                }
273
                if(origen.equals("fcmtoken")){
274
                    Log.e("Token", "termino de sync con exito");
275
                }
276
            }
277
        } catch (JSONException e) {
278
            e.printStackTrace();
279
        }
30 efrain 280
    }*/
28 gabriel 281
 
1 gabriel 282
}