| 21478 |
rajender |
1 |
package com.saholic.profittill;
|
|
|
2 |
|
|
|
3 |
import android.content.Context;
|
|
|
4 |
import android.content.SharedPreferences;
|
|
|
5 |
import android.telephony.TelephonyManager;
|
|
|
6 |
import android.util.Log;
|
|
|
7 |
|
|
|
8 |
import com.google.firebase.iid.FirebaseInstanceId;
|
|
|
9 |
import com.google.firebase.iid.FirebaseInstanceIdService;
|
|
|
10 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
11 |
import com.saholic.profittill.Utils.UtilityFunctions;
|
|
|
12 |
|
|
|
13 |
import org.apache.http.HttpEntity;
|
|
|
14 |
import org.apache.http.HttpResponse;
|
|
|
15 |
import org.apache.http.NameValuePair;
|
|
|
16 |
import org.apache.http.client.HttpClient;
|
|
|
17 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
18 |
import org.apache.http.client.methods.HttpPost;
|
|
|
19 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
20 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
21 |
|
|
|
22 |
import java.io.IOException;
|
|
|
23 |
import java.util.ArrayList;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Created by rajender on 5/4/17.
|
|
|
27 |
*/
|
|
|
28 |
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
|
|
|
29 |
private static final String TAG = "MyFirebaseIIDService";
|
|
|
30 |
SharedPreferences apiData;
|
|
|
31 |
SharedPreferences userData;
|
|
|
32 |
SharedPreferences.Editor userDataEditor;
|
|
|
33 |
SharedPreferences.Editor apiSettingsEditor;
|
|
|
34 |
ArrayList<NameValuePair> nameValuePairsGcm;
|
|
|
35 |
|
|
|
36 |
@Override
|
|
|
37 |
public void onTokenRefresh() {
|
|
|
38 |
apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
|
|
|
39 |
userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
|
|
|
40 |
userDataEditor = userData.edit();
|
|
|
41 |
apiSettingsEditor = apiData.edit();
|
|
|
42 |
//Getting registration token
|
|
|
43 |
|
|
|
44 |
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
|
|
|
45 |
userDataEditor.putString("fcm_token_sent", "false");
|
|
|
46 |
userDataEditor.commit();
|
|
|
47 |
|
|
|
48 |
//Displaying token on logcat
|
|
|
49 |
Log.d(TAG, "Refreshed token: " + refreshedToken);
|
|
|
50 |
try {
|
|
|
51 |
if(userData.getString("id", "").equals("")){
|
|
|
52 |
Log.d(TAG, "Refreshed token: " + refreshedToken);
|
|
|
53 |
}
|
|
|
54 |
else {
|
|
|
55 |
sendRegistrationToServer(refreshedToken);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
catch (IOException e) {
|
|
|
59 |
e.printStackTrace();
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
private void sendRegistrationToServer(String token) throws IOException {
|
|
|
65 |
try{
|
|
|
66 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
67 |
HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url",null));
|
|
|
68 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
69 |
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
70 |
nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid",token));
|
|
|
71 |
nameValuePairsGcm.add(new BasicNameValuePair("imeinumber",telephonyManager.getDeviceId()));
|
|
|
72 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id",userData.getString("id", null)));
|
|
|
73 |
nameValuePairsGcm.add(new BasicNameValuePair("device_message","Device registered"));
|
|
|
74 |
nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
|
|
|
75 |
nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(MyFirebaseInstanceIDService.this)));
|
|
|
76 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
77 |
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
|
|
|
78 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
79 |
HttpEntity entity = response.getEntity();
|
|
|
80 |
int status = response.getStatusLine().getStatusCode();
|
|
|
81 |
|
|
|
82 |
if(status == 200){
|
|
|
83 |
userDataEditor.putString("fcm_token_sent", "true");
|
|
|
84 |
userDataEditor.commit();
|
|
|
85 |
Log.d("ResponseCode GCM ",status+"");
|
|
|
86 |
} else {
|
|
|
87 |
Log.d("ResponseCode GCM ",status+"");
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
nameValuePairsGcm.clear();
|
|
|
91 |
Log.e("pass 1", "connection success ");
|
|
|
92 |
}
|
|
|
93 |
catch (Exception e) {
|
|
|
94 |
Log.e("Fail 1", e.toString());
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
}
|