예제 #1
0
  @Override
  public void onReceive(final Context context, Intent intent) {
    String intentAction = intent.getAction();
    if (intentAction != null && intentAction.equals("android.intent.action.BOOT_COMPLETED")) {
      AlarmHelper alarmManager = new AlarmHelper(context);
      alarmManager.setAlarm();
      //		} else if (intentAction != null && intentAction.equals("android.intent.action.TIME_SET"))
      // {
    } else {
      Notification notification = NotificationUtils.createNotification(context);
      NotificationUtils.notify(context, 123, notification);

      removeNotificationAfterTime(context);
    }
  }
  /**
   * Shows the notification message in the notification bar If the app is in background, launches
   * the app
   *
   * @param context
   * @param title
   * @param message
   * @param intent
   */
  private void showNotificationMessage(
      Context context, String title, String message, Intent intent) {

    notificationUtils = new NotificationUtils(context);

    intent.putExtras(parseIntent.getExtras());

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    notificationUtils.showNotificationMessage(title, message, intent);
  }
예제 #3
0
 /** Showing notification with text and image */
 private void showNotificationMessageWithBigImage(
     Context context,
     String title,
     String message,
     String timeStamp,
     Intent intent,
     String imageUrl) {
   notificationUtils = new NotificationUtils(context);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
   notificationUtils.showNotificationMessage(title, message, timeStamp, intent, imageUrl);
 }
예제 #4
0
 private boolean isSecret(int minVisibility, int privacyMask) {
   return NotificationUtils.isSecret(mContext, mNotification, minVisibility, privacyMask);
 }
예제 #5
0
  /**
   * Processing chat room push message this message will be broadcasts to all the activities
   * registered
   */
  private void processChatRoomPush(String title, boolean isBackground, String data) {
    if (!isBackground) {

      try {
        JSONObject datObj = new JSONObject(data);

        JSONObject mObj = datObj.getJSONObject("message");

        FeedNotifications message = new FeedNotifications();

        String for_unique_id = mObj.getString("for_unique_id");
        Log.e("for_unique_id : ", for_unique_id);
        message.setUnique_id(mObj.getString("unique_id"));
        message.setFor_unique_id(mObj.getString("for_unique_id"));
        message.setMessage(mObj.getString("message"));
        message.setId(mObj.getString("notification_id"));
        message.setCreatedAt(mObj.getString("created_at"));

        JSONObject uObj = datObj.getJSONObject("user");

        // skip the message if the message belongs to same user as
        // the user would be having the same message when he was sending
        // but it might differs in your scenario
        if (uObj.getString("user_id")
            .equals(AppController.getInstance().getPrefManager().getUser().getUid())) {
          Log.e(TAG, "Skipping the push message as it belongs to same user");
          return;
        }

        FeedUser user = new FeedUser();
        user.setUid(uObj.getString("user_id"));
        user.setEmail(uObj.getString("email"));
        //                user.setPhoto(uObj.getString("photo"));
        user.setName_user(uObj.getString("name"));
        message.setUser(user);

        // verifying whether the app is in background or foreground
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {

          // app is in foreground, broadcast the push message
          Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
          pushNotification.putExtra("type", Config.PUSH_TYPE_CHATROOM);
          pushNotification.putExtra("message", message);
          pushNotification.putExtra("for_unique_id", for_unique_id);
          LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

          // play notification sound
          NotificationUtils notificationUtils = new NotificationUtils();
          notificationUtils.playNotificationSound();
        } else {

          // app is in background. show the message in notification try
          Intent resultIntent = new Intent(getApplicationContext(), Notifications.class);
          resultIntent.putExtra("for_unique_id", for_unique_id);
          showNotificationMessage(
              getApplicationContext(),
              title,
              user.getName_user() + " : " + message.getMessage(),
              message.getCreatedAt(),
              resultIntent);

          // play notification sound
          NotificationUtils notificationUtils = new NotificationUtils();
          notificationUtils.playNotificationSound();
        }

      } catch (JSONException e) {
        Log.e(TAG, "json parsing error: " + e.getMessage());
        Toast.makeText(
                getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG)
            .show();
      }

    } else {
      // the push notification is silent, may be other operations needed
      // like inserting it in to SQLite
    }
  }
