aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/GCMMessageService.java
blob: 06ee0e38f4a8584dafd9ea141667649ab5d5e03c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662

package org.wordpress.android;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.google.android.gms.gcm.GcmListenerService;

import org.apache.commons.lang.StringEscapeUtils;
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.analytics.AnalyticsTracker.Stat;
import org.wordpress.android.analytics.AnalyticsTrackerMixpanel;
import org.wordpress.android.models.AccountHelper;
import org.wordpress.android.ui.main.WPMainActivity;
import org.wordpress.android.ui.notifications.NotificationDismissBroadcastReceiver;
import org.wordpress.android.ui.notifications.NotificationEvents;
import org.wordpress.android.ui.notifications.NotificationsListFragment;
import org.wordpress.android.ui.notifications.utils.NotificationsUtils;
import org.wordpress.android.ui.notifications.utils.SimperiumUtils;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.HelpshiftHelper;
import org.wordpress.android.util.ImageUtils;
import org.wordpress.android.util.PhotonUtils;
import org.wordpress.android.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import de.greenrobot.event.EventBus;

public class GCMMessageService extends GcmListenerService {
    private static final ArrayMap<Integer, Bundle> sActiveNotificationsMap = new ArrayMap<>();
    private static String sPreviousNoteId = null;
    private static long sPreviousNoteTime = 0L;

    private static final String NOTIFICATION_GROUP_KEY = "notification_group_key";
    private static final int PUSH_NOTIFICATION_ID = 10000;
    private static final int AUTH_PUSH_NOTIFICATION_ID = 20000;
    public static final int GROUP_NOTIFICATION_ID = 30000;
    private static final int MAX_INBOX_ITEMS = 5;

    private static final String PUSH_ARG_USER = "user";
    private static final String PUSH_ARG_TYPE = "type";
    private static final String PUSH_ARG_TITLE = "title";
    private static final String PUSH_ARG_MSG = "msg";
    private static final String PUSH_ARG_NOTE_ID = "note_id";

    private static final String PUSH_TYPE_COMMENT = "c";
    private static final String PUSH_TYPE_LIKE = "like";
    private static final String PUSH_TYPE_COMMENT_LIKE = "comment_like";
    private static final String PUSH_TYPE_AUTOMATTCHER = "automattcher";
    private static final String PUSH_TYPE_FOLLOW = "follow";
    private static final String PUSH_TYPE_REBLOG = "reblog";
    private static final String PUSH_TYPE_PUSH_AUTH = "push_auth";
    private static final String PUSH_TYPE_BADGE_RESET = "badge-reset";

    // Add to the analytics properties map a subset of the push notification payload.
    private static String[] propertiesToCopyIntoAnalytics = {PUSH_ARG_NOTE_ID, PUSH_ARG_TYPE, "blog_id", "post_id",
            "comment_id"};

    private void synchronizedHandleDefaultPush(String from, @NonNull Bundle data) {
        // sActiveNotificationsMap being static, we can't just synchronize the method
        synchronized (GCMMessageService.class) {
            handleDefaultPush(from, data);
        }
    }

    private void handleDefaultPush(String from, @NonNull Bundle data) {
        // Ensure Simperium is running so that notes sync
        SimperiumUtils.configureSimperium(this, AccountHelper.getDefaultAccount().getAccessToken());

        long wpcomUserId = AccountHelper.getDefaultAccount().getUserId();
        String pushUserId = data.getString(PUSH_ARG_USER);
        // pushUserId is always set server side, but better to double check it here.
        if (!String.valueOf(wpcomUserId).equals(pushUserId)) {
            AppLog.e(T.NOTIFS, "wpcom userId found in the app doesn't match with the ID in the PN. Aborting.");
            return;
        }

        String noteType = StringUtils.notNullStr(data.getString(PUSH_ARG_TYPE));

        // Check for wpcom auth push, if so we will process this push differently
        if (noteType.equals(PUSH_TYPE_PUSH_AUTH)) {
            handlePushAuth(from, data);
            return;
        }

        if (noteType.equals(PUSH_TYPE_BADGE_RESET)) {
            handleBadgeResetPN(data);
            return;
        }

        buildAndShowNotificationFromNoteData(data);

        EventBus.getDefault().post(new NotificationEvents.NotificationsChanged());
    }

