Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 1
package com.saholic.profittill;
2
 
3
import android.app.NotificationManager;
4
import android.app.PendingIntent;
5
import android.content.Context;
6
import android.content.Intent;
7
import android.content.SharedPreferences;
8
import android.graphics.Bitmap;
9
import android.graphics.BitmapFactory;
10
import android.media.RingtoneManager;
11
import android.net.Uri;
12
import android.os.AsyncTask;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.support.v4.app.NotificationCompat;
16
import android.util.Log;
17
 
18
import com.google.android.gms.analytics.HitBuilders;
19
import com.google.android.gms.analytics.Tracker;
20
import com.google.firebase.messaging.FirebaseMessagingService;
21
import com.google.firebase.messaging.RemoteMessage;
22
import com.mixpanel.android.mpmetrics.MixpanelAPI;
23
import com.saholic.profittill.Constants.ProfitTillConstants;
24
import com.saholic.profittill.Network.NotificationCount;
25
import com.saholic.profittill.Utils.AnalyticsUtility;
26
import com.saholic.profittill.Utils.UtilityFunctions;
27
import com.saholic.profittill.Volley.Analytics;
28
import com.saholic.profittill.Volley.AnalyticsErrorResponse;
29
import com.saholic.profittill.Volley.AnalyticsJsonResponse;
30
import com.saholic.profittill.main.GoogleAnalyticsTracker;
31
import com.saholic.profittill.main.LoginActivity;
32
import com.saholic.profittill.main.MainActivity;
33
 
34
import org.apache.http.HttpEntity;
35
import org.apache.http.HttpResponse;
36
import org.apache.http.NameValuePair;
37
import org.apache.http.client.HttpClient;
38
import org.apache.http.client.entity.UrlEncodedFormEntity;
39
import org.apache.http.client.methods.HttpPost;
40
import org.apache.http.impl.client.DefaultHttpClient;
41
import org.apache.http.message.BasicNameValuePair;
42
import org.apache.http.util.EntityUtils;
43
import org.json.JSONArray;
44
import org.json.JSONException;
45
import org.json.JSONObject;
46
 
47
import java.io.InputStream;
48
import java.net.HttpURLConnection;
49
import java.net.URL;
50
import java.util.ArrayList;
51
import java.util.Map;
52
 
53
/**
54
 * Created by rajender on 5/4/17.
55
 */
