Proyectos de Subversion Android Microlearning - Nuevo Interface

Rev

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