androidx.core.app.NotificationCompat.Builder Java Examples

The following examples show how to use androidx.core.app.NotificationCompat.Builder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
public Notification getOngoingCallNotification(final AbstractJingleConnection.Id id, final Set<Media> media) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(mXmppConnectionService, "ongoing_calls");
    if (media.contains(Media.VIDEO)) {
        builder.setSmallIcon(R.drawable.ic_videocam_white_24dp);
        builder.setContentTitle(mXmppConnectionService.getString(R.string.ongoing_video_call));
    } else {
        builder.setSmallIcon(R.drawable.ic_call_white_24dp);
        builder.setContentTitle(mXmppConnectionService.getString(R.string.ongoing_call));
    }
    builder.setContentText(id.account.getRoster().getContact(id.with).getDisplayName());
    builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setCategory(NotificationCompat.CATEGORY_CALL);
    builder.setContentIntent(createPendingRtpSession(id, Intent.ACTION_VIEW, 101));
    builder.setOngoing(true);
    builder.addAction(new NotificationCompat.Action.Builder(
            R.drawable.ic_call_end_white_48dp,
            mXmppConnectionService.getString(R.string.hang_up),
            createCallAction(id.sessionId, XmppConnectionService.ACTION_END_CALL, 104))
            .build());
    return builder.build();
}
 
Example #2
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
private Person getPerson(Message message) {
    final Contact contact = message.getContact();
    final Person.Builder builder = new Person.Builder();
    if (contact != null) {
        builder.setName(contact.getDisplayName());
        final Uri uri = contact.getSystemAccount();
        if (uri != null) {
            builder.setUri(uri.toString());
        }
    } else {
        builder.setName(UIHelper.getColoredUsername(mXmppConnectionService, message));
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        builder.setIcon(IconCompat.createWithBitmap(mXmppConnectionService.getAvatarService().get(message, AvatarService.getSystemUiAvatarSize(mXmppConnectionService), false)));
    }
    return builder.build();
}
 
Example #3
Source File: NotificationManager.java    From ground-android with Apache License 2.0 5 votes vote down vote up
public Notification createSyncNotification(
    UploadState state, String title, int total, int progress) {
  NotificationCompat.Builder notification =
      new Builder(context, CHANNEL_ID)
          .setSmallIcon(R.drawable.ic_sync)
          .setContentTitle(title)
          .setPriority(NotificationCompat.PRIORITY_DEFAULT)
          .setOnlyAlertOnce(false)
          .setOngoing(false)
          .setProgress(total, progress, false);

  switch (state) {
    case STARTING:
      notification.setContentText(context.getString(R.string.starting));
      break;
    case IN_PROGRESS:
      notification
          .setContentText(context.getString(R.string.in_progress))
          // only alert once and don't allow cancelling it
          .setOnlyAlertOnce(true)
          .setOngoing(true);
      break;
    case PAUSED:
      notification.setContentText(context.getString(R.string.paused));
      break;
    case FAILED:
      notification.setContentText(context.getString(R.string.failed));
      break;
    case COMPLETED:
      notification.setContentText(context.getString(R.string.completed));
      break;
    default:
      Timber.e("Unknown sync state: %s", state.name());
      break;
  }
  return notification.build();
}
 
Example #4
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void updateNotification(final boolean notify, final List<String> conversations, final boolean summaryOnly) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
    final boolean quiteHours = isQuietHours();
    final boolean notifyOnlyOneChild = notify && conversations != null && conversations.size() == 1; //if this check is changed to > 0 catchup messages will create one notification per conversation
    if (notifications.size() == 0) {
        cancel(NOTIFICATION_ID);
    } else {
        if (notify) {
            this.markLastNotification();
        }
        final Builder mBuilder;
        if (notifications.size() == 1 && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mBuilder = buildSingleConversations(notifications.values().iterator().next(), notify, quiteHours);
            modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
            notify(NOTIFICATION_ID, mBuilder.build());
        } else {
            mBuilder = buildMultipleConversation(notify, quiteHours);
            if (notifyOnlyOneChild) {
                mBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
            }
            modifyForSoundVibrationAndLight(mBuilder, notify, quiteHours, preferences);
            if (!summaryOnly) {
                for (Map.Entry<String, ArrayList<Message>> entry : notifications.entrySet()) {
                    String uuid = entry.getKey();
                    final boolean notifyThis = notifyOnlyOneChild ? conversations.contains(uuid) : notify;
                    Builder singleBuilder = buildSingleConversations(entry.getValue(), notifyThis, quiteHours);
                    if (!notifyOnlyOneChild) {
                        singleBuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
                    }
                    modifyForSoundVibrationAndLight(singleBuilder, notifyThis, quiteHours, preferences);
                    singleBuilder.setGroup(CONVERSATIONS_GROUP);
                    setNotificationColor(singleBuilder);
                    notify(entry.getKey(), NOTIFICATION_ID, singleBuilder.build());
                }
            }
            notify(NOTIFICATION_ID, mBuilder.build());
        }
    }
}
 