56
public class MyFirebaseMessagingService extends FirebaseMessagingService {
57
    SharedPreferences userData;
58
    SharedPreferences apiData;
59
    MixpanelAPI mixpanel;
60
    SharedPreferences.Editor userDataEditor;
61
    SharedPreferences.Editor apiSettingsEditor;
62
  ArrayList<NameValuePair> nameValuePairsGcm;
63
    public static int NOTIFICATION_ID = 1;
64
    private NotificationManager mNotificationManager;
65
    NotificationCompat.Builder builder;
66
    SharedPreferences notificationData;
67
    SharedPreferences.Editor notificationDataEditor;
68
    private static final String TAG = "MyFirebaseMsgService";
69
    Intent intent;
70
  Bitmap bitmap;
71
 
72
    @Override
73
    public void onMessageReceived(RemoteMessage remoteMessage) {
74
        userData = this.getSharedPreferences("User_Data", Context.MODE_PRIVATE);
75
        apiData = this.getSharedPreferences("API_Data", Context.MODE_PRIVATE);
76
        userDataEditor = userData.edit();
77
        apiSettingsEditor = apiData.edit();
78
        notificationData = this.getSharedPreferences("Notification_Data", Context.MODE_PRIVATE);
79
        notificationDataEditor = notificationData.edit();
80
        mixpanel = MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
81
        Map<String, String> data = remoteMessage.getData();
82
        if (data.containsKey("image")) {
83
            String imageUri = data.get("image");
84
            bitmap = getBitmapfromUrl(imageUri);
85
 
86
        }
87
            try {
88
               if(data.get("type").equalsIgnoreCase("update")){
89
                    new CheckNotificationExpiry().execute(data);
90
                }
91
                String campaignUserId = data.get("url").split("user_id=")[1];
92
                if (userData.getString("id", "0").equalsIgnoreCase(campaignUserId)) {
93
                    new Analytics(ProfitTillConstants.ANALYTICS_URL,
94
                            AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Check Expiry", data.get("cid") + ""),
95
                            AnalyticsJsonResponse.getAnalyticsRequestInstance(),
96
                            AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
97
                    new CheckNotificationExpiry().execute(data);
98
                }
99
 
100
 
101
            }
102
            catch (Exception e) {
103
                e.printStackTrace();
104
                Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
105
                        GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
106
                t.send(new HitBuilders.ExceptionBuilder()
107
                        .setDescription("Exception while Checking" + e.getMessage())
108
                        .build());
109
            }
110
 
111
        }
112
 
113
        class CheckNotificationExpiry extends AsyncTask<Map, Integer, String> {
114
            Map extras;
115
 
116
            @Override
117
            protected String doInBackground(Map... params) {
118
                extras = params[0];
119
                try {
120
                    HttpClient httpclient = new DefaultHttpClient();
121
                    String notificationCheckUrl = apiData.getString("notification.check.url", "http://api.profittill.com/notification_campaigns/notificationactive");
122
                    HttpPost httppost = new HttpPost(notificationCheckUrl + "?cid=" + extras.get("cid") + "&imeinumber=" + UtilityFunctions.getImeiNumber(getApplicationContext()));
123
                    httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
124
                    HttpResponse response = httpclient.execute(httppost);
125
                    HttpEntity entity = response.getEntity();
126
                    JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
127
                    int status = response.getStatusLine().getStatusCode();
128
                    return String.valueOf(jObjGmail.getString("success"));
129
                    // return jObjGmail.getString("success");
130
                } catch (Exception e) {
131
                    e.printStackTrace();
132
                    Log.e("Fail 1", e.toString());
133
                    return "true";
134
                }
135
 
136
            }
137
 
138
            @Override
139
            protected void onPostExecute(String result) {
140
                super.onPostExecute(result);
141
                if (result.isEmpty() || result.equals(null) || result.equalsIgnoreCase("true")) {
142
                    if (notificationData.getString("cid", "").equalsIgnoreCase(extras.get("cid").toString().split("_")[0])) {
143
                        Log.d("Campaign Id is same " + notificationData.getString("cid", ""), "Campaign Id is same " + extras.get("cid").toString().split("_")[0]);
144
                    }
145
                    else {
146
                        notificationDataEditor.putString("cid", extras.get("cid").toString().split("_")[0]);
147
                        notificationDataEditor.commit();
148
                        sendNotification(extras.get("message") + "", extras.get("type") + "", extras.get("title") + "", extras.get("url") + "", extras.get("cid") + "", bitmap);
149
                    }
150
                } else {
151
                    new Analytics(ProfitTillConstants.ANALYTICS_URL,
152
                            AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Notification Expired", extras.get("cid") + ""),
153
                            AnalyticsJsonResponse.getAnalyticsRequestInstance(),
154
                            AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
155
                    nameValuePairsGcm = new ArrayList<>();
156
                    nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
157
                    nameValuePairsGcm.add(new BasicNameValuePair("cid", extras.get("cid") + ""));
158
                    nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
159
                    nameValuePairsGcm.add(new BasicNameValuePair("result", "expired"));
160
                    new NotificationRecievedData().execute(nameValuePairsGcm);
161
                }
162
            }
163
        }
164
 
165
    private void sendNotification(String msg, String type, String title, String url, String cid, Bitmap image) {
166
        JSONObject propsNotificationRecieved = new JSONObject();
167
        Bundle bundle;
168
        try {
169
            mixpanel.identify(userData.getString("id", null));
170
            propsNotificationRecieved.put("Screen", "Notification Reached");
171
            mixpanel.track("Notification Recieved", propsNotificationRecieved
172
            );
173
        } catch (JSONException e) {
174
            e.printStackTrace();
175
        }
176
        if (title.isEmpty()) {
177
            title = "ProfitMandi";
178
        }
179
        Intent resultIntent;
180
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
181
        if (userData.getString("id", "").isEmpty()) {
182
            resultIntent = new Intent(this, LoginActivity.class);
183
            resultIntent.setAction("Login");
184
            bundle = new Bundle();
185
            bundle.putString("cid", cid);
186
            resultIntent.putExtras(bundle);
187
        } /*else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
188
            //resultIntent = new Intent(this, ReferrerActivity.class);
189
            //resultIntent.setAction("Login");
190
            //bundle = new Bundle();
191
           // bundle.putString("cid", cid);
192
            //resultIntent.putExtras(bundle);} */
193
            else {
194
            if (type.equalsIgnoreCase("Url")) {
195
                resultIntent = new Intent(this, MainActivity.class);
196
                bundle = new Bundle();
197
                bundle.putString("cid", cid);
198
                resultIntent.putExtras(bundle);
199
                resultIntent.setAction("20");
200
 
201
                if (url.isEmpty()) {
202
                    resultIntent.setData(Uri.parse(apiData.getString("mobile.website.url", "") + "?user_id=" + userData.getString("id", "")));
203
                } else {
204
                    resultIntent.setData(Uri.parse(url));
205
                }
206
 
207
            } else if (type.equalsIgnoreCase("Update")) {
208
                final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
209
                try {
210
                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
211
                } catch (android.content.ActivityNotFoundException anfe) {
212
                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
213
                }
214
            } else {
215
                if (url.contains("Profile")) {
216
                    resultIntent = new Intent(this, MainActivity.class);
217
                    bundle = new Bundle();
218
                    bundle.putString("cid", cid);
219
                    resultIntent.putExtras(bundle);
220
                    resultIntent.setAction("15");
221
                } else if (url.contains("Tutorial")) {
222
                    resultIntent = new Intent(this, MainActivity.class);
223
                    bundle = new Bundle();
224
                    bundle.putString("cid", cid);
225
                    resultIntent.putExtras(bundle);
226
                    resultIntent.setAction("7");
227
                } else if (url.contains("Contact")) {
228
                    resultIntent = new Intent(this, MainActivity.class);
229
                    bundle = new Bundle();
230
                    bundle.putString("cid", cid);
231
                    resultIntent.putExtras(bundle);
232
                    resultIntent.setAction("6");
233
                } else {
234
                    resultIntent = new Intent(this, MainActivity.class);
235
                    bundle = new Bundle();
236
                    bundle.putString("cid", cid);
237
                    resultIntent.putExtras(bundle);
238
                }
239
            }
240
        }
241
        PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, resultIntent, 0);
242
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
243
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.pmlauncher);
244
        if (image==null) {
245
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
246
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
247
                mBuilder.setSmallIcon(R.drawable.navigation_bar);
248
            } else{
249
                mBuilder.setSmallIcon(R.drawable.pmnotification3);
250
            }
251
            mBuilder.setContentTitle(title);
252
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
253
            mBuilder.setContentText(msg);
254
            mBuilder.setLargeIcon(bm);
255
            mBuilder.setAutoCancel(true);
256
            mBuilder.setSound(alarmSound);
257
            mBuilder.setContentIntent(contentIntent);
258
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
259
        } else {
260
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
261
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
262
                mBuilder.setSmallIcon(R.drawable.navigation_bar);
263
            } else{
264
                mBuilder.setSmallIcon(R.drawable.pmnotification3);
265
            }
266
            mBuilder.setContentTitle(title);
267
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
268
            mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image));
