private void handleNotificationTapInternal(Intent intent) {
    AbstractCloudMessage message = intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
    CloudAction action = intent.getParcelableExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.cancel(message.getId());

    MParticle.start(getApplicationContext());
    if (message instanceof MPCloudNotificationMessage) {
      MParticle.getInstance()
          .logNotification(
              (MPCloudNotificationMessage) message,
              action,
              true,
              getAppState(),
              AbstractCloudMessage.FLAG_READ | AbstractCloudMessage.FLAG_DIRECT_OPEN);
    }

    Intent broadcast = new Intent(MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED);
    broadcast.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message);
    broadcast.putExtra(MPMessagingAPI.CLOUD_ACTION_EXTRA, action);

    List<ResolveInfo> result = getPackageManager().queryBroadcastReceivers(broadcast, 0);
    if (result != null && result.size() > 0) {
      sendBroadcast(broadcast, null);
    } else {
      onHandleIntent(broadcast);
    }
  }
 @Override
 public final void onHandleIntent(final Intent intent) {
   boolean release = true;
   try {
     String action = intent.getAction();
     Log.i("MPService", "Handling action: " + action);
     if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
       MParticle.start(getApplicationContext());
       MParticle.getInstance().mKitManager.handleIntent(intent);
     } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) {
       generateCloudMessage(intent);
     } else if (action.startsWith(INTERNAL_NOTIFICATION_TAP)) {
       handleNotificationTapInternal(intent);
     } else if (action.equals(MPMessagingAPI.BROADCAST_NOTIFICATION_TAPPED)) {
       handleNotificationTap(intent);
     } else if (action.equals(MPMessagingAPI.BROADCAST_NOTIFICATION_RECEIVED)) {
       final AbstractCloudMessage message =
           intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
       showNotification(message);
       release = false;
     } else if (action.equals(INTERNAL_DELAYED_RECEIVE)) {
       final MPCloudNotificationMessage message =
           intent.getParcelableExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA);
       broadcastNotificationReceived(message);
     }
   } finally {
     synchronized (LOCK) {
       if (release && sWakeLock != null && sWakeLock.isHeld()) {
         sWakeLock.release();
       }
     }
   }
 }
 private void generateCloudMessage(Intent intent) {
   if (!processSilentPush(getApplicationContext(), intent.getExtras())) {
     try {
       MParticle.start(this);
       boolean handled = MParticle.getInstance().mKitManager.handleGcmMessage(intent);
       AbstractCloudMessage cloudMessage =
           AbstractCloudMessage.createMessage(intent, ConfigManager.getPushKeys(this));
       cloudMessage.setDisplayed(handled);
       String appState = getAppState();
       if (cloudMessage instanceof MPCloudNotificationMessage) {
         MParticle.getInstance()
             .saveGcmMessage(((MPCloudNotificationMessage) cloudMessage), appState);
         if (((MPCloudNotificationMessage) cloudMessage).isDelayed()) {
           MParticle.getInstance()
               .logNotification(
                   (MPCloudNotificationMessage) cloudMessage,
                   null,
                   false,
                   appState,
                   AbstractCloudMessage.FLAG_RECEIVED);
           scheduleFutureNotification((MPCloudNotificationMessage) cloudMessage);
           return;
         }
       } else if (cloudMessage instanceof ProviderCloudMessage) {
         MParticle.getInstance().saveGcmMessage(((ProviderCloudMessage) cloudMessage), appState);
       }
       broadcastNotificationReceived(cloudMessage);
     } catch (Exception e) {
       Log.w(TAG, "GCM parsing error: " + e.toString());
     }
   }
 }
 public boolean processSilentPush(Context context, Bundle extras) {
   if (extras != null && extras.containsKey(MPCloudNotificationMessage.COMMAND)) {
     int command = Integer.parseInt(extras.getString(MPCloudNotificationMessage.COMMAND));
     switch (command) {
       case MPCloudNotificationMessage.COMMAND_ALERT_CONFIG_REFRESH:
         MParticle.start(context);
         MParticle.getInstance().refreshConfiguration();
       case MPCloudNotificationMessage.COMMAND_DONOTHING:
         return true;
       default:
         return false;
     }
   }
   return false;
 }
 private String getAppState() {
   String appState = AppStateManager.APP_STATE_NOTRUNNING;
   if (AppStateManager.mInitialized) {
     if (MParticle.getInstance().mAppStateManager.isBackgrounded()) {
       appState = AppStateManager.APP_STATE_BACKGROUND;
     } else {
       appState = AppStateManager.APP_STATE_FOREGROUND;
     }
   }
   return appState;
 }
  private void showNotification(final AbstractCloudMessage message) {
    if (!message.getDisplayed()) {
      MParticle.getInstance().setNetworkTrackingEnabled(false);
      (new AsyncTask<AbstractCloudMessage, Void, Notification>() {
            @Override
            protected Notification doInBackground(AbstractCloudMessage... params) {
              return message.buildNotification(MPService.this, System.currentTimeMillis());
            }

            @Override
            protected void onPostExecute(Notification notification) {
              super.onPostExecute(notification);
              if (notification != null) {
                NotificationManager mNotifyMgr =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                mNotifyMgr.cancel(message.getId());
                mNotifyMgr.notify(message.getId(), notification);
              }
              synchronized (LOCK) {
                if (sWakeLock != null && sWakeLock.isHeld()) {
                  sWakeLock.release();
                }
              }
            }
          })
          .execute(message);
    }
    String appState = getAppState();
    if (message instanceof ProviderCloudMessage) {
      MParticle.getInstance().logNotification((ProviderCloudMessage) message, false, appState);
    } else if (message instanceof MPCloudNotificationMessage) {
      MParticle.getInstance()
          .logNotification(
              (MPCloudNotificationMessage) message,
              null,
              false,
              appState,
              AbstractCloudMessage.FLAG_RECEIVED | AbstractCloudMessage.FLAG_DISPLAYED);
    }
  }