    private void buildAndShowNotificationFromNoteData(Bundle data) {

        if (data == null)
            return;

        String noteType = StringUtils.notNullStr(data.getString(PUSH_ARG_TYPE));

        String title = StringEscapeUtils.unescapeHtml(data.getString(PUSH_ARG_TITLE));
        if (title == null) {
            title = getString(R.string.app_name);
        }
        String message = StringEscapeUtils.unescapeHtml(data.getString(PUSH_ARG_MSG));
        String noteId = data.getString(PUSH_ARG_NOTE_ID, "");

        /*
         * if this has the same note_id as the previous notification, and the previous notification
         * was received within the last second, then skip showing it - this handles duplicate
         * notifications being shown due to the device being registered multiple times with different tokens.
         * (still investigating how this could happen - 21-Oct-13)
         *
         * this also handles the (rare) case where the user receives rapid-fire sub-second like notifications
         * due to sudden popularity (post gets added to FP and is liked by many people all at once, etc.),
         * which we also want to avoid since it would drain the battery and annoy the user
         *
         * NOTE: different comments on the same post will have a different note_id, but different likes
         * on the same post will have the same note_id, so don't assume that the note_id is unique
         */
        long thisTime = System.currentTimeMillis();
        if (sPreviousNoteId != null && sPreviousNoteId.equals(noteId)) {
            long seconds = TimeUnit.MILLISECONDS.toSeconds(thisTime - sPreviousNoteTime);
            if (seconds <= 1) {
                AppLog.w(T.NOTIFS, "skipped potential duplicate notification");
                return;
            }
        }

        sPreviousNoteId = noteId;
        sPreviousNoteTime = thisTime;

        // Update notification content for the same noteId if it is already showing
        int pushId = 0;
        for (Integer id : sActiveNotificationsMap.keySet()) {
            if (id == null) {
                continue;
            }
            Bundle noteBundle = sActiveNotificationsMap.get(id);
            if (noteBundle != null && noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(noteId)) {
                pushId = id;
                sActiveNotificationsMap.put(pushId, data);
                break;
            }
        }

        if (pushId == 0) {
            pushId = PUSH_NOTIFICATION_ID + sActiveNotificationsMap.size();
            sActiveNotificationsMap.put(pushId, data);
        }

        // Bump Analytics for PNs if "Show notifications" setting is checked (default). Skip otherwise.
        if (NotificationsUtils.isNotificationsEnabled(this)) {
            Map<String, Object> properties = new HashMap<>();
            if (!TextUtils.isEmpty(noteType)) {
                // 'comment' and 'comment_pingback' types are sent in PN as type = "c"
                if (noteType.equals(PUSH_TYPE_COMMENT)) {
                    properties.put("notification_type", "comment");
                } else {
                    properties.put("notification_type", noteType);
                }
            }

            bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_RECEIVED, data, properties);
            AnalyticsTracker.flush();
        }

