Subversion Repositories SmartDukaan

Rev

Rev 21167 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21167 Rev 21214
Line 1... Line 1...
1
package com.saholic.profittill;
1
package com.saholic.profittill;
2
 
2
 
-
 
3
import android.content.Context;
-
 
4
import android.content.SharedPreferences;
-
 
5
import android.telephony.TelephonyManager;
3
import android.util.Log;
6
import android.util.Log;
4
 
7
 
5
import com.google.firebase.iid.FirebaseInstanceId;
8
import com.google.firebase.iid.FirebaseInstanceId;
6
import com.google.firebase.iid.FirebaseInstanceIdService;
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;
7
 
24
 
8
/**
25
/**
9
 * Created by rajender on 5/4/17.
26
 * Created by rajender on 5/4/17.
10
 */
27
 */
11
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
28
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService{
12
        private static final String TAG = "MyFirebaseIIDService";
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;
13
 
35
 
14
        @Override
36
        @Override
15
        public void onTokenRefresh() {
37
        public void onTokenRefresh() {
16
 
-
 
-
 
38
            apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
-
 
39
            userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
-
 
40
            userDataEditor = userData.edit();
-
 
41
            apiSettingsEditor = apiData.edit();
17
            //Getting registration token
42
            //Getting registration token
-
 
43
 
18
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
44
            String refreshedToken = FirebaseInstanceId.getInstance().getToken();
-
 
45
            userDataEditor.putString("fcm_token_sent", "false");
-
 
46
            userDataEditor.commit();
19
 
47
 
20
            //Displaying token on logcat
48
            //Displaying token on logcat
21
            Log.d(TAG, "Refreshed token: " + refreshedToken);
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 {
22
            //   sendRegistrationToServer(refreshedToken);
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
            }
23
 
89
 
-
 
90
            nameValuePairsGcm.clear();
-
 
91
            Log.e("pass 1", "connection success ");
24
        }
92
        }
-
 
93
            catch (Exception e) {
-
 
94
        Log.e("Fail 1", e.toString());
25
 
95
 
-
 
96
    }
-
 
97
}
26
 
98
 
27
        private void sendRegistrationToServer(String token) {
-
 
28
        }}
-
 
29
99
}
-
 
100
30
101