public static NotificationCompat.Builder getBaseBuilder(Context ctx, boolean setAutoCancel) {
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(ctx);
    notifBuilder.setSound(defaultSoundUri);
    notifBuilder.setAutoCancel(setAutoCancel);
    notifBuilder.setSmallIcon(R.drawable.ic_notification);

    // Notification styles changed since Lollipop
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      int color;
      // We have to check the M version for the deprecation of the method getColor()
      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        color = ctx.getResources().getColor(R.color.highlight_light);
      } else {
        color = ctx.getResources().getColor(R.color.highlight_light);
      }
      notifBuilder.setColor(color);
    } else {
      // in older versions, we show the App logo
      notifBuilder.setLargeIcon(
          BitmapFactory.decodeResource(ctx.getResources(), MobileLearning.APP_LOGO));
    }

    return notifBuilder;
  }
 @Override
 public void onReceive(Context context, Intent intent) {
   LaundryRoom laundryRoom = intent.getParcelableExtra(context.getString(R.string.laundry));
   LaundryMachine machine =
       intent.getParcelableExtra(context.getString(R.string.laundry_machine_intent));
   if (laundryRoom == null || machine == null) {
     return;
   }
   StringBuilder builder = new StringBuilder();
   String roomname = laundryRoom.name.replace("FL", "Floor");
   String type =
       machine.machine_type.contains(context.getString(R.string.laundry_washer_textview))
           ? context.getString(R.string.laundry_washer_textview)
           : context.getString(R.string.laundry_dryer_textview);
   builder
       .append(roomname)
       .append(" ")
       .append(type)
       .append(" ")
       .append(machine.number)
       .append(" is available");
   NotificationCompat.Builder mBuilder =
       new NotificationCompat.Builder(context)
           .setSmallIcon(R.drawable.ic_local_laundry_service)
           .setContentTitle(context.getString(R.string.app_name))
           .setContentText(builder);
   NotificationManager manager =
       (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     mBuilder.setColor(ContextCompat.getColor(context, R.color.color_primary));
   }
   Intent main = new Intent(context, MainActivity.class);
   main.putExtra(context.getString(R.string.laundry_notification_alarm_intent), true);
   main.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
   main.putExtra(context.getString(R.string.laundry_hall_no), laundryRoom.hall_no);
   PendingIntent notifyIntent =
       PendingIntent.getActivity(
           context, NOTIFICATION_ID, main, PendingIntent.FLAG_UPDATE_CURRENT);
   mBuilder.setContentIntent(notifyIntent);
   manager.notify(NOTIFICATION_ID, mBuilder.build());
   PendingIntent fromIntent =
       PendingIntent.getBroadcast(
           context,
           intent.getIntExtra(context.getString(R.string.laundry_position), 0),
           intent,
           PendingIntent.FLAG_NO_CREATE);
   fromIntent.cancel();
 }
 private void setNotificationIconColor(
     String color, NotificationCompat.Builder mBuilder, String localIconColor) {
   int iconColor = 0;
   if (color != null && !"".equals(color)) {
     try {
       iconColor = Color.parseColor(color);
     } catch (IllegalArgumentException e) {
       Log.e(LOG_TAG, "couldn't parse color from android options");
     }
   } else if (localIconColor != null && !"".equals(localIconColor)) {
     try {
       iconColor = Color.parseColor(localIconColor);
     } catch (IllegalArgumentException e) {
       Log.e(LOG_TAG, "couldn't parse color from android options");
     }
   }
   if (iconColor != 0) {
     mBuilder.setColor(iconColor);
   }
 }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Received start id " + startId + ": " + intent);
    if (intent != null) {
      // config = Config.fromByteArray(intent.getByteArrayExtra("config"));
      config = (Config) intent.getParcelableExtra("config");
      Log.i(TAG, "Config: " + config.toString());

      // Build a Notification required for running service in foreground.
      NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
      builder.setContentTitle(config.getNotificationTitle());
      builder.setContentText(config.getNotificationText());
      builder.setSmallIcon(android.R.drawable.ic_menu_mylocation);
      if (config.getNotificationIcon() != null) {
        builder.setSmallIcon(getPluginResource(config.getSmallNotificationIcon()));
        builder.setLargeIcon(
            BitmapFactory.decodeResource(
                getApplication().getResources(),
                getPluginResource(config.getLargeNotificationIcon())));
      }
      if (config.getNotificationIconColor() != null) {
        builder.setColor(this.parseNotificationIconColor(config.getNotificationIconColor()));
      }

      setClickEvent(builder);

      Notification notification = builder.build();
      notification.flags |=
          Notification.FLAG_ONGOING_EVENT
              | Notification.FLAG_FOREGROUND_SERVICE
              | Notification.FLAG_NO_CLEAR;
      startForeground(startId, notification);
    }

    // We want this service to continue running until it is explicitly stopped
    return START_REDELIVER_INTENT;
  }
