| 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";
|
|
|
39 |
|
| 27 |
gabriel |
40 |
|
| 1 |
gabriel |
41 |
@Override
|
|
|
42 |
public void onCreate() {
|
| 28 |
gabriel |
43 |
|
| 1 |
gabriel |
44 |
super.onCreate();
|
| 30 |
efrain |
45 |
Log.e("BUG Token", "FcmMessagingService - onCreate");
|
| 1 |
gabriel |
46 |
}
|
|
|
47 |
|
|
|
48 |
public void onMessageReceived(RemoteMessage remoteMessage) {
|
|
|
49 |
|
| 28 |
gabriel |
50 |
|
| 1 |
gabriel |
51 |
Log.d("FcmMessagingService", "onMessageReceived");
|
|
|
52 |
|
|
|
53 |
if (remoteMessage.getData().size() > 0) {
|
|
|
54 |
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
55 |
|
|
|
56 |
if(remoteMessage.getData().containsKey("command")) {
|
|
|
57 |
String command = remoteMessage.getData().get("command").trim();
|
|
|
58 |
|
|
|
59 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_COMMAND);
|
|
|
60 |
intent.putExtra("command", command);
|
| 31 |
efrain |
61 |
getApplicationContext().sendBroadcast(intent);
|
| 1 |
gabriel |
62 |
}
|
|
|
63 |
}
|
|
|
64 |
if (remoteMessage.getNotification() != null) {
|
|
|
65 |
String title = remoteMessage.getNotification().getTitle();
|
|
|
66 |
String body = remoteMessage.getNotification().getBody();
|
|
|
67 |
|
|
|
68 |
|
| 39 |
gabriel |
69 |
String command = "", url="";
|
| 1 |
gabriel |
70 |
int new_capsules = 0;
|
|
|
71 |
if (remoteMessage.getData().size() > 0) {
|
|
|
72 |
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
73 |
|
|
|
74 |
if(remoteMessage.getData().containsKey("command")) {
|
|
|
75 |
command = remoteMessage.getData().get("command").trim();
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if(remoteMessage.getData().containsKey("new_capsules")) {
|
|
|
79 |
new_capsules = Functions.Numero2Int(remoteMessage.getData().get("new_capsules"));
|
|
|
80 |
}
|
| 39 |
gabriel |
81 |
|
|
|
82 |
if(remoteMessage.getData().containsKey("url")) {
|
|
|
83 |
url = remoteMessage.getData().get("url");
|
|
|
84 |
}
|
| 1 |
gabriel |
85 |
}
|
|
|
86 |
|
|
|
87 |
Log.d(TAG, "title = " + title);
|
|
|
88 |
Log.d(TAG, "body = " + body);
|
|
|
89 |
Log.d(TAG, "command = " + command);
|
|
|
90 |
Log.d(TAG, "new_capsules = " + new_capsules);
|
|
|
91 |
|
|
|
92 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_NOTIFICATION);
|
|
|
93 |
intent.putExtra("title", title);
|
|
|
94 |
intent.putExtra("body", body);
|
|
|
95 |
intent.putExtra("command", command);
|
| 8 |
gabriel |
96 |
intent.putExtra("new_capsules", new_capsules);
|
| 39 |
gabriel |
97 |
intent.putExtra("url",url);
|
| 1 |
gabriel |
98 |
|
|
|
99 |
|
| 44 |
efrain |
100 |
|
|
|
101 |
|
|
|
102 |
|
| 43 |
gabriel |
103 |
if(command.equals("content-refresh")){
|
| 44 |
efrain |
104 |
intent.putExtra("content_refresh",true);
|
|
|
105 |
} else {
|
|
|
106 |
intent.putExtra("content_refresh",false);
|
| 43 |
gabriel |
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);
|
| 28 |
gabriel |
119 |
|
| 31 |
efrain |
120 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
|
|
|
121 |
intent.putExtra("token", Functions.trimNull(fcm_token));
|
|
|
122 |
getApplicationContext().sendBroadcast(intent);
|
| 28 |
gabriel |
123 |
|
| 31 |
efrain |
124 |
|
| 28 |
gabriel |
125 |
}
|
|
|
126 |
|
| 1 |
gabriel |
127 |
}
|