/** * If the upload task is initialized with a notification configuration, this handles its * creation. * * @param uploadInfo upload information and statistics */ private void createNotification(UploadInfo uploadInfo) { if (params.notificationConfig == null || params.notificationConfig.getProgress().message == null) return; UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); notificationCreationTimeMillis = System.currentTimeMillis(); NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setWhen(notificationCreationTimeMillis) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) .setSmallIcon(statusConfig.iconResourceID) .setLargeIcon(statusConfig.largeIcon) .setColor(statusConfig.iconColorResourceID) .setGroup(UploadService.NAMESPACE) .setProgress(100, 0, true) .setOngoing(true); statusConfig.addActionsToNotificationBuilder(notification); Notification builtNotification = notification.build(); if (service.holdForegroundNotification(params.id, builtNotification)) { notificationManager.cancel(notificationId); } else { notificationManager.notify(notificationId, builtNotification); } }
private void updateNotification(UploadInfo uploadInfo, UploadNotificationStatusConfig statusConfig) { if (params.notificationConfig == null) return; notificationManager.cancel(notificationId); if (statusConfig.message == null) return; if (!statusConfig.autoClear) { NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) .setAutoCancel(statusConfig.clearOnAction) .setSmallIcon(statusConfig.iconResourceID) .setLargeIcon(statusConfig.largeIcon) .setColor(statusConfig.iconColorResourceID) .setGroup(UploadService.NAMESPACE) .setProgress(0, 0, false) .setOngoing(false); statusConfig.addActionsToNotificationBuilder(notification); setRingtone(notification); // this is needed because the main notification used to show progress is ongoing // and a new one has to be created to allow the user to dismiss it uploadInfo.setNotificationID(notificationId + 1); notificationManager.notify(notificationId + 1, notification.build()); } }
/** * Informs the {@link UploadService} that the task has made some progress. You should call this * method from your task whenever you have successfully transferred some bytes to the server. * * @param uploadInfo upload information and statistics */ private void updateNotificationProgress(UploadInfo uploadInfo) { if (params.notificationConfig == null || params.notificationConfig.getProgress().message == null) return; UploadNotificationStatusConfig statusConfig = params.notificationConfig.getProgress(); NotificationCompat.Builder notification = new NotificationCompat.Builder(service, params.notificationConfig.getNotificationChannelId()) .setWhen(notificationCreationTimeMillis) .setContentTitle(Placeholders.replace(statusConfig.title, uploadInfo)) .setContentText(Placeholders.replace(statusConfig.message, uploadInfo)) .setContentIntent(statusConfig.getClickIntent(service)) .setSmallIcon(statusConfig.iconResourceID) .setLargeIcon(statusConfig.largeIcon) .setColor(statusConfig.iconColorResourceID) .setGroup(UploadService.NAMESPACE) .setProgress((int) uploadInfo.getTotalBytes(), (int) uploadInfo.getUploadedBytes(), false) .setOngoing(true); statusConfig.addActionsToNotificationBuilder(notification); Notification builtNotification = notification.build(); if (service.holdForegroundNotification(params.id, builtNotification)) { notificationManager.cancel(notificationId); } else { notificationManager.notify(notificationId, builtNotification); } }
mBuilder.setOngoing(true);
.setContentIntent(getOpenTransfersIntent()) .setSmallIcon(R.drawable.ic_notification_upload) .setOngoing(true) .setOnlyAlertOnce(true) .addAction(createCancelAction(id));
private Notification createForegroundNotification() { //Why is this needed: https://developer.android.com/guide/components/services#Foreground Intent intent = new Intent(this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder notification = new NotificationCompat.Builder(this, NotificationHelper.Channels.PERSISTENT); notification .setSmallIcon(R.drawable.ic_notification) .setOngoing(true) .setContentIntent(pi) .setPriority(NotificationCompat.PRIORITY_MIN) //MIN so it's not shown in the status bar before Oreo, on Oreo it will be bumped to LOW .setShowWhen(false) .setAutoCancel(false); notification.setGroup("BackgroundService"); ArrayList<String> connectedDevices = new ArrayList<>(); for (Device device : getDevices().values()) { if (device.isReachable() && device.isPaired()) { connectedDevices.add(device.getName()); } } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { //Pre-oreo, the notification will have an empty title line without this notification.setContentTitle(getString(R.string.kde_connect)); } if (connectedDevices.isEmpty()) { notification.setContentText(getString(R.string.foreground_notification_no_devices)); } else { notification.setContentText(getString(R.string.foreground_notification_devices, TextUtils.join(", ", connectedDevices))); } return notification.build(); }
private void updateDone(boolean successful) { int icon; String title; String text; if (successful) { if (numFiles > 1) { text = res.getQuantityString(R.plurals.outgoing_files_text, numFiles, sentFiles, numFiles); } else { final String filename = toSend.get(0).getString("filename"); text = res.getString(R.string.sent_file_text, filename); } title = res.getString(R.string.sent_file_title, device.getName()); icon = android.R.drawable.stat_sys_upload_done; } else { final String filename = toSend.get(sentFiles).getString("filename"); title = res.getString(R.string.sent_file_failed_title, device.getName()); text = res.getString(R.string.sent_file_failed_text, filename); icon = android.R.drawable.stat_notify_error; } builder.setOngoing(false) .setTicker(title) .setContentTitle(title) .setContentText(text) .setSmallIcon(icon) .setProgress(0, 0, false); //setting progress to 0 out of 0 remove the progress bar }
private void runAsForeground() { if (mBroadcastReceiver == null) { mBroadcastReceiver = new ActionReceiver(); // Create a filter to only handle certain actions IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_CANCEL_BULKDOWNLOAD); registerReceiver(mBroadcastReceiver, intentFilter); } mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .setContentTitle(getString(R.string.downloader_notification_title)) .setStyle(new NotificationCompat.BigTextStyle() .bigText(getString(R.string.downloader_notification_remaining_images) + ' ' + String.valueOf(mSumImageDownloads - (mRemainingArtists + mRemainingAlbums)) + '/' + String.valueOf(mSumImageDownloads))) .setProgress(mSumImageDownloads, mSumImageDownloads - (mRemainingArtists + mRemainingAlbums), false) .setSmallIcon(R.drawable.odyssey_notification); openChannel(); mBuilder.setOngoing(true); // Cancel action Intent nextIntent = new Intent(BulkDownloadService.ACTION_CANCEL_BULKDOWNLOAD); PendingIntent nextPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT); androidx.core.app.NotificationCompat.Action cancelAction = new androidx.core.app.NotificationCompat.Action.Builder(R.drawable.ic_close_24dp, getString(R.string.dialog_action_cancel), nextPendingIntent).build(); mBuilder.addAction(cancelAction); Notification notification = mBuilder.build(); startForeground(NOTIFICATION_ID, notification); mNotificationManager.notify(NOTIFICATION_ID, notification); }
private void createUploadNotification(DCCServerManager.UploadEntry uploadEntry) { Integer id; synchronized (this) { id = mUploadNotificationIds.get(uploadEntry.getServer()); } if (id == null) return; createNotificationChannel(); NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL) .setContentTitle(uploadEntry.getFileName()) .setContentText(mContext.getString(R.string.dcc_active_waiting_for_connection)) .setContentIntent(getOpenTransfersIntent()) .setSmallIcon(R.drawable.ic_notification_upload) .setOngoing(true) .addAction(createCancelAction(id)); if (isNotificationGroupingEnabled()) builder.setGroup(NOTIFICATION_GROUP_DCC); mDisplayedNotificationIds.put(id, uploadEntry); mNotificationManager.notify(id, builder.build()); createSummaryNotification(); }
public void showBackgroundPlaybackNotification() { NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_headphones_white_24dp) .setOngoing(true) .setColor(Color.parseColor("#E62118")) .addAction(R.drawable.ic_pause_grey600_24dp, "PAUSE", NotificationCloser.getDismissIntent(NOTIFICATION_ID, context)) .setContentTitle(context.getString(R.string.app_name)) .setContentText(webView.getTitle().replace(" - YouTube", "")) .setAutoCancel(true) .setContentIntent(PendingIntent.getActivity( context, NOTIFICATION_ID, new Intent(context, MainActivity.class) .setAction(Intent.ACTION_VIEW) .setData(Uri.parse(webView.getUrl())), PendingIntent.FLAG_UPDATE_CURRENT)); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(NOTIFICATION_ID, builder.build()); }
NotificationUpdateCallback(Context context, Device device, ArrayList<NetworkPacket> toSend) { this.toSend = toSend; this.device = device; this.res = context.getResources(); String title; if (toSend.size() > 1) { title = res.getString(R.string.outgoing_files_title, device.getName()); } else { title = res.getString(R.string.outgoing_file_title, device.getName()); } notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(context, NotificationHelper.Channels.FILETRANSFER) .setSmallIcon(android.R.drawable.stat_sys_upload) .setAutoCancel(true) .setOngoing(true) .setProgress(100, 0, false) .setContentTitle(title) .setTicker(title); notificationId = (int) System.currentTimeMillis(); numFiles = toSend.size(); }
private void foreground(String message) { Intent notificationIntent = new Intent(this, MessagesActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, NotificationSupport.Channel.FOREGROUND) .setSmallIcon(R.drawable.ic_gotify) .setOngoing(true) .setPriority(NotificationCompat.PRIORITY_MIN) .setShowWhen(false) .setWhen(0) .setContentTitle(getString(R.string.app_name)) .setContentText(message) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setContentIntent(pendingIntent) .setColor( ContextCompat.getColor( getApplicationContext(), R.color.colorPrimary)) .build(); startForeground(NotificationSupport.ID.FOREGROUND, notification); }
public static Notification getFTPRunning(Context context, String title, String text, PendingIntent intent) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelHelper.getChannelLow(context)) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) .setSmallIcon(R.drawable.ic_notification_ftp) .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) .setDefaults(NotificationCompat.DEFAULT_ALL) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) .setAutoCancel(false) .setOngoing(true) .setContentTitle(title) .setContentText(text) .setContentIntent(intent); return builder.build(); } }
public void clearNotification() { if (mNotification != null) { if (mContext instanceof Service) { ((Service) mContext).stopForeground(true); mNotificationBuilder.setOngoing(false); mNotificationManager.cancel(NOTIFICATION_ID); } mNotification = null; mLastTrack = null; mLastBitmap = null; mNotificationBuilder = null; } }
public Notification getNotification(String message) { return new NotificationCompat.Builder(getApplicationContext(), MainActivity.id1) .setSmallIcon(R.mipmap.ic_launcher) .setOngoing(true) //persistent notification! .setChannelId(MainActivity.id1) .setContentTitle("Service") //Title message top row. .setContentText(message) //message when looking at the notification, second row .build(); //finally build and return a Notification. } }
private Notification buildForegroundNotification() { NotificationCompat.Builder b= new NotificationCompat.Builder(this, CHANNEL_WHATEVER); b.setOngoing(true) .setContentTitle(getString(R.string.msg_foreground)) .setSmallIcon(R.drawable.ic_stat_screen) .addAction(android.R.drawable.ic_media_pause, getString(R.string.msg_stop), buildStopPendingIntent()); return(b.build()); }
public ShareNotification(Device device) { this.device = device; notificationId = (int) System.currentTimeMillis(); notificationManager = (NotificationManager) device.getContext().getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.FILETRANSFER) .setSmallIcon(android.R.drawable.stat_sys_download) .setAutoCancel(true) .setOngoing(true) .setProgress(100, 0, true); }
@Override public NotificationBuilder setOngoing(final boolean ongoing) { super.setOngoing(ongoing); return this; }
public void setFinished(String message) { builder = new NotificationCompat.Builder(device.getContext(), NotificationHelper.Channels.DEFAULT); builder.setContentTitle(message) .setTicker(message) .setSmallIcon(android.R.drawable.stat_sys_download_done) .setAutoCancel(true) .setOngoing(false); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(device.getContext()); if (prefs.getBoolean("share_notification_preference", true)) { builder.setDefaults(Notification.DEFAULT_ALL); } }
.setOngoing(true) .setTicker(getResources().getString(R.string.notice_activated)) .setContentIntent(pIntent)