예제 #6
0
  /**
   * Processing user specific push message It will be displayed with / without image in push
   * notification tray
   */
  private void processUserMessage(String title, boolean isBackground, String data) {
    if (!isBackground) {

      try {
        JSONObject datObj = new JSONObject(data);

        String imageUrl = datObj.getString("image");

        JSONObject mObj = datObj.getJSONObject("message");
        FeedNotifications message = new FeedNotifications();
        message.setMessage(mObj.getString("message"));
        message.setId(mObj.getString("message_id"));
        message.setCreatedAt(mObj.getString("created_at"));

        JSONObject uObj = datObj.getJSONObject("user");
        FeedUser user = new FeedUser();
        user.setUid(uObj.getString("user_id"));
        user.setEmail(uObj.getString("email"));
        user.setName_user(uObj.getString("name"));
        message.setUser(user);

        // verifying whether the app is in background or foreground
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {

          // app is in foreground, broadcast the push message
          Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
          pushNotification.putExtra("type", Config.PUSH_TYPE_USER);
          pushNotification.putExtra("message", message);
          LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

          // play notification sound
          NotificationUtils notificationUtils = new NotificationUtils();
          notificationUtils.playNotificationSound();
        } else {

          // app is in background. show the message in notification try
          Intent resultIntent = new Intent(getApplicationContext(), Notifications.class);

          // check for push notification image attachment
          if (TextUtils.isEmpty(imageUrl)) {
            showNotificationMessage(
                getApplicationContext(),
                title,
                user.getName_user() + " : " + message.getMessage(),
                message.getCreatedAt(),
                resultIntent);
          } else {
            // push notification contains image
            // show it with the image
            showNotificationMessageWithBigImage(
                getApplicationContext(),
                title,
                message.getMessage(),
                message.getCreatedAt(),
                resultIntent,
                imageUrl);
          }
        }
      } catch (JSONException e) {
        Log.e(TAG, "json parsing error: " + e.getMessage());
        Toast.makeText(
                getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG)
            .show();
      }

    } else {
      // the push notification is silent, may be other operations needed
      // like inserting it in to SQLite
    }
  }
  /**
   * @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... ");
    }
  }
  public void postNotification(int id) {

    if (notificationItems.size() == 0) {
      return;
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setContentTitle(notificationTitle);
    mBuilder.setSmallIcon(R.drawable.ic_stat_icon);
    mBuilder.setLargeIcon(
        BitmapFactory.decodeResource(
            context.getResources(), R.drawable.ic_action_notification_dark));

    if (notificationItems.size() > 1) {
      // inbox style
      NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle();

      try {
        inbox.setBigContentTitle(notificationTitle);
      } catch (Exception e) {

      }

      if (notificationItems.size() <= 5) {
        for (String s : notificationItems) {
          inbox.addLine(Html.fromHtml(s));
        }
      } else {
        for (int i = 0; i < 5; i++) {
          inbox.addLine(Html.fromHtml(notificationItems.get(i)));
        }

        int extra = notificationItems.size() - 5;
        if (extra > 1) {
          inbox.setSummaryText("+" + extra + " " + context.getString(R.string.items));
        } else {
          inbox.setSummaryText("+" + extra + " " + context.getString(R.string.item));
        }
      }

      mBuilder.setStyle(inbox);
      mBuilder.setContentText(notificationItems.size() + " " + context.getString(R.string.items));
    } else {
      // big text style
      NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
      bigText.bigText(Html.fromHtml(notificationItems.get(0)));
      bigText.setBigContentTitle(notificationTitle);

      mBuilder.setStyle(bigText);
      mBuilder.setContentText(Html.fromHtml(notificationItems.get(0)));
    }

    if (!useSecondAccount) {
      mBuilder.setContentIntent(
          PendingIntent.getActivity(context, 0, new Intent(context, RedirectToActivity.class), 0));
    }

    if (settings.vibrate) {
      mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }

    if (settings.sound) {
      try {
        mBuilder.setSound(Uri.parse(settings.ringtone));
      } catch (Exception e) {
        mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
      }
    }

    if (settings.led) {
      mBuilder.setLights(0xFFFFFF, 1000, 1000);
    }

    if (settings.wakeScreen) {
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
      final PowerManager.WakeLock wakeLock =
          pm.newWakeLock(
              (PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                  | PowerManager.FULL_WAKE_LOCK
                  | PowerManager.ACQUIRE_CAUSES_WAKEUP),
              "TAG");
      wakeLock.acquire(5000);
    }

    // Pebble notification
    if (sharedPrefs.getBoolean("pebble_notification", false)) {
      NotificationUtils.sendAlertToPebble(context, notificationTitle, notificationItems.get(0));
    }

    // Light Flow notification
    NotificationUtils.sendToLightFlow(context, notificationTitle, notificationItems.get(0));

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(id, mBuilder.build());
  }