Subversion Repositories SmartDukaan

Rev

Rev 22381 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21480 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;
22835 rajender 49
import java.net.MalformedURLException;
21480 rajender 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;
22835 rajender 63
    ArrayList<NameValuePair> nameValuePairsGcm;
21480 rajender 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;
22835 rajender 71
    Bitmap bitmap;
21480 rajender 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
 
22381 rajender 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]);
21480 rajender 150
                }
22381 rajender 151
                else {
152
                    notificationDataEditor.putString("cid", extras.get("cid").toString().split("_")[0]);
153
                    notificationDataEditor.commit();
22835 rajender 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
                    }
22381 rajender 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);
21480 rajender 171
            }
172
        }
22381 rajender 173
    }
21480 rajender 174
 
22835 rajender 175
    private void sendNotification(String msg, String type, String title, String url, String cid, Bitmap image) throws MalformedURLException {
21480 rajender 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);} */
22381 rajender 203
        else {
21480 rajender 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()) {
22835 rajender 212
                    resultIntent.setData(Uri.parse(apiData.getString("app.domain.profitmandi", "app.profitmandi.com")));
21480 rajender 213
                } else {
22835 rajender 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
 
21480 rajender 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.pmlauncher);
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.navigation_bar);
272
            } else{
273
                mBuilder.setSmallIcon(R.drawable.pmnotification3);
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(alarmSound);
281
            mBuilder.setContentIntent(contentIntent);
282
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
283
        } else {
284
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
285
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
286
                mBuilder.setSmallIcon(R.drawable.navigation_bar);
287
            } else{
288
                mBuilder.setSmallIcon(R.drawable.pmnotification3);
289
            }
290
            mBuilder.setContentTitle(title);
291
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
292
            mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image));
293
            mBuilder.setContentText(msg);
294
            mBuilder.setLargeIcon(bm);
295
            mBuilder.setAutoCancel(true);
296
            mBuilder.setSound(alarmSound);
297
            mBuilder.setContentIntent(contentIntent);
298
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
299
        }
300
 
301
        Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
302
                GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
303
        t.send(new HitBuilders.EventBuilder()
304
                .setCategory("Notification")
305
                .setAction("Building Notification for campaign Id " + cid)
306
                .setLabel("For User Id " + userData.getString("id", "0"))
307
                .build());
308
        new Analytics(ProfitTillConstants.ANALYTICS_URL,
309
                AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Notification Received", cid),
310
                AnalyticsJsonResponse.getAnalyticsRequestInstance(),
311
                AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
312
        ++NOTIFICATION_ID;
313
        nameValuePairsGcm = new ArrayList<>();
314
        nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
315
        nameValuePairsGcm.add(new BasicNameValuePair("cid", cid));
316
        nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
317
        nameValuePairsGcm.add(new BasicNameValuePair("result", "recieved"));
318
        String userId = userData.getString("id", "");
319
        String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
320
        if (!userId.isEmpty() && !androidId.isEmpty()) {
321
            String notificationUrl = apiData.getString("notification.count", "http://45.33.50.227:3001/getNotificationCount") + "?user_id=" + userId + "&android_id=" + androidId;
322
            new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
323
        }
324
        new NotificationRecievedData().execute(nameValuePairsGcm);
325
    }
326
 
327
    class NotificationRecievedData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
328
 
329
        @Override
330
        protected void onPreExecute() {
331
            super.onPreExecute();
332
            Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
333
                    GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
334
            t.send(new HitBuilders.EventBuilder()
335
                    .setCategory("Notification")
336
                    .setAction("Received Notification")
337
                    .setLabel("For User Id " + userData.getString("id", "0"))
338
                    .build());
339
        }
340
 
341
        @Override
342
        protected String doInBackground(ArrayList<NameValuePair>... arg0) {
343
 
344
            try {
345
                HttpClient httpclient = new DefaultHttpClient();
346
                HttpPost httppost = new HttpPost(apiData.getString("notification.data.url", "http://api.profittill.com/pushnotifications/add"));
347
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
348
                httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
349
                HttpResponse response = httpclient.execute(httppost);
350
                HttpEntity entity = response.getEntity();
351
                int status = response.getStatusLine().getStatusCode();
352
                nameValuePairsGcm.clear();
353
            } catch (Exception e) {
354
            }
355
            return "success";
356
        }
357
 
358
        @Override