Example #5
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void modifyForSoundVibrationAndLight(Builder mBuilder, boolean notify, boolean quietHours, SharedPreferences preferences) {
    final Resources resources = mXmppConnectionService.getResources();
    final String ringtone = preferences.getString("notification_ringtone", resources.getString(R.string.notification_ringtone));
    final boolean vibrate = preferences.getBoolean("vibrate_on_notification", resources.getBoolean(R.bool.vibrate_on_notification));
    final boolean led = preferences.getBoolean("led", resources.getBoolean(R.bool.led));
    final boolean headsup = preferences.getBoolean("notification_headsup", resources.getBoolean(R.bool.headsup_notifications));
    if (notify && !quietHours) {
        if (vibrate) {
            final int dat = 70;
            final long[] pattern = {0, 3 * dat, dat, dat};
            mBuilder.setVibrate(pattern);
        } else {
            mBuilder.setVibrate(new long[]{0});
        }
        Uri uri = Uri.parse(ringtone);
        try {
            mBuilder.setSound(fixRingtoneUri(uri));
        } catch (SecurityException e) {
            Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
        }
    } else {
        mBuilder.setLocalOnly(true);
    }
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setCategory(Notification.CATEGORY_MESSAGE);
    }
    mBuilder.setPriority(notify ? (headsup ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT) : NotificationCompat.PRIORITY_LOW);
    setNotificationColor(mBuilder);
    mBuilder.setDefaults(0);
    if (led) {
        mBuilder.setLights(LED_COLOR, 2000, 3000);
    }
}
 
Example #6
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void modifyIncomingCall(Builder mBuilder) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mXmppConnectionService);
    final Resources resources = mXmppConnectionService.getResources();
    final String ringtone = preferences.getString("call_ringtone", resources.getString(R.string.incoming_call_ringtone));
    mBuilder.setVibrate(CALL_PATTERN);
    final Uri uri = Uri.parse(ringtone);
    try {
        mBuilder.setSound(fixRingtoneUri(uri));
    } catch (SecurityException e) {
        Log.d(Config.LOGTAG, "unable to use custom notification sound " + uri.toString());
    }
    mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
    setNotificationColor(mBuilder);
    mBuilder.setLights(LED_COLOR, 2000, 3000);
}
 
Example #7
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
private void modifyForImage(final Builder builder, final Message message, final ArrayList<Message> messages) {
    try {
        final Bitmap bitmap = mXmppConnectionService.getFileBackend().getThumbnail(message, getPixel(288), false);
        final ArrayList<Message> tmp = new ArrayList<>();
        for (final Message msg : messages) {
            if (msg.getType() == Message.TYPE_TEXT
                    && msg.getTransferable() == null) {
                tmp.add(msg);
            }
        }
        final BigPictureStyle bigPictureStyle = new NotificationCompat.BigPictureStyle();
        bigPictureStyle.bigPicture(bitmap);
        if (tmp.size() > 0) {
            CharSequence text = getMergedBodies(tmp);
            bigPictureStyle.setSummaryText(text);
            builder.setContentText(text);
            builder.setTicker(text);
        } else {
            final String description = UIHelper.getFileDescriptionString(mXmppConnectionService, message);
            builder.setContentText(description);
            builder.setTicker(description);
        }
        builder.setStyle(bigPictureStyle);
    } catch (final IOException e) {
        modifyForTextOnly(builder, messages);
    }
}
 
Example #8
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
void updateFileAddingNotification(int current, Message message) {
    Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
    mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video));
    mBuilder.setProgress(100, current, false);
    mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
    mBuilder.setContentIntent(createContentIntent(message.getConversation()));
    mBuilder.setOngoing(true);
    if (Compatibility.runsTwentySix()) {
        mBuilder.setChannelId(VIDEOCOMPRESSION_CHANNEL_ID);
    }
    Notification notification = mBuilder.build();
    notify(FOREGROUND_NOTIFICATION_ID, notification);
}
 