269
            mBuilder.setContentText(msg);
270
            mBuilder.setLargeIcon(bm);
271
            mBuilder.setAutoCancel(true);
272
            mBuilder.setSound(alarmSound);
273
            mBuilder.setContentIntent(contentIntent);
274
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
275
        }
276
 
277
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
278
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
279
        t.send(new HitBuilders.EventBuilder()
280
                .setCategory("Notification")
281
                .setAction("Building Notification for campaign Id " + cid)
282
                .setLabel("For User Id " + userData.getString("id", "0"))
283
                .build());
284
        new Analytics(ProfitTillConstants.ANALYTICS_URL,
285
                AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Notification Received", cid),
286
                AnalyticsJsonResponse.getAnalyticsRequestInstance(),
287
                AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
288
        ++NOTIFICATION_ID;
289
        nameValuePairsGcm = new ArrayList<>();
290
        nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
291
        nameValuePairsGcm.add(new BasicNameValuePair("cid", cid));
292
        nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
293
        nameValuePairsGcm.add(new BasicNameValuePair("result", "recieved"));
294
        String userId = userData.getString("id", "");
295
        String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
296
        if (!userId.isEmpty() && !androidId.isEmpty()) {
297
            String notificationUrl = apiData.getString("notification.count", "http://45.33.50.227:3001/getNotificationCount") + "?user_id=" + userId + "&android_id=" + androidId;
298
            new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
299
        }
300
        new NotificationRecievedData().execute(nameValuePairsGcm);
301
    }
302
 
303
    class NotificationRecievedData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
304
 
305
        @Override
