| 21478 |
rajender |
1 |
package com.saholic.profittill.BroadcastReceivers;
|
|
|
2 |
|
|
|
3 |
import android.content.BroadcastReceiver;
|
|
|
4 |
import android.content.Context;
|
|
|
5 |
import android.content.Intent;
|
|
|
6 |
import android.content.SharedPreferences;
|
|
|
7 |
import android.os.AsyncTask;
|
|
|
8 |
import android.telephony.TelephonyManager;
|
|
|
9 |
import android.util.Log;
|
|
|
10 |
|
|
|
11 |
import com.google.firebase.iid.FirebaseInstanceId;
|
|
|
12 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
13 |
import com.saholic.profittill.Utils.AnalyticsUtility;
|
|
|
14 |
import com.saholic.profittill.Utils.UtilityFunctions;
|
|
|
15 |
import com.saholic.profittill.Volley.Analytics;
|
|
|
16 |
import com.saholic.profittill.Volley.AnalyticsErrorResponse;
|
|
|
17 |
import com.saholic.profittill.Volley.AnalyticsJsonResponse;
|
|
|
18 |
|
|
|
19 |
import org.apache.http.HttpEntity;
|
|
|
20 |
import org.apache.http.HttpResponse;
|
|
|
21 |
import org.apache.http.NameValuePair;
|
|
|
22 |
import org.apache.http.client.HttpClient;
|
|
|
23 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
24 |
import org.apache.http.client.methods.HttpPost;
|
|
|
25 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
26 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
27 |
|
|
|
28 |
import java.util.ArrayList;
|
|
|
29 |
|
|
|
30 |
public class UpdateReceiver extends BroadcastReceiver {
|
|
|
31 |
String regId;
|
|
|
32 |
FirebaseInstanceId fcm;
|
|
|
33 |
Context c;
|
|
|
34 |
SharedPreferences.Editor apiDataEditor,userDataEditor;
|
|
|
35 |
SharedPreferences apiData,userData;
|
|
|
36 |
ArrayList<NameValuePair> nameValuePairsGcm;
|
|
|
37 |
public UpdateReceiver() {
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
@Override
|
|
|
41 |
public void onReceive(Context context, Intent intent) {
|
|
|
42 |
c=context;
|
|
|
43 |
apiData = context.getSharedPreferences("API_Data", Context.MODE_PRIVATE);
|
|
|
44 |
userData = context.getSharedPreferences("User_Data", Context.MODE_PRIVATE);
|
|
|
45 |
userDataEditor = userData.edit();
|
|
|
46 |
apiDataEditor = apiData.edit();
|
|
|
47 |
registerGCM();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public void registerGCM() {
|
|
|
51 |
|
|
|
52 |
// gcm = GoogleCloudMessaging.getInstance(c);
|
|
|
53 |
registerInBackground();
|
|
|
54 |
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
private void registerInBackground() {
|
|
|
58 |
new AsyncTask<Void, Void, String>() {
|
|
|
59 |
@Override
|
|
|
60 |
protected String doInBackground(Void... params) {
|
|
|
61 |
String msg = "";
|
|
|
62 |
if (fcm == null) {
|
|
|
63 |
regId = FirebaseInstanceId.getInstance().getToken();
|
|
|
64 |
msg = "Device registered";
|
|
|
65 |
if (userData.getString("fcm_token_sent", "false").equals("false") && (!userData.getString("id", "").equals(""))) {
|
|
|
66 |
regId = FirebaseInstanceId.getInstance().getToken();
|
|
|
67 |
Log.d("RegisterActivity", "registerInBackground - regId: "
|
|
|
68 |
+ regId);
|
|
|
69 |
msg = "Device registered";
|
|
|
70 |
new gcmPushData().execute(msg);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
}
|
|
|
74 |
return msg;
|
|
|
75 |
}
|
|
|
76 |
@Override
|
|
|
77 |
protected void onPostExecute(String msg) {
|
|
|
78 |
new gcmPushData().execute(msg);
|
|
|
79 |
}
|
|
|
80 |
}.execute(null, null, null);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
class gcmPushData extends AsyncTask<String, Integer, String> {
|
|
|
85 |
|
|
|
86 |
@Override
|
|
|
87 |
protected void onPreExecute() {
|
|
|
88 |
super.onPreExecute();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
@Override
|
|
|
92 |
protected String doInBackground(String... arg0) {
|
|
|
93 |
String id = null;
|
|
|
94 |
try {
|
|
|
95 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
96 |
HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url", "http://api.profittill.com/gcm_users/add"));
|
|
|
97 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
98 |
TelephonyManager telephonyManager = (TelephonyManager) c.getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
99 |
nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
|
|
|
100 |
nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
|
|
|
101 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
|
|
|
102 |
nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
|
|
|
103 |
nameValuePairsGcm.add(new BasicNameValuePair("notification_type", "fcm"));
|
|
|
104 |
nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(c)));
|
|
|
105 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
106 |
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
|
|
|
107 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
108 |
HttpEntity entity = response.getEntity();
|
|
|
109 |
int status = response.getStatusLine().getStatusCode();
|
|
|
110 |
if (status == 200) {
|
|
|
111 |
Log.d("ResponseCode GCM ", status + "");
|
|
|
112 |
userDataEditor.putString("fcm_token_sent", "true");
|
|
|
113 |
userDataEditor.commit();
|
|
|
114 |
} else {
|
|
|
115 |
Log.d("ResponseCode GCM ", status + "");
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
nameValuePairsGcm.clear();
|
|
|
119 |
Log.e("pass 1", "connection success ");
|
|
|
120 |
} catch (Exception e) {
|
|
|
121 |
}
|
|
|
122 |
return id;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
@Override
|
|
|
126 |
protected void onPostExecute(String result) {
|
|
|
127 |
super.onPostExecute(result);
|
|
|
128 |
if(!(userData.getString("id", "").isEmpty())) {
|
|
|
129 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
130 |
AnalyticsUtility.getAnalyticsRequest(c, userData.getString("id", ""), "Reciever", "Update Reciever", "Update Receiver called"),
|
|
|
131 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
|
|
132 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(c);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
}
|