359
        protected void onPostExecute(String result) {
360
            super.onPostExecute(result);
361
        }
362
    }
363
 
364
    public void sendNotificationByPolling(JSONArray objects,Context c) {
365
        userData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
366
        apiData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
367
        notificationData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("Notification_Data", Context.MODE_PRIVATE);
368
        userDataEditor = userData.edit();
369
        apiSettingsEditor = apiData.edit();
370
        notificationDataEditor = notificationData.edit();
371
 
372
        Bundle bundle;
373
 
374
        try {
375
            if (objects.length() != 0) {
376
                JSONObject obj = new JSONObject();
377
                JSONArray jsArray = new JSONArray();
378
                for (int i = 0; i < objects.length(); i++) {
379
                    JSONObject j = objects.getJSONObject(i);
380
                    String msg = j.getString("message");
381
                    String type = j.getString("type");
382
                    String title = j.getString("title");
383
                    String url = j.getString("url");
384
                    String cid = j.getString("cid");
385
                    String image=null;
386
                    if(j.has("image")) {
387
                        image = j.getString("image");
388
                        bitmap=getBitmapfromUrl(image);
389
                    }
390
                    if (notificationData.getString("cid", "").equalsIgnoreCase(cid.split("_")[0])) {
391
                        Log.d("Campaign Id is same " + notificationData.getString("cid", ""), "Campaign Id is same " + cid.split("_")[0]);
392
                    } else {
393
                        notificationDataEditor.putString("cid", cid.split("_")[0]);
394
                        notificationDataEditor.commit();
395
                        if (title.isEmpty()) {
396
                            title = "ProfitMandi";
397
                        }
398
                        Intent resultIntent;
399
                        mNotificationManager = (NotificationManager) GoogleAnalyticsTracker.getAppContext().getSystemService(NOTIFICATION_SERVICE);
400
                        if (userData.getString("id", "").isEmpty()) {
401
                            resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), LoginActivity.class);
402
                            resultIntent.setAction("Login");
403
                            bundle = new Bundle();
404
                            bundle.putString("cid", cid);
405
                            resultIntent.putExtras(bundle);
406
                        }
407
                        /*else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
408
                            resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), ReferrerActivity.class);
409
                            resultIntent.setAction("Login");
410
                            bundle = new Bundle();
411
                            bundle.putString("cid", cid);
412
                            resultIntent.putExtras(bundle);
413
                        }*/
414
                        else {
415
                            if (type.equalsIgnoreCase("Url")) {
416
                                resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
417
                                bundle = new Bundle();
418
                                bundle.putString("cid", cid);
419
                                resultIntent.putExtras(bundle);
420
                                resultIntent.setAction("20");
421
 
22835 rajender 422
 
21480 rajender 423
                                if (url.isEmpty()) {
22835 rajender 424
                                    resultIntent.setData(Uri.parse(apiData.getString("app.domain.profitmandi", "http://app.profitmandi.com")));
21480 rajender 425
                                } else {
22835 rajender 426
                                    URL urlString = new URL(url);
427
                                    String domain = urlString.getAuthority();
428
                                    String path =urlString.getFile();
429
                                    String protocol=urlString.getProtocol();
430
                                    String rr=urlString.getRef();
431
 
432
                                    if(domain.equals(apiData.getString("app.domain.profitmandi", "app.profitmandi.com"))){
433
                                        resultIntent.setData(Uri.parse(url));
434
                                    }
435
                                    else{
436
                                        url=protocol+"://"+(apiData.getString("app.domain.profitmandi", "app.profitmandi.com")) + path;
437
                                        resultIntent.setData(Uri.parse(url));
438
                                    }
439
 
21480 rajender 440
                                }
441
 
442
                            } else if (type.equalsIgnoreCase("Update")) {
443
                                final String appPackageName = GoogleAnalyticsTracker.getAppContext().getPackageName();
444
                                try {
445
                                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
446
                                } catch (android.content.ActivityNotFoundException anfe) {
447
                                    resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
448
                                }
449
                            } else {
450
                                if (url.contains("Profile")) {
451
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
452
                                    bundle = new Bundle();
453
                                    bundle.putString("cid", cid);
454
                                    resultIntent.putExtras(bundle);
455
                                    resultIntent.setAction("15");
456
                                } else if (url.contains("Tutorial")) {
457
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
458
                                    bundle = new Bundle();
459
                                    bundle.putString("cid", cid);
460
                                    resultIntent.putExtras(bundle);
461
                                    resultIntent.setAction("7");
462
                                } else if (url.contains("Contact")) {
463
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
464
                                    bundle = new Bundle();
465
                                    bundle.putString("cid", cid);
466
                                    resultIntent.putExtras(bundle);
467
                                    resultIntent.setAction("6");
468
                                } else {
469
                                    resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
470
                                    bundle = new Bundle();
471
                                    bundle.putString("cid", cid);
472
                                    resultIntent.putExtras(bundle);
473
                                }
474
                            }
475
                        }
476
                        PendingIntent contentIntent = PendingIntent.getActivity(GoogleAnalyticsTracker.getAppContext(), NOTIFICATION_ID, resultIntent, 0);
477
                        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
478
                        Bitmap bm = BitmapFactory.decodeResource(GoogleAnalyticsTracker.getAppContext().getResources(), R.drawable.pmlauncher);
479
                        if (bitmap==null) {
480
                            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
22381 rajender 481
                                    c )
21480 rajender 482
                                    .setSmallIcon(R.drawable.navigation_bar)
483
                                    .setContentTitle(title)
484
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
485
                                    .setContentText(msg)
486
                                    .setLargeIcon(bm)
487
                                    .setAutoCancel(true)
488
                                    .setSound(alarmSound);
489
                            mBuilder.setContentIntent(contentIntent);
490
                            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
491
                        }
492
 
493
                        else {
494
                            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
495
                                    GoogleAnalyticsTracker.getAppContext())
496
                                    .setSmallIcon(R.drawable.pmnotification3)
497
                                    .setContentTitle(title)
498
                                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
499
                                    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
500
                                    .setContentText(msg)
501
                                    .setLargeIcon(bm)
502
                                    .setAutoCancel(true)
503
                                    .setSound(alarmSound);
504
                            mBuilder.setContentIntent(contentIntent);
505
                            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
506
                        }
507
                        ++NOTIFICATION_ID;
508
                        JSONObject data = new JSONObject();
509
                        data.put("cid", cid);
510
                        data.put("user_id", userData.getString("id", null));
511
                        data.put("timestamp", UtilityFunctions.notificationDate());
512
                        data.put("result", "recieved");
513
                        jsArray.put(data);
514
                    }
