Subversion Repositories SmartDukaan

Rev

Rev 21179 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19653 manas 1
package com.saholic.profittill.Services;
2
 
3
import android.app.IntentService;
21215 rajender 4
import android.content.Context;
19653 manas 5
import android.content.Intent;
6
import android.content.SharedPreferences;
7
import android.net.ConnectivityManager;
21215 rajender 8
import android.os.AsyncTask;
9
import android.telephony.TelephonyManager;
19653 manas 10
import android.util.Log;
11
 
21215 rajender 12
import com.google.firebase.iid.FirebaseInstanceId;
13
import com.saholic.profittill.Constants.ProfitTillConstants;
19653 manas 14
import com.saholic.profittill.Network.NotificationPoll;
15
import com.saholic.profittill.Utils.UtilityFunctions;
16
 
21215 rajender 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
 
19653 manas 26
import java.text.DateFormat;
21215 rajender 27
import java.util.ArrayList;
19653 manas 28
 
29
public class PollingService extends IntentService {
30
    SharedPreferences apiData, userData;
21215 rajender 31
    SharedPreferences.Editor apiEditor,userEditor,userDataEditor;
32
    String regId;
33
    String msg = "";
34
    ArrayList<NameValuePair> nameValuePairsGcm;
19653 manas 35
    public PollingService() {
21179 rajender 36
 
19653 manas 37
        super("PollingService");
38
    }
39
 
40
    @Override
41
    protected void onHandleIntent(Intent intent) {
42
        userData = getApplicationContext().getSharedPreferences("User_Data", MODE_PRIVATE);
43
        apiData = getApplicationContext().getSharedPreferences("API_Data", MODE_PRIVATE);
21215 rajender 44
 
19653 manas 45
        userEditor = userData.edit();
21215 rajender 46
        userDataEditor = userData.edit();
19653 manas 47
        apiEditor = apiData.edit();
21215 rajender 48
        userEditor.putString("alarm_set", "true").commit();
49
        if (!userData.getString("id", "").equals("")) {
50
            if (isInternetOn()) {
51
                String url = apiData.getString("pollnotification.url", "http://45.33.50.227:3001/pollNotifications") + "?user_id=" + userData.getString("id", "") + "&android_id=" + UtilityFunctions.androidId(getApplicationContext());
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
 
19653 manas 63
        }
64
    }
65
 
66
    public final boolean isInternetOn() {
67
        ConnectivityManager connec = (ConnectivityManager)getSystemService(this.getBaseContext().CONNECTIVITY_SERVICE);
68
        if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
69
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
70
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
71
                connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED ) {
72
            return true;
73
 
74
        } else if (
75
                connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||
76
                        connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED  ) {
77
            return false;
78
        }
79
        return false;
80
    }
21215 rajender 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
        }
124
 
125
        @Override
126
        protected void onPostExecute(String result) {
127
            super.onPostExecute(result);
128
}}}