Esempio n. 5
0
 @Override
 public Builder setColor(int argb) {
   super.setColor(argb);
   return this;
 }
Esempio n. 6
0
  public void showNotifications(List<String> contacts) {
    if (!(ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationsActivity)) {
      Conversation[] conversations =
          Database.getInstance(applicationContext).getConversations(preferences.getDid());
      for (Conversation conversation : conversations) {
        if (!conversation.isUnread()
            || !contacts.contains(conversation.getContact())
            || (ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationActivity
                && ((ConversationActivity) ActivityMonitor.getInstance().getCurrentActivity())
                    .getContact()
                    .equals(conversation.getContact()))) {
          continue;
        }

        String smsContact = Utils.getContactName(applicationContext, conversation.getContact());
        if (smsContact == null) {
          smsContact = Utils.getFormattedPhoneNumber(conversation.getContact());
        }

        String allSmses = "";
        String mostRecentSms = "";
        boolean initial = true;
        for (Message message : conversation.getMessages()) {
          if (message.getType() == Message.Type.INCOMING && message.isUnread()) {
            if (initial) {
              allSmses = message.getText();
              mostRecentSms = message.getText();
              initial = false;
            } else {
              allSmses = message.getText() + "\n" + allSmses;
            }
          } else {
            break;
          }
        }

        NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(applicationContext);

        notificationBuilder.setContentTitle(smsContact);
        notificationBuilder.setContentText(mostRecentSms);
        notificationBuilder.setSmallIcon(R.drawable.ic_chat_white_24dp);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        notificationBuilder.setSound(
            Uri.parse(Preferences.getInstance(applicationContext).getNotificationSound()));
        notificationBuilder.setLights(0xFFAA0000, 1000, 5000);
        if (Preferences.getInstance(applicationContext).getNotificationVibrateEnabled()) {
          notificationBuilder.setVibrate(new long[] {0, 250, 250, 250});
        } else {
          notificationBuilder.setVibrate(new long[] {0});
        }
        notificationBuilder.setColor(0xFFAA0000);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(allSmses));

        Bitmap largeIconBitmap;
        try {
          largeIconBitmap =
              MediaStore.Images.Media.getBitmap(
                  applicationContext.getContentResolver(),
                  Uri.parse(
                      Utils.getContactPhotoUri(applicationContext, conversation.getContact())));
          largeIconBitmap = Bitmap.createScaledBitmap(largeIconBitmap, 256, 256, false);
          largeIconBitmap = Utils.applyCircularMask(largeIconBitmap);
          notificationBuilder.setLargeIcon(largeIconBitmap);
        } catch (Exception ignored) {

        }

        Intent intent = new Intent(applicationContext, ConversationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(applicationContext);
        stackBuilder.addParentStack(ConversationActivity.class);
        stackBuilder.addNextIntent(intent);
        notificationBuilder.setContentIntent(
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT));

        Intent replyIntent = new Intent(applicationContext, ConversationQuickReplyActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        } else {
          //noinspection deprecation
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        }
        replyIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent replyPendingIntent =
            PendingIntent.getActivity(
                applicationContext, 0, replyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder replyAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_reply_white_24dp,
                applicationContext.getString(R.string.notifications_button_reply),
                replyPendingIntent);
        notificationBuilder.addAction(replyAction.build());

        Intent markAsReadIntent = new Intent(applicationContext, MarkAsReadReceiver.class);
        markAsReadIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent markAsReadPendingIntent =
            PendingIntent.getBroadcast(
                applicationContext, 0, markAsReadIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder markAsReadAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_drafts_white_24dp,
                applicationContext.getString(R.string.notifications_button_mark_read),
                markAsReadPendingIntent);
        notificationBuilder.addAction(markAsReadAction.build());

        int id;
        if (notificationIds.get(conversation.getContact()) != null) {
          id = notificationIds.get(conversation.getContact());
        } else {
          id = notificationIdCount++;
          notificationIds.put(conversation.getContact(), id);
        }
        NotificationManager notificationManager =
            (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, notificationBuilder.build());
      }
    }
  }