306
        protected void onPreExecute() {
307
            super.onPreExecute();
308
            Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
309
                    GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
310
            t.send(new HitBuilders.EventBuilder()
311
                    .setCategory("Notification")
312
                    .setAction("Received Notification")
313
                    .setLabel("For User Id " + userData.getString("id", "0"))
314
                    .build());
315
        }
316
 
317
        @Override
318
        protected String doInBackground(ArrayList<NameValuePair>... arg0) {
319
 
320
            try {
321
                HttpClient httpclient = new DefaultHttpClient();
322
                HttpPost httppost = new HttpPost(apiData.getString("notification.data.url", "http://api.profittill.com/pushnotifications/add"));
323
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
324
                httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
325
                HttpResponse response = httpclient.execute(httppost);
326
                HttpEntity entity = response.getEntity();
327
                int status = response.getStatusLine().getStatusCode();
328
                nameValuePairsGcm.clear();
329
            } catch (Exception e) {
330
            }
331
            return "success";
332
        }
333
 
334
        @Override
335
        protected void onPostExecute(String result) {
336
            super.onPostExecute(result);
337
        }
338
    }
339
 
340
    public void sendNotificationByPolling(JSONArray objects,Context c) {
341
        userData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
342
        apiData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
343
        notificationData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("Notification_Data", Context.MODE_PRIVATE);
344
        userDataEditor = userData.edit();
345
        apiSettingsEditor = apiData.edit();
346
        notificationDataEditor = notificationData.edit();
347
 
348
        Bundle bundle;
349
 
350
        try {
351
            if (objects.length() != 0) {
352
                JSONObject obj = new JSONObject();
353
                JSONArray jsArray = new JSONArray();
354
                for (int i = 0; i < objects.length(); i++) {
355
                    JSONObject j = objects.getJSONObject(i);
356
                    String msg = j.getString("message");
357
                    String type = j.getString("type");
358
                    String title = j.getString("title");
359
                    String url = j.getString("url");
360
                    String cid = j.getString("cid");
361
                    String image=null;
362
                    if(j.has("image")) {
363
                        image = j.getString("image");
364
                        bitmap=getBitmapfromUrl(image);
365
                    }
366
                    if (notificationData.getString("cid", "").equalsIgnoreCase(cid.split("_")[0])) {
367
                        Log.d("Campaign Id is same " + notificationData.getString("cid", ""), "Campaign Id is same " + cid.split("_")[0]);
368
                    } else {
369
                        notificationDataEditor.putString("cid", cid.split("_")[0]);
370
                        notificationDataEditor.commit();
371
                        if (title.isEmpty()) {
372
                            title = "ProfitMandi";
373
                        }
374
                        Intent resultIntent;
375
                        mNotificationManager = (NotificationManager) GoogleAnalyticsTracker.getAppContext().getSystemService(NOTIFICATION_SERVICE);
376
                        if (userData.getString("id", "").isEmpty()) {
377
                            resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), LoginActivity.class);
378
                            resultIntent.setAction("Login");
379
                            bundle = new Bundle();
380
                            bundle.putString("cid", cid);
381
                            resultIntent.putExtras(bundle);
382
                        }
383
                        /*else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
384
                            resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), ReferrerActivity.class);
385
                            resultIntent.setAction("Login");
386
                            bundle = new Bundle();
387
                            bundle.putString("cid", cid);
388
                            resultIntent.putExtras(bundle);
389
                        }*/
390
                        else {
391
                            if (type.equalsIgnoreCase("Url")) {
392
                                resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
393
                                bundle = new Bundle();
394
                                bundle.putString("cid", cid);
395
                                resultIntent.putExtras(bundle);
396
                                resultIntent.setAction("20");
397
 
398
                                if (url.isEmpty()) {
399
                                    resultIntent.setData(Uri.parse(apiData.getString("mobile.website.url", "") + "?user_id=" + userData.getString("id", "")));
400
                                } else {
401
                                    resultIntent.setData(Uri.parse(url));
402
                                }
403
 
404
                            } else if (type.equalsIgnoreCase("Update")) {
405
                                final String appPackageName = GoogleAnalyticsTracker.getAppContext().getPackageName();
406
                                try {
407
                                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
408
                                } catch (android.content.ActivityNotFoundException anfe) {
409
                                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
410
                                }
411
                            } else {
412
                                if (url.contains("Profile")) {
413
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
414
                                    bundle = new Bundle();
415
                                    bundle.putString("cid", cid);
416
                                    resultIntent.putExtras(bundle);
417
                                    resultIntent.setAction("15");
418
                                } else if (url.contains("Tutorial")) {
419
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
420
                                    bundle = new Bundle();
421
                                    bundle.putString("cid", cid);
422
                                    resultIntent.putExtras(bundle);
423
                                    resultIntent.setAction("7");
424
                                } else if (url.contains("Contact")) {
425
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
426
                                    bundle = new Bundle();
427
                                    bundle.putString("cid", cid);
428
                                    resultIntent.putExtras(bundle);
429
                                    resultIntent.setAction("6");
430
                                } else {
431
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
432
                                    bundle = new Bundle();
433
                                    bundle.putString("cid", cid);
434
                                    resultIntent.putExtras(bundle);
435
                                }
436
                            }
437
                        }
438
                        PendingIntent contentIntent = PendingIntent.getActivity(GoogleAnalyticsTracker.getAppContext(), NOTIFICATION_ID, resultIntent, 0);
439
                        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
440
                        Bitmap bm = BitmapFactory.decodeResource(GoogleAnalyticsTracker.getAppContext().getResources(), R.drawable.pmlauncher);
441
                        if (bitmap==null) {
442
                            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
443
                                  c )
444
                                    .setSmallIcon(R.drawable.navigation_bar)
445
                                    .setContentTitle(title)
446
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
447
                                    .setContentText(msg)
448
                                    .setLargeIcon(bm)
449
                                    .setAutoCancel(true)
450
                                    .setSound(alarmSound);
451
                            mBuilder.setContentIntent(contentIntent);
452
                            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
453
                        }
454
 
455
                        else {
456
                            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
457
                                    GoogleAnalyticsTracker.getAppContext())
458
                                    .setSmallIcon(R.drawable.pmnotification3)
459
                                    .setContentTitle(title)
460
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
461
                                    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
462
                                    .setContentText(msg)
463
                                    .setLargeIcon(bm)
464
                                    .setAutoCancel(true)
465
                                    .setSound(alarmSound);
466
                            mBuilder.setContentIntent(contentIntent);
467
                            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
468
                        }
469
                        ++NOTIFICATION_ID;
470
                        JSONObject data = new JSONObject();
471
                        data.put("cid", cid);
472
                        data.put("user_id", userData.getString("id", null));
473
                        data.put("timestamp", UtilityFunctions.notificationDate());
474
                        data.put("result", "recieved");
475
                        jsArray.put(data);
476
                    }