        showGroupNotificationForActiveNotificationsMap(pushId, noteId, noteType, data.getString("icon"), title, message);
    }

    private void showGroupNotificationForActiveNotificationsMap(int pushId, String noteId, String noteType,
                                                                String largeIconUri, String title, String message) {

        // Build the new notification, add group to support wearable stacking
        NotificationCompat.Builder builder = getNotificationBuilder(title, message);

        Bitmap largeIconBitmap = getLargeIconBitmap(largeIconUri, shouldCircularizeNoteIcon(noteType));
        if (largeIconBitmap != null) {
            builder.setLargeIcon(largeIconBitmap);
        }

        showIndividualNotificationForBuilder(builder, noteType, noteId, pushId);

        // Also add a group summary notification, which is required for non-wearable devices
        showGroupNotificationForBuilder(builder, message);
    }

    private void addActionsForCommentNotification(NotificationCompat.Builder builder, String noteId) {
        // Add some actions if this is a comment notification
        Intent commentReplyIntent = new Intent(this, WPMainActivity.class);
        commentReplyIntent.putExtra(WPMainActivity.ARG_OPENED_FROM_PUSH, true);
        commentReplyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        commentReplyIntent.setAction("android.intent.action.MAIN");
        commentReplyIntent.addCategory("android.intent.category.LAUNCHER");
        commentReplyIntent.addCategory("comment-reply");
        commentReplyIntent.putExtra(NotificationsListFragment.NOTE_INSTANT_REPLY_EXTRA, true);
        if (noteId != null) {
            commentReplyIntent.putExtra(NotificationsListFragment.NOTE_ID_EXTRA, noteId);
        }
        PendingIntent commentReplyPendingIntent = PendingIntent.getActivity(this, 0, commentReplyIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        builder.addAction(R.drawable.ic_reply_white_24dp, getText(R.string.reply),
                commentReplyPendingIntent);
    }

    private Bitmap getLargeIconBitmap(String iconUrl, boolean shouldCircularizeIcon){
        Bitmap largeIconBitmap = null;
        if (iconUrl != null) {
            try {
                iconUrl = URLDecoder.decode(iconUrl, "UTF-8");
                int largeIconSize = getResources().getDimensionPixelSize(
                        android.R.dimen.notification_large_icon_height);
                String resizedUrl = PhotonUtils.getPhotonImageUrl(iconUrl, largeIconSize, largeIconSize);
                largeIconBitmap = ImageUtils.downloadBitmap(resizedUrl);
                if (largeIconBitmap != null && shouldCircularizeIcon) {
                    largeIconBitmap = ImageUtils.getCircularBitmap(largeIconBitmap);
                }
            } catch (UnsupportedEncodingException e) {
                AppLog.e(T.NOTIFS, e);
            }
        }
        return largeIconBitmap;
    }

    private NotificationCompat.Builder getNotificationBuilder(String title, String message){
        // Build the new notification, add group to support wearable stacking
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setColor(getResources().getColor(R.color.blue_wordpress))
                .setContentTitle(title)
                .setContentText(message)
                .setTicker(message)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setGroup(NOTIFICATION_GROUP_KEY);

        return builder;
    }

    private void showGroupNotificationForBuilder(NotificationCompat.Builder builder, String message) {

        if (builder == null) {
            return;
        }

        if (sActiveNotificationsMap.size() > 1) {
            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            int noteCtr = 1;
            for (Bundle pushBundle : sActiveNotificationsMap.values()) {
                // InboxStyle notification is limited to 5 lines
                if (noteCtr > MAX_INBOX_ITEMS) {
                    break;
                }
                if (pushBundle == null || pushBundle.getString(PUSH_ARG_MSG) == null) {
                    continue;
                }

                if (pushBundle.getString(PUSH_ARG_TYPE, "").equals(PUSH_TYPE_COMMENT)) {
                    String pnTitle = StringEscapeUtils.unescapeHtml((pushBundle.getString(PUSH_ARG_TITLE)));
                    String pnMessage = StringEscapeUtils.unescapeHtml((pushBundle.getString(PUSH_ARG_MSG)));
                    inboxStyle.addLine(pnTitle + ": " + pnMessage);
                } else {
                    String pnMessage = StringEscapeUtils.unescapeHtml((pushBundle.getString(PUSH_ARG_MSG)));
                    inboxStyle.addLine(pnMessage);
                }

                noteCtr++;
            }

            if (sActiveNotificationsMap.size() > MAX_INBOX_ITEMS) {
                inboxStyle.setSummaryText(String.format(getString(R.string.more_notifications),
                        sActiveNotificationsMap.size() - MAX_INBOX_ITEMS));
            }

            String subject = String.format(getString(R.string.new_notifications), sActiveNotificationsMap.size());
            NotificationCompat.Builder groupBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setColor(getResources().getColor(R.color.blue_wordpress))
                    .setGroup(NOTIFICATION_GROUP_KEY)
                    .setGroupSummary(true)
                    .setAutoCancel(true)
                    .setTicker(message)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText(subject)
                    .setStyle(inboxStyle);

            showNotificationForBuilder(groupBuilder, this, GROUP_NOTIFICATION_ID);

        } else {
            // Set the individual notification we've already built as the group summary
            builder.setGroupSummary(true);
            showNotificationForBuilder(builder, this, GROUP_NOTIFICATION_ID);
        }
    }

    private void showIndividualNotificationForBuilder(NotificationCompat.Builder builder, String noteType, String noteId, int pushId) {
        if (builder == null) {
            return;
        }

        if (noteType.equals(PUSH_TYPE_COMMENT)) {
            addActionsForCommentNotification(builder, noteId);
        }
        showNotificationForBuilder(builder, this, pushId);
    }

    // Displays a notification to the user
    private void showNotificationForBuilder(NotificationCompat.Builder builder, Context context, int notificationId) {
        if (builder == null || context == null) {
            return;
        }

        Intent resultIntent = new Intent(this, WPMainActivity.class);
        resultIntent.putExtra(WPMainActivity.ARG_OPENED_FROM_PUSH, true);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        resultIntent.setAction("android.intent.action.MAIN");
        resultIntent.addCategory("android.intent.category.LAUNCHER");
        if (sPreviousNoteId != null) {
            resultIntent.putExtra(NotificationsListFragment.NOTE_ID_EXTRA, sPreviousNoteId);
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

        boolean shouldPlaySound = prefs.getBoolean("wp_pref_notification_sound", false);
        boolean shouldVibrate = prefs.getBoolean("wp_pref_notification_vibrate", false);
        boolean shouldBlinkLight = prefs.getBoolean("wp_pref_notification_light", false);
        String notificationSound = prefs.getString("wp_pref_custom_notification_sound", null); //"" if None is selected


        // use default sound if the legacy sound preference was ON but the custom sound was not selected (null)
        if (shouldPlaySound && notificationSound == null) {
            builder.setSound(Uri.parse("content://settings/system/notification_sound"));
        } else if (!TextUtils.isEmpty(notificationSound)) {
            builder.setSound(Uri.parse(notificationSound));
        }

        if (shouldVibrate) {
            builder.setVibrate(new long[]{500, 500, 500});
        }
        if (shouldBlinkLight) {
            builder.setLights(0xff0000ff, 1000, 5000);
        }

        // Call broadcast receiver when notification is dismissed
        Intent notificationDeletedIntent = new Intent(this, NotificationDismissBroadcastReceiver.class);
        notificationDeletedIntent.putExtra("notificationId", notificationId);
        notificationDeletedIntent.setAction(String.valueOf(notificationId));
        PendingIntent pendingDeleteIntent =
                PendingIntent.getBroadcast(context, notificationId, notificationDeletedIntent, 0);
        builder.setDeleteIntent(pendingDeleteIntent);

        builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, resultIntent,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(notificationId, builder.build());
    }

    private void rebuildAndUpdateNotificationsOnSystemBar(Bundle data) {
        Bitmap largeIconBitmap = null;
        // here notify the existing group notification by eliminating the line that is now gone
        String title = getNotificationTitleOrAppNameFromBundle(data);
        String message = StringEscapeUtils.unescapeHtml(data.getString(PUSH_ARG_MSG));

        NotificationCompat.Builder builder = null;

        if (sActiveNotificationsMap.size() == 1) {
            //only one notification remains, so get the proper message for it and re-instate in the system dashboard
            Bundle remainingNote = sActiveNotificationsMap.values().iterator().next();
            if (remainingNote != null) {
                String remainingNoteTitle = StringEscapeUtils.unescapeHtml(remainingNote.getString(PUSH_ARG_TITLE));
                if (!TextUtils.isEmpty(remainingNoteTitle)) {
                    title = remainingNoteTitle;
                }
                String remainingNoteMessage = StringEscapeUtils.unescapeHtml(remainingNote.getString(PUSH_ARG_MSG));
                if (!TextUtils.isEmpty(remainingNoteMessage)) {
                    message = remainingNoteMessage;
                }
                largeIconBitmap = getLargeIconBitmap(remainingNote.getString("icon"),
                        shouldCircularizeNoteIcon(remainingNote.getString(PUSH_ARG_TYPE)));

                builder = getNotificationBuilder(title, message);

                String noteType = StringUtils.notNullStr(remainingNote.getString(PUSH_ARG_TYPE));
                String noteId = remainingNote.getString(PUSH_ARG_NOTE_ID, "");
                if (!sActiveNotificationsMap.isEmpty()) {
                    showIndividualNotificationForBuilder(builder, noteType, noteId, sActiveNotificationsMap.keyAt(0));
                }
            }
        }

        if (builder == null) {
            builder = getNotificationBuilder(title, message);
        }

        if (largeIconBitmap == null) {
            largeIconBitmap = getLargeIconBitmap(data.getString("icon"), shouldCircularizeNoteIcon(PUSH_TYPE_BADGE_RESET));
        }

        if (largeIconBitmap != null) {
            builder.setLargeIcon(largeIconBitmap);
        }

        showGroupNotificationForBuilder(builder, message);

    }

    private String getNotificationTitleOrAppNameFromBundle(Bundle data){
        String title = StringEscapeUtils.unescapeHtml(data.getString(PUSH_ARG_TITLE));
        if (title == null) {
            title = getString(R.string.app_name);
        }
        return title;
    }

    // Clear all notifications
    private void handleBadgeResetPN(Bundle data) {
        if (data == null || !data.containsKey(PUSH_ARG_NOTE_ID))  {
            // ignore the reset-badge PN if it's a global one
            return;
        }

        removeNotificationWithNoteIdFromSystemBar(this, data);
        //now that we cleared the specific notif, we can check and make any visual updates
        if (sActiveNotificationsMap.size() > 0) {
            rebuildAndUpdateNotificationsOnSystemBar(data);
        }

        EventBus.getDefault().post(new NotificationEvents.NotificationsChanged());
    }

    // Show a notification for two-step auth users who sign in from a web browser
    private void handlePushAuth(String from, Bundle data) {
        if (data == null) {
            return;
        }

        String pushAuthToken = data.getString("push_auth_token", "");
        String title = data.getString("title", "");
        String message = data.getString("msg", "");
        long expirationTimestamp = Long.valueOf(data.getString("expires", "0"));

        // No strings, no service
        if (TextUtils.isEmpty(pushAuthToken) || TextUtils.isEmpty(title) || TextUtils.isEmpty(message)) {
            return;
        }

        // Show authorization intent
        Intent pushAuthIntent = new Intent(this, WPMainActivity.class);
        pushAuthIntent.putExtra(WPMainActivity.ARG_OPENED_FROM_PUSH, true);
        pushAuthIntent.putExtra(NotificationsUtils.ARG_PUSH_AUTH_TOKEN, pushAuthToken);
        pushAuthIntent.putExtra(NotificationsUtils.ARG_PUSH_AUTH_TITLE, title);
        pushAuthIntent.putExtra(NotificationsUtils.ARG_PUSH_AUTH_MESSAGE, message);
        pushAuthIntent.putExtra(NotificationsUtils.ARG_PUSH_AUTH_EXPIRES, expirationTimestamp);
        pushAuthIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        pushAuthIntent.setAction("android.intent.action.MAIN");
        pushAuthIntent.addCategory("android.intent.category.LAUNCHER");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_icon)
                .setColor(getResources().getColor(R.color.blue_wordpress))
                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setPriority(NotificationCompat.PRIORITY_MAX);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, pushAuthIntent,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(AUTH_PUSH_NOTIFICATION_ID, builder.build());
    }

    @Override
    public void onMessageReceived(String from, Bundle data) {
        AppLog.v(T.NOTIFS, "Received Message");

        if (data == null) {
            AppLog.v(T.NOTIFS, "No notification message content received. Aborting.");
            return;
        }

        // Handle helpshift PNs
        if (TextUtils.equals(data.getString("origin"), "helpshift")) {
            HelpshiftHelper.getInstance().handlePush(this, new Intent().putExtras(data));
            return;
        }

        // Handle mixpanel PNs
        if (data.containsKey("mp_message")) {
            String mpMessage = data.getString("mp_message");
            String title = getString(R.string.app_name);
            Intent resultIntent = new Intent(this, WPMainActivity.class);
            resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            AnalyticsTrackerMixpanel.showNotification(this, pendingIntent,
                    R.drawable.notification_icon, title, mpMessage);
            return;
        }

        if (!AccountHelper.isSignedInWordPressDotCom()) {
            return;
        }

        synchronizedHandleDefaultPush(from, data);
    }

    // Returns true if the note type is known to have a gravatar
    public boolean shouldCircularizeNoteIcon(String noteType) {
        if (TextUtils.isEmpty(noteType)) {
            return false;
        }

        switch (noteType) {
            case PUSH_TYPE_COMMENT:
            case PUSH_TYPE_LIKE:
            case PUSH_TYPE_COMMENT_LIKE:
            case PUSH_TYPE_AUTOMATTCHER:
            case PUSH_TYPE_FOLLOW:
            case PUSH_TYPE_REBLOG:
                return true;
            default:
                return false;
        }
    }

    public static synchronized void clearNotifications() {
        sActiveNotificationsMap.clear();
    }

    public static synchronized int getNotificationsCount() {
        return sActiveNotificationsMap.size();
    }

    public static synchronized boolean hasNotifications() {
        return !sActiveNotificationsMap.isEmpty();
    }

    // Removes a specific notification from the internal map - only use this when we know
    // the user has dismissed the app by swiping it off the screen
    public static synchronized void removeNotification(int notificationId) {
        sActiveNotificationsMap.remove(notificationId);
    }

    // Removes a specific notification from the system bar
    public static synchronized void removeNotificationWithNoteIdFromSystemBar(Context context, Bundle data) {
        if (context == null || data == null || !hasNotifications()) {
            return;
        }

        String noteID = data.getString(PUSH_ARG_NOTE_ID, "");
        if (TextUtils.isEmpty(noteID)) {
            return;
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        // here we loop with an Iterator as there might be several Notifications with the same Note ID (i.e. likes on the same Note)
        // so we need to keep cancelling them and removing them from our activeNotificationsMap as we find it suitable
        for(Iterator<Map.Entry<Integer, Bundle>> it = sActiveNotificationsMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<Integer, Bundle> row = it.next();
            Integer pushId = row.getKey();
            Bundle noteBundle = row.getValue();
            if (noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(noteID)) {
                notificationManager.cancel(pushId);
                it.remove();
            }
        }

        if (sActiveNotificationsMap.size() == 0) {
            notificationManager.cancel(GCMMessageService.GROUP_NOTIFICATION_ID);
        }
    }

    // Removes all app notifications from the system bar
    public static synchronized void removeAllNotifications(Context context) {
        if (context == null || !hasNotifications()) {
            return;
        }

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        for (Integer pushId : sActiveNotificationsMap.keySet()) {
            notificationManager.cancel(pushId);
        }
        notificationManager.cancel(GCMMessageService.GROUP_NOTIFICATION_ID);

        clearNotifications();
    }

    // NoteID is the ID if the note in WordPress
    public static synchronized void bumpPushNotificationsTappedAnalytics(String noteID) {
        for (int id : sActiveNotificationsMap.keySet()) {
            Bundle noteBundle = sActiveNotificationsMap.get(id);
            if (noteBundle.getString(PUSH_ARG_NOTE_ID, "").equals(noteID)) {
                bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_TAPPED, noteBundle, null);
                AnalyticsTracker.flush();
                return;
            }
        }
    }

    // Mark all notifications as tapped
    public static synchronized void bumpPushNotificationsTappedAllAnalytics() {
        for (int id : sActiveNotificationsMap.keySet()) {
            Bundle noteBundle = sActiveNotificationsMap.get(id);
            bumpPushNotificationsAnalytics(Stat.PUSH_NOTIFICATION_TAPPED, noteBundle, null);
        }
        AnalyticsTracker.flush();
    }

    private static void bumpPushNotificationsAnalytics(Stat stat, Bundle noteBundle,
                                                       Map<String, Object> properties) {
        // Bump Analytics for PNs if "Show notifications" setting is checked (default). Skip otherwise.
        if (!NotificationsUtils.isNotificationsEnabled(WordPress.getContext())) {
            return;
        }
        if (properties == null) {
            properties = new HashMap<>();
        }

        String notificationID = noteBundle.getString(PUSH_ARG_NOTE_ID, "");
        if (!TextUtils.isEmpty(notificationID)) {
            for (String currentPropertyToCopy : propertiesToCopyIntoAnalytics) {
                if (noteBundle.containsKey(currentPropertyToCopy)) {
                    properties.put("push_notification_" + currentPropertyToCopy, noteBundle.get(currentPropertyToCopy));
                }
            }
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(WordPress.getContext());
            String lastRegisteredGCMToken = preferences.getString(NotificationsUtils.WPCOM_PUSH_DEVICE_TOKEN, null);
            properties.put("push_notification_token", lastRegisteredGCMToken);
            AnalyticsTracker.track(stat, properties);
        }
    }
}