Example #9
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
Notification AppUpdateNotification(PendingIntent intent, String version, String filesize) {
    Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
    mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.app_name));
    mBuilder.setContentText(String.format(mXmppConnectionService.getString(R.string.update_available), version, filesize));
    mBuilder.setSmallIcon(R.drawable.ic_update_notification);
    mBuilder.setContentIntent(intent);
    mBuilder.setOngoing(true);
    if (Compatibility.runsTwentySix()) {
        mBuilder.setChannelId(UPDATE_CHANNEL_ID);
    }
    return mBuilder.build();
}
 
Example #10
Source File: WearableNotificationWithVoice.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param notificationBuilder
 * @param actionTitleId
 * @param replyLabelResourceId
 * @param actionIcon
 * @param notificationId
 */
public WearableNotificationWithVoice(Builder notificationBuilder,
                                     int actionTitleId, int replyLabelResourceId, int actionIcon, int notificationId) {
    this.notificationBuilder = notificationBuilder;
    this.replyLabelResourceId = replyLabelResourceId;
    this.actionIconResId = actionIcon;
    this.actionTitleId = actionTitleId;
    this.notificationId = notificationId;
}
 
Example #11
Source File: WearableNotificationWithVoice.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Action buildWearableAction() {
    String replyLabel = mContext.getString(replyLabelResourceId);
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(replyLabel).build();
    // Create an intent for the reply action
    if (pendingIntent == null) {
        Intent replyIntent = new Intent(mContext, notificationHandler);
        pendingIntent = PendingIntent.getActivity(mContext, (int) (System.currentTimeMillis() & 0xfffffff), replyIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }
    // Create the reply action and add the remote input
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(actionIconResId,
            mContext.getString(actionTitleId), pendingIntent).addRemoteInput(remoteInput).build();
    return action;
}
 
Example #12
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.O)
void initializeChannels() {
    final Context c = mXmppConnectionService;
    final NotificationManager notificationManager = c.getSystemService(NotificationManager.class);
    if (notificationManager == null) {
        return;
    }
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("status", c.getString(R.string.notification_group_status_information)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats", c.getString(R.string.notification_group_messages)));
    notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("calls", c.getString(R.string.notification_group_calls)));
    final NotificationChannel foregroundServiceChannel = new NotificationChannel(FOREGROUND_CHANNEL_ID,
            c.getString(R.string.foreground_service_channel_name),
            NotificationManager.IMPORTANCE_MIN);
    foregroundServiceChannel.setDescription(c.getString(R.string.foreground_service_channel_description));
    foregroundServiceChannel.setShowBadge(false);
    foregroundServiceChannel.setGroup("status");
    notificationManager.createNotificationChannel(foregroundServiceChannel);

    final NotificationChannel backupChannel = new NotificationChannel(BACKUP_CHANNEL_ID,
            c.getString(R.string.backup_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    backupChannel.setShowBadge(false);
    backupChannel.setGroup("status");
    notificationManager.createNotificationChannel(backupChannel);

    final NotificationChannel videoCompressionChannel = new NotificationChannel(VIDEOCOMPRESSION_CHANNEL_ID,
            c.getString(R.string.video_compression_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    videoCompressionChannel.setShowBadge(false);
    videoCompressionChannel.setGroup("status");
    notificationManager.createNotificationChannel(videoCompressionChannel);

    final NotificationChannel AppUpdateChannel = new NotificationChannel(UPDATE_CHANNEL_ID,
            c.getString(R.string.app_update_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    AppUpdateChannel.setShowBadge(false);
    AppUpdateChannel.setGroup("status");
    notificationManager.createNotificationChannel(AppUpdateChannel);

    final NotificationChannel incomingCallsChannel = new NotificationChannel("incoming_calls",
            c.getString(R.string.incoming_calls_channel_name),
            NotificationManager.IMPORTANCE_HIGH);
    incomingCallsChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE), new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build());
    incomingCallsChannel.setShowBadge(false);
    incomingCallsChannel.setLightColor(LED_COLOR);
    incomingCallsChannel.enableLights(true);
    incomingCallsChannel.setGroup("calls");
    incomingCallsChannel.setBypassDnd(true);
    incomingCallsChannel.enableVibration(true);
    incomingCallsChannel.setVibrationPattern(CALL_PATTERN);
    notificationManager.createNotificationChannel(incomingCallsChannel);

    final NotificationChannel ongoingCallsChannel = new NotificationChannel("ongoing_calls",
            c.getString(R.string.ongoing_calls_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    ongoingCallsChannel.setShowBadge(false);
    ongoingCallsChannel.setGroup("calls");
    notificationManager.createNotificationChannel(ongoingCallsChannel);


    final NotificationChannel messagesChannel = new NotificationChannel("messages",
            c.getString(R.string.messages_channel_name),
            NotificationManager.IMPORTANCE_HIGH);
    messagesChannel.setShowBadge(true);
    messagesChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
            .build());
    messagesChannel.setLightColor(LED_COLOR);
    final int dat = 70;
    final long[] pattern = {0, 3 * dat, dat, dat};
    messagesChannel.setVibrationPattern(pattern);
    messagesChannel.enableVibration(true);
    messagesChannel.enableLights(true);
    messagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(messagesChannel);

    final NotificationChannel silentMessagesChannel = new NotificationChannel("silent_messages",
            c.getString(R.string.silent_messages_channel_name),
            NotificationManager.IMPORTANCE_LOW);
    silentMessagesChannel.setDescription(c.getString(R.string.silent_messages_channel_description));
    silentMessagesChannel.setShowBadge(true);
    silentMessagesChannel.setLightColor(LED_COLOR);
    silentMessagesChannel.enableLights(true);
    silentMessagesChannel.setGroup("chats");
    notificationManager.createNotificationChannel(silentMessagesChannel);

    final NotificationChannel quietHoursChannel = new NotificationChannel("quiet_hours",
            c.getString(R.string.title_pref_quiet_hours),
            NotificationManager.IMPORTANCE_LOW);
    quietHoursChannel.setShowBadge(true);
    quietHoursChannel.setLightColor(LED_COLOR);
    quietHoursChannel.enableLights(true);
    quietHoursChannel.setGroup("chats");
    quietHoursChannel.enableVibration(false);
    quietHoursChannel.setSound(null, null);
    notificationManager.createNotificationChannel(quietHoursChannel);
}
 
Example #13
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
private void setNotificationColor(final Builder mBuilder) {
    mBuilder.setColor(ContextCompat.getColor(mXmppConnectionService, R.color.primary));
}
 
Example #14
Source File: NotificationService.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
Notification createForegroundNotification() {
    final Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
    mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
    Account mAccount = null;
    String status;
    final List<Account> accounts = mXmppConnectionService.getAccounts();
    int enabled = 0;
    int connected = 0;
    if (accounts != null) {
        for (Account account : accounts) {
            if (account.isOnlineAndConnected()) {
                connected++;
                enabled++;
            } else if (account.isEnabled()) {
                enabled++;
            }
        }
        if (accounts.size() == 1) {
            mAccount = accounts.get(0);
            if (mAccount.getStatus() == Account.State.ONLINE) {
                status = "(" + mXmppConnectionService.getString(R.string.account_status_online) + ")";
                status = " " + status;
                Log.d(Config.LOGTAG, "Status: " + status);
                mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
            } else if (mAccount.getStatus() == Account.State.CONNECTING) {
                status = "(" + mXmppConnectionService.getString(R.string.account_status_connecting) + ")";
                status = " " + status;
                Log.d(Config.LOGTAG, "Status: " + status);
                mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
            } else {
                status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
                status = " " + status;
                Log.d(Config.LOGTAG, "Status: " + status);
                mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
            }
        } else if (accounts.size() > 1) {
            mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service));
        } else {
            status = "(" + mXmppConnectionService.getString(R.string.account_status_offline) + ")";
            status = " " + status;
            Log.d(Config.LOGTAG, "Status: " + status);
            mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.conversations_foreground_service) + status);
        }
    }
    mBuilder.setContentText(mXmppConnectionService.getString(R.string.connected_accounts, connected, enabled));
    mBuilder.setContentIntent(createOpenConversationsIntent());
    final PendingIntent openIntent = createOpenConversationsIntent();
    if (openIntent != null) {
        mBuilder.setContentIntent(openIntent);
    }
    mBuilder.setWhen(0);
    mBuilder.setPriority(Notification.PRIORITY_MIN);
    mBuilder.setSmallIcon(connected > 0 ? R.drawable.ic_link_white_24dp : R.drawable.ic_link_off_white_24dp);
    if (Compatibility.runsTwentySix()) {
        mBuilder.setChannelId(FOREGROUND_CHANNEL_ID);
    }
    return mBuilder.build();
}