477
                    obj.put("pushdata", jsArray);
478
                    String userId = userData.getString("id", "");
479
                    String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
480
                    if (!userId.isEmpty() && !androidId.isEmpty()) {
481
                        String notificationUrl = apiData.getString("notification.count", "") + "?user_id=" + userId + "&android_id=" + androidId;
482
                        new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
483
                    }
484
                }
485
                new PollNotificationRecievedData().execute(obj);
486
            }
487
        }
488
        catch (Exception e) {
489
            e.printStackTrace();
490
        }
491
    }
492
 
493
    class PollNotificationRecievedData extends AsyncTask<JSONObject, Integer, String> {
494
 
495
        @Override
496
        protected void onPreExecute() {
497
            super.onPreExecute();
498
        }
499
 
500
        @Override
501
        protected String doInBackground(JSONObject... arg0) {
502
 
503
            try {
504
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
505
                nameValuePairs.add(new BasicNameValuePair("pushdata", arg0[0].toString()));
506
                HttpClient httpclient = new DefaultHttpClient();
507
                HttpPost httppost = new HttpPost(apiData.getString("pollnotification.url.received", "http://45.79.106.95:3001/addPollNotification"));
508
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
509
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
510
                HttpResponse response = httpclient.execute(httppost);
511
                HttpEntity entity = response.getEntity();
512
            }
513
            catch (Exception e) {
514
                e.printStackTrace();
515
            }
516
            return "success";
517
        }
518
 
519
        @Override
520
        protected void onPostExecute(String result) {
521
            super.onPostExecute(result);
522
        }
523
    }
524
 
525
 
526
    public Bitmap getBitmapfromUrl(String imageUrl) {
527
        try {
528
            URL url = new URL(imageUrl);
529
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
530
            connection.setDoInput(true);
531
            connection.connect();
532
            InputStream input = connection.getInputStream();
533
            Bitmap bitmap = BitmapFactory.decodeStream(input);
534
            return bitmap;
535
        }
536
        catch (Exception e) {
537
            // TODO Auto-generated catch block
538
            e.printStackTrace();
539
            return null;
540
 
541
        }
542
    }
543
}