515
                    obj.put("pushdata", jsArray);
516
                    String userId = userData.getString("id", "");
517
                    String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
518
                    if (!userId.isEmpty() && !androidId.isEmpty()) {
519
                        String notificationUrl = apiData.getString("notification.count", "") + "?user_id=" + userId + "&android_id=" + androidId;
520
                        new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
521
                    }
522
                }
523
                new PollNotificationRecievedData().execute(obj);
524
            }
525
        }
526
        catch (Exception e) {
527
            e.printStackTrace();
528
        }
529
    }
530
 
531
    class PollNotificationRecievedData extends AsyncTask<JSONObject, Integer, String> {
532
 
533
        @Override
534
        protected void onPreExecute() {
535
            super.onPreExecute();
536
        }
537
 
538
        @Override
539
        protected String doInBackground(JSONObject... arg0) {
540
 
541
            try {
542
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
543
                nameValuePairs.add(new BasicNameValuePair("pushdata", arg0[0].toString()));
544
                HttpClient httpclient = new DefaultHttpClient();
545
                HttpPost httppost = new HttpPost(apiData.getString("pollnotification.url.received", "http://45.79.106.95:3001/addPollNotification"));
546
                httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
547
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
548
                HttpResponse response = httpclient.execute(httppost);
549
                HttpEntity entity = response.getEntity();
550
            }
551
            catch (Exception e) {
552
                e.printStackTrace();
553
            }
554
            return "success";
555
        }
556
 
557
        @Override
558
        protected void onPostExecute(String result) {
559
            super.onPostExecute(result);
560
        }
561
    }
562
 
563
 
564
    public Bitmap getBitmapfromUrl(String imageUrl) {
565
        try {
566
            URL url = new URL(imageUrl);
567
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
568
            connection.setDoInput(true);
569
            connection.connect();
570
            InputStream input = connection.getInputStream();
571
            Bitmap bitmap = BitmapFactory.decodeStream(input);
572
            return bitmap;
573
        }
574
        catch (Exception e) {
575
            // TODO Auto-generated catch block
576
            e.printStackTrace();
577
            return null;
578
 
579
        }
580
    }
581
}