Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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