| 1 |
gabriel |
1 |
package com.cesams.twogetskills.service;
|
|
|
2 |
import android.content.Intent;
|
|
|
3 |
import android.util.Log;
|
|
|
4 |
|
| 27 |
gabriel |
5 |
import com.cesams.twogetskills.Configuration;
|
| 1 |
gabriel |
6 |
import com.cesams.twogetskills.Constants;
|
|
|
7 |
import com.cesams.twogetskills.library.Functions;
|
| 27 |
gabriel |
8 |
import com.cesams.twogetskills.library.Http;
|
|
|
9 |
import com.cesams.twogetskills.preference.Preference;
|
| 1 |
gabriel |
10 |
import com.google.firebase.messaging.FirebaseMessagingService;
|
|
|
11 |
import com.google.firebase.messaging.RemoteMessage;
|
|
|
12 |
|
| 27 |
gabriel |
13 |
import java.io.IOException;
|
| 1 |
gabriel |
14 |
|
| 27 |
gabriel |
15 |
import okhttp3.Call;
|
|
|
16 |
import okhttp3.FormBody;
|
|
|
17 |
import okhttp3.OkHttpClient;
|
|
|
18 |
import okhttp3.Request;
|
|
|
19 |
import okhttp3.RequestBody;
|
|
|
20 |
import okhttp3.Response;
|
| 1 |
gabriel |
21 |
|
| 27 |
gabriel |
22 |
|
| 1 |
gabriel |
23 |
public class FcmMessagingService extends FirebaseMessagingService {
|
|
|
24 |
|
|
|
25 |
|
|
|
26 |
private static final String TAG = "C2GS - FcmMessageServ";
|
|
|
27 |
|
| 27 |
gabriel |
28 |
|
| 1 |
gabriel |
29 |
@Override
|
|
|
30 |
public void onCreate() {
|
|
|
31 |
super.onCreate();
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
public void onMessageReceived(RemoteMessage remoteMessage) {
|
|
|
35 |
|
|
|
36 |
Log.d("FcmMessagingService", "onMessageReceived");
|
|
|
37 |
|
|
|
38 |
if (remoteMessage.getData().size() > 0) {
|
|
|
39 |
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
40 |
|
|
|
41 |
if(remoteMessage.getData().containsKey("command")) {
|
|
|
42 |
String command = remoteMessage.getData().get("command").trim();
|
|
|
43 |
|
|
|
44 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_COMMAND);
|
|
|
45 |
intent.putExtra("command", command);
|
|
|
46 |
this.sendBroadcast(intent);
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
if (remoteMessage.getNotification() != null) {
|
|
|
50 |
String title = remoteMessage.getNotification().getTitle();
|
|
|
51 |
String body = remoteMessage.getNotification().getBody();
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
String command = "";
|
|
|
55 |
int new_capsules = 0;
|
|
|
56 |
if (remoteMessage.getData().size() > 0) {
|
|
|
57 |
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
|
|
|
58 |
|
|
|
59 |
if(remoteMessage.getData().containsKey("command")) {
|
|
|
60 |
command = remoteMessage.getData().get("command").trim();
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if(remoteMessage.getData().containsKey("new_capsules")) {
|
|
|
64 |
new_capsules = Functions.Numero2Int(remoteMessage.getData().get("new_capsules"));
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
Log.d(TAG, "title = " + title);
|
|
|
69 |
Log.d(TAG, "body = " + body);
|
|
|
70 |
Log.d(TAG, "command = " + command);
|
|
|
71 |
Log.d(TAG, "new_capsules = " + new_capsules);
|
|
|
72 |
|
|
|
73 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_NOTIFICATION);
|
|
|
74 |
intent.putExtra("title", title);
|
|
|
75 |
intent.putExtra("body", body);
|
|
|
76 |
intent.putExtra("command", command);
|
| 8 |
gabriel |
77 |
intent.putExtra("new_capsules", new_capsules);
|
| 1 |
gabriel |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
this.sendBroadcast(intent);
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
public void onNewToken(String fcm_token)
|
|
|
88 |
{
|
| 27 |
gabriel |
89 |
Log.e("FcmMessagingService", "onNewToken token = " + fcm_token);
|
|
|
90 |
Preference preference = Preference.getInstance(getApplicationContext());
|
|
|
91 |
preference.load();
|
|
|
92 |
preference.setDeviceToken (fcm_token);
|
|
|
93 |
preference.save();
|
| 1 |
gabriel |
94 |
|
| 27 |
gabriel |
95 |
/*
|
| 1 |
gabriel |
96 |
|
| 27 |
gabriel |
97 |
try{
|
|
|
98 |
Intent intent = new Intent(Constants.BROADCAST_TYPE_TOKEN);
|
|
|
99 |
intent.putExtra("token", Functions.trimNull(fcm_token));
|
|
|
100 |
//intent.putExtra("token", fcm_token);
|
|
|
101 |
getApplicationContext().sendBroadcast(intent);
|
|
|
102 |
} catch (Exception e) {
|
|
|
103 |
Log.e("Error",""+e);
|
|
|
104 |
}
|
|
|
105 |
*/
|
| 1 |
gabriel |
106 |
}
|
|
|
107 |
|
|
|
108 |
}
|