Пример #1
0
  /**
   * Check if this activity was launched from a local notification, and send details to application
   */
  private void checkLaunchedFromNotification() {
    if (this.lastIntentExtras != null) {
      LOG.Log(Module.GUI, "Activity was launched from Notification Manager... ");
      final String message = lastIntentExtras.getString(NotificationUtils.EXTRA_MESSAGE);
      final String notificationSound =
          this.lastIntentExtras.getString(NotificationUtils.EXTRA_SOUND);
      final String customJSONString =
          this.lastIntentExtras.getString(NotificationUtils.EXTRA_CUSTOM_JSON);
      final String notificationId =
          lastIntentExtras.getString(NotificationUtils.EXTRA_NOTIFICATION_ID);
      final String notificationType = lastIntentExtras.getString(NotificationUtils.EXTRA_TYPE);
      LOG.Log(Module.GUI, notificationType + " Notification ID = " + notificationId);

      NotificationData notif = new NotificationData();
      notif.setAlertMessage(message);
      notif.setSound(notificationSound);
      notif.setCustomDataJsonString(customJSONString);

      if (notificationType != null
          && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_LOCAL)) {
        appView.loadUrl(
            "javascript:try{Unity.OnLocalNotificationReceived("
                + JSONSerializer.serialize(notif)
                + ")}catch(e){}");
      } else if (notificationType != null
          && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_REMOTE)) {
        appView.loadUrl(
            "javascript:try{Unity.OnRemoteNotificationReceived("
                + JSONSerializer.serialize(notif)
                + ")}catch(e){}");
      }

      this.lastIntentExtras = null;
    }
  }
  /**
   * Check if this activity was launched from a local notification, and send details to application
   */
  private void checkLaunchedFromNotificationOrExternaly() {
    List<LaunchData> launchDataList = null;
    LOG.Log(Module.GUI, "checkLaunchedFromNotificationOrExternaly ");
    if (this.lastIntentExtras != null) {
      LOG.Log(Module.GUI, "checkLaunchedFromNotificationOrExternaly has intent extras");
      final String notificationId =
          lastIntentExtras.getString(NotificationUtils.EXTRA_NOTIFICATION_ID);
      if (notificationId != null && notificationId.length() > 0) {

        LOG.Log(Module.GUI, "Activity was launched from Notification Manager... ");
        final String message = lastIntentExtras.getString(NotificationUtils.EXTRA_MESSAGE);
        final String notificationSound =
            this.lastIntentExtras.getString(NotificationUtils.EXTRA_SOUND);
        final String customJSONString =
            this.lastIntentExtras.getString(NotificationUtils.EXTRA_CUSTOM_JSON);
        final String notificationType = lastIntentExtras.getString(NotificationUtils.EXTRA_TYPE);
        LOG.LogDebug(Module.GUI, notificationType + " Notification ID = " + notificationId);

        NotificationData notif = new NotificationData();
        notif.setAlertMessage(message);
        notif.setSound(notificationSound);
        notif.setCustomDataJsonString(customJSONString);

        if (notificationType != null
            && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_LOCAL)) {
          this.activityManager.loadUrlIntoWebView(
              "javascript:try{Appverse.OnLocalNotificationReceived("
                  + JSONSerializer.serialize(notif)
                  + ")}catch(e){}");
        } else if (notificationType != null
            && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_REMOTE)) {
          this.activityManager.loadUrlIntoWebView(
              "javascript:try{Appverse.PushNotifications.OnRemoteNotificationReceived("
                  + JSONSerializer.serialize(notif)
                  + ")}catch(e){}");
        }
      } else {
        LOG.Log(Module.GUI, "Activity was launched from an external app with extras... ");

        for (String key : this.lastIntentExtras.keySet()) {
          Object value = this.lastIntentExtras.get(key);
          /*
           * debugging LOG.Log(Module.GUI, String.format("%s %s (%s)",
           * key, value.toString(), value.getClass().getName()));
           */
          if (launchDataList == null) launchDataList = new ArrayList<LaunchData>();
          LaunchData launchData = new LaunchData();
          launchData.setName(key);
          launchData.setValue(value.toString());

          launchDataList.add(launchData);
        }
        LOG.Log(Module.GUI, "#num extras: " + launchDataList.size());
      }

      this.lastIntentExtras = null;
    }
    if (this.lastIntentData != null) {
      LOG.Log(Module.GUI, "Activity was launched from an external app with uri scheme... ");

      Set<String> lastIntentDataSet = this.getQueryParameterNames(this.lastIntentData);
      for (String key : lastIntentDataSet) {
        // for (String key : this.lastIntentData.getQueryParameterNames()) {
        String value = this.lastIntentData.getQueryParameter(key);
        /*
         * debugging LOG.Log(Module.GUI, String.format("%s %s (%s)",
         * key, value.toString(), value.getClass().getName()));
         */
        if (launchDataList == null) launchDataList = new ArrayList<LaunchData>();
        LaunchData launchData = new LaunchData();
        launchData.setName(key);
        launchData.setValue(value);

        launchDataList.add(launchData);
      }
      LOG.LogDebug(
          Module.GUI, "#num Data: " + (lastIntentDataSet == null ? 0 : lastIntentDataSet.size()));

      this.lastIntentData = null;
    }

    if (launchDataList != null) {
      String executeExternallyLaunchedListener =
          "javascript:try{Appverse.OnExternallyLaunched ("
              + JSONSerializer.serialize(
                  launchDataList.toArray(new LaunchData[launchDataList.size()]))
              + ")}catch(e){console.log('TESTING OnExternallyLaunched: ' + e);}";

      if (this.isWebviewReady()) {
        LOG.Log(Module.GUI, "Calling OnExternallyLaunched JS listener...");
        this.activityManager.loadUrlIntoWebView(executeExternallyLaunchedListener);
      } else {
        this.queueJSStatementsForWebviewClient(executeExternallyLaunchedListener);
      }
    }
  }
  /**
   * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
   *     android.content.Intent)
   */
  @Override
  public void onReceive(Context context, Intent intent) {

    LOGGER.logDebug("onReceive", "******* Local Notification Received " + intent.getAction());

    // Getting notification manager
    final NotificationManager notificationMgr = NotificationUtils.getNotificationManager(context);

    // Getting notification details from the Intent EXTRAS
    final Bundle bundle = intent.getExtras();
    final String ticker = bundle.getString(TICKER);
    final String title = bundle.getString(TITLE);
    final String body = bundle.getString(BODY);
    final String notificationSound = bundle.getString(SOUND);
    final String customJsonData = bundle.getString(CUSTOM_JSON_DATA);
    int notificationId = 0;

    try {
      notificationId = Integer.parseInt(bundle.getString(NOTIFICATION_ID));
    } catch (Exception e) {
      LOGGER.logError(
          "onReceive",
          "Unable to process local notification with id: " + bundle.getString(NOTIFICATION_ID));
    }

    int iIconId =
        context
            .getResources()
            .getIdentifier(
                NotificationUtils.DEFAULT_ICON_NAME,
                NotificationUtils.DRAWABLE_TYPE,
                NotificationUtils.PACKAGE_NAME);

    // Creates the notification to display
    Notification notif = null;
    NotificationData notificationData = new NotificationData();
    notificationData.setAlertMessage(body);
    notificationData.setSound(notificationSound);
    notificationData.setCustomDataJsonString(customJsonData);

    Notification.Builder mBuilder =
        new Notification.Builder(context)
            .setDefaults(0)
            .setContentIntent(
                NotificationUtils.getMainActivityAsPendingIntent(
                    context,
                    NotificationUtils.NOTIFICATION_TYPE_LOCAL,
                    "" + notificationId,
                    notificationData))
            .setSmallIcon(iIconId)
            // TODO
            // .setLights(Integer.parseInt(NOTIFICATION_STRUCTURE.get(RemoteNotificationFields.RN_LED_COLOR_ARGB.toString())), 100, 100)
            .setTicker(ticker)
            .setContentText(body)
            .setContentTitle(title);

    /**
     * TODO Bitmap largeIconBMP = null;
     *
     * <p>if(NOTIFICATION_STRUCTURE.containsKey(RemoteNotificationFields.RN_LARGE_ICON.toString())){
     * int iLargeIconId =
     * APP_RESOURCES.getIdentifier(NOTIFICATION_STRUCTURE.get(RemoteNotificationFields.RN_LARGE_ICON.toString()),
     * NotificationUtils.DRAWABLE_TYPE, PACKAGE_NAME); largeIconBMP =
     * BitmapFactory.decodeResource(APP_RESOURCES, iLargeIconId); } if(largeIconBMP!=null){
     * mBuilder.setLargeIcon(largeIconBMP); }
     */
    if (notificationSound != null
        && !notificationSound.isEmpty()
        && !notificationSound.equals("default")) {
      mBuilder.setSound(Uri.parse(notificationSound));
    } else {
      // set default sound
      mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    }
    notif = mBuilder.getNotification();

    // check if vibration should be enabled
    // notification.vibrate = new long[] { 0, 100, 200, 300 };

    /*
     * In order to stack all reminders in the notification bar, a random ID should be generated.
     * To replace an existing notification, ID should match in the notification intent.
     */
    notificationMgr.notify(notificationId, notif);

    // remove notification from stored shared preferences, as it has been already processed by the
    // notification manager
    NotificationUtils.removeLocalNotificationFromSharedPrefereces(context, "" + notificationId);

    final NotificationData notifData = notificationData;

    // TESTING MARGA
    if (appActivity != null && activityManager != null) {
      activityManager.loadUrlIntoWebView(
          "javascript:try{Unity.OnLocalNotificationReceived("
              + JSONSerializer.serialize(notifData)
              + ")}catch(e){}");
      LOGGER.logDebug("onReceive", "Invoking rNotification on UI thread... ");
    }
  }