Subversion Repositories SmartDukaan

Rev

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

Rev 21179 Rev 21215
Line 1... Line 1...
1
package com.saholic.profittill.Services;
1
package com.saholic.profittill.Services;
2
 
2
 
3
import android.app.IntentService;
3
import android.app.IntentService;
-
 
4
import android.content.Context;
4
import android.content.Intent;
5
import android.content.Intent;
5
import android.content.SharedPreferences;
6
import android.content.SharedPreferences;
6
import android.net.ConnectivityManager;
7
import android.net.ConnectivityManager;
-
 
8
import android.os.AsyncTask;
-
 
9
import android.telephony.TelephonyManager;
7
import android.util.Log;
10
import android.util.Log;
8
 
11
 
-
 
12
import com.google.firebase.iid.FirebaseInstanceId;
-
 
13
import com.saholic.profittill.Constants.ProfitTillConstants;
9
import com.saholic.profittill.Network.NotificationPoll;
14
import com.saholic.profittill.Network.NotificationPoll;
10
import com.saholic.profittill.Utils.UtilityFunctions;
15
import com.saholic.profittill.Utils.UtilityFunctions;
11
 
16
 
-
 
17
import org.apache.http.HttpEntity;
-
 
18
import org.apache.http.HttpResponse;
-
 
19
import org.apache.http.NameValuePair;
-
 
20
import org.apache.http.client.HttpClient;
-
 
21
import org.apache.http.client.entity.UrlEncodedFormEntity;
-
 
22
import org.apache.http.client.methods.HttpPost;
-
 
23
import org.apache.http.impl.client.DefaultHttpClient;
-
 
24
import org.apache.http.message.BasicNameValuePair;
-
 
25
 
12
import java.text.DateFormat;
26
import java.text.DateFormat;
-
 
27
import java.util.ArrayList;
13
 
28
 
14
public class PollingService extends IntentService {
29
public class PollingService extends IntentService {
15
    SharedPreferences apiData, userData;
30
    SharedPreferences apiData, userData;
16
    SharedPreferences.Editor apiEditor,userEditor;
31
    SharedPreferences.Editor apiEditor,userEditor,userDataEditor;
-
 
32
    String regId;
-
 
33
    String msg = "";
-
 
34
    ArrayList<NameValuePair> nameValuePairsGcm;
17
    public PollingService() {
35
    public PollingService() {
18
 
36
 
19
        super("PollingService");
37
        super("PollingService");
20
    }
38
    }
21
 
39
 
22
    @Override
40
    @Override
23
    protected void onHandleIntent(Intent intent) {
41
    protected void onHandleIntent(Intent intent) {
24
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
42
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
25
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
43
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
-
 
44
 
26
        userEditor = userData.edit();
45
        userEditor = userData.edit();
-
 
46
        userDataEditor = userData.edit();
27
        apiEditor = apiData.edit();
47
        apiEditor = apiData.edit();
28
        userEditor.putString("alarm_set","true").commit();
48
        userEditor.putString("alarm_set", "true").commit();
-
 
49
        if (!userData.getString("id", "").equals("")) {
29
        if(isInternetOn()) {
50
            if (isInternetOn()) {
30
            String url = apiData.getString("pollnotification.url", "http://45.33.50.227:3001/pollNotifications") + "?user_id=" + userData.getString("id", "") + "&android_id=" + UtilityFunctions.androidId(getApplicationContext());
51
                String url = apiData.getString("pollnotification.url", "http://45.33.50.227:3001/pollNotifications") + "?user_id=" + userData.getString("id", "") + "&android_id=" + UtilityFunctions.androidId(getApplicationContext());
31
            new NotificationPoll().fetchNotfications(getApplicationContext(), null, url);
52
                new NotificationPoll().fetchNotfications(getApplicationContext(), null, url);
-
 
53
            }
-
 
54
 
-
 
55
            if (userData.getString("fcm_token_sent", "false").equals("false") && (!userData.getString("id", "").equals(""))) {
-
 
56
                regId = FirebaseInstanceId.getInstance().getToken();
-
 
57
                Log.d("RegisterActivity", "registerInBackground - regId: "
-
 
58
                        + regId);
-
 
59
                msg = "Device registered";
-
 
60
                new gcmPushData().execute(msg);
-
 
61
            }
-
 
62
 
32
        }
63
        }
33
    }
64
    }
34
 
65
 
35
    public final boolean isInternetOn() {
66
    public final boolean isInternetOn() {
36
        ConnectivityManager connec = (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
67
        ConnectivityManager connec = (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
Line 45... Line 76...
45
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
76
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
46
            return false;
77
            return false;
47
        }
78
        }
48
        return false;
79
        return false;
49
    }
80
    }
-
 
81
    class gcmPushData extends AsyncTask<String, Integer, String> {
-
 
82
 
-
 
83
        @Override
-
 
84
        protected void onPreExecute() {
-
 
85
            super.onPreExecute();
-
 
86
        }
-
 
87
 
-
 
88
        @Override
-
 
89
        protected String doInBackground(String... arg0) {
-
 
90
            String id = null;
-
 
91
            try {
-
 
92
                HttpClient httpclient = new DefaultHttpClient();
-
 
93
                HttpPost httppost = new HttpPost(apiData.getString("gcm.push.url", "http://api.profittill.com/gcm_users/add"));
-
 
94
                nameValuePairsGcm = new ArrayList<>();
-
 
95
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
-
 
96
                nameValuePairsGcm.add(new BasicNameValuePair("gcm_regid", regId));
-
 
97
                nameValuePairsGcm.add(new BasicNameValuePair("imeinumber", telephonyManager.getDeviceId()));
-
 
98
                nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", null)));
-
 
99
                nameValuePairsGcm.add(new BasicNameValuePair("notification_type","fcm"));
-
 
100
                nameValuePairsGcm.add(new BasicNameValuePair("device_message", arg0[0]));
-
 
101
                nameValuePairsGcm.add(new BasicNameValuePair("androidid", UtilityFunctions.androidId(PollingService.this)));
-
 
102
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
-
 
103
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairsGcm));
-
 
104
                HttpResponse response = httpclient.execute(httppost);
-
 
105
                HttpEntity entity = response.getEntity();
-
 
106
                int status = response.getStatusLine().getStatusCode();
-
 
107
 
-
 
108
                if (status == 200) {
-
 
109
                    Log.d("ResponseCode GCM ", status + "");
-
 
110
                    userDataEditor.putString("fcm_token_sent", "true");
-
 
111
                    userDataEditor.commit();
-
 
112
                } else {
-
 
113
                    Log.d("ResponseCode GCM ", status + "");
-
 
114
                }
-
 
115
 
-
 
116
                nameValuePairsGcm.clear();
-
 
117
                Log.e("pass 1", "connection success ");
-
 
118
            } catch (Exception e) {
-
 
119
                Log.e("Fail 1", e.toString());
-
 
120
 
-
 
121
            }
-
 
122
            return id;
-
 
123
        }
50
}
124
 
-
 
125
        @Override
-
 
126
        protected void onPostExecute(String result) {
-
 
127
            super.onPostExecute(result);
-
 
128
}}}