Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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