/** * Show a notification while this service is running. */ @SuppressWarnings("deprecation") private void showNotification() { Intent toLaunch = new Intent(getApplicationContext(), MonitorActivity.class); toLaunch.setAction(Intent.ACTION_MAIN); toLaunch.addCategory(Intent.CATEGORY_LAUNCHER); toLaunch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, toLaunch, PendingIntent.FLAG_UPDATE_CURRENT ); // In this sample, we'll use the same text for the ticker and the expanded notification CharSequence text = getText(R.string.secure_service_started); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_stat_haven) .setContentTitle(getString(R.string.app_name)) .setContentText(text); mBuilder.setPriority(NotificationCompat.PRIORITY_MIN); mBuilder.setContentIntent(resultPendingIntent); mBuilder.setWhen(System.currentTimeMillis()); mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET); startForeground(1, mBuilder.build()); }
/** * 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); } }
/** * 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); } }
public void shownoti(int val, String message, File file) { NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification noti; if (val >= 0) { Intent notificationIntent = new Intent(); notificationIntent.setAction(Intent.ACTION_VIEW); notificationIntent.setDataAndType( Uri.parse("file://" + file.getAbsolutePath()), "image/*" ); PendingIntent contentIntent = PendingIntent.getActivity(this, 100, notificationIntent, 0); noti = new NotificationCompat.Builder(getApplicationContext(), MainActivity.id) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) //When the event occurred, now, since noti are stored by time. .setContentIntent(contentIntent) //what activity to open. .setContentTitle("FileDownload") //Title message top row. .setContentText(message) //message when looking at the notification, second row .setAutoCancel(true) //allow auto cancel when pressed. .build(); //finally build and return a Notification. } else { noti = new NotificationCompat.Builder(getApplicationContext(), MainActivity.id) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) //When the event occurred, now, since noti are stored by time. .setContentTitle("File Download") //Title message top row. .setContentText(message) //message when looking at the notification, second row .setAutoCancel(true) //allow auto cancel when pressed. .build(); //finally build and return a Notification. } //finally Show the notification nm.notify(100, noti); }
private void makeForegroundNotify() { /* For starting main activity */ Intent startupIntent = new Intent(getApplicationContext(), MainActivity.class); startupIntent.setAction(Intent.ACTION_MAIN); startupIntent.addCategory(Intent.CATEGORY_LAUNCHER); startupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent startupPendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, startupIntent, PendingIntent.FLAG_UPDATE_CURRENT); foregroundNotify = new NotificationCompat.Builder(getApplicationContext(), FOREGROUND_NOTIFY_CHAN_ID) .setSmallIcon(R.drawable.ic_app_notification) .setContentIntent(startupPendingIntent) .setContentTitle(getString(R.string.app_running_in_the_background)) .setTicker(getString(R.string.app_running_in_the_background)) .setContentText((isNetworkOnline ? getString(R.string.network_online) : getString(R.string.network_offline))) .setWhen(System.currentTimeMillis()); foregroundNotify.addAction(makeFuncButtonAction()); foregroundNotify.addAction(makeShutdownAction()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) foregroundNotify.setCategory(Notification.CATEGORY_SERVICE); /* Disallow killing the service process by system */ startForeground(SERVICE_STARTED_NOTIFICATION_ID, foregroundNotify.build()); }
.setTicker(getString(R.string.torrent_finished_notify)) .setContentText(torrent.getName()) .setWhen(System.currentTimeMillis());
private synchronized void makeTorrentsMoveNotify() { if (torrentsMoveTotal == null || torrentsMoveSuccess == null || torrentsMoveFailed == null) { return; } NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID); String resultTemplate = getString(R.string.torrents_moved_content); int successfully = torrentsMoveSuccess.size(); int failed = torrentsMoveFailed.size(); builder.setContentTitle(getString(R.string.torrents_moved_title)) .setTicker(getString(R.string.torrents_moved_title)) .setContentText(String.format(resultTemplate, successfully, failed)); builder.setSmallIcon(R.drawable.ic_folder_move_white_24dp) .setAutoCancel(true) .setWhen(System.currentTimeMillis()) .setStyle(makeTorrentsMoveInboxStyle(torrentsMoveSuccess, torrentsMoveFailed)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { builder.setCategory(Notification.CATEGORY_STATUS); } notifyManager.notify(TORRENTS_MOVED_NOTIFICATION_ID, builder.build()); torrentsMoveTotal = null; torrentsMoveSuccess = null; torrentsMoveFailed = null; }
private void showNotification(int id, String title, String message, long priority) { Intent intent = new Intent(this, MessagesActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder( this, NotificationSupport.convertPriorityToChannel(priority)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { showNotificationGroup(priority); } b.setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_gotify) .setTicker(getString(R.string.app_name) + " - " + title) .setGroup(NotificationSupport.Group.MESSAGES) .setContentTitle(title) .setContentText(message) .setStyle(new NotificationCompat.BigTextStyle().bigText(message)) .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND) .setLights(Color.CYAN, 1000, 5000) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) .setContentIntent(contentIntent); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(id, b.build()); }
@RequiresApi(Build.VERSION_CODES.N) public void showNotificationGroup(long priority) { Intent intent = new Intent(this, MessagesActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder b = new NotificationCompat.Builder( this, NotificationSupport.convertPriorityToChannel(priority)); b.setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_gotify) .setTicker(getString(R.string.app_name)) .setGroup(NotificationSupport.Group.MESSAGES) .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN) .setContentTitle(getString(R.string.grouped_notification_text)) .setGroupSummary(true) .setContentText(getString(R.string.grouped_notification_text)) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) .setContentIntent(contentIntent); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(-5, b.build()); } }
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); }
private static NotificationCompat.Builder createNotificationBuilder( Context ctx, Account account, NotificationEntity entity, boolean feedback) { return new NotificationCompat.Builder(ctx, account.getAccountHash()) .setContentTitle(entity.mNotification.subject) .setContentText(getContentTitle(ctx, entity, true)) .setSubText(getNotificationSubText(account)) .setSmallIcon(R.drawable.ic_stat_notify) .setAutoCancel(true) .setCategory(NotificationCompat.CATEGORY_SOCIAL) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setDefaults(feedback ? NotificationCompat.DEFAULT_ALL : 0) .setWhen(entity.mWhen) .setColor(ContextCompat.getColor(ctx, R.color.primaryDark)) .setContentIntent(getViewChangePendingIntent(ctx, entity)) .setDeleteIntent(getDeleteGroupNotificationPendingIntent(ctx, entity.mGroupId)) .setOnlyAlertOnce(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); }
private void makeTorrentInfoNotify(String name, String message) { if (name == null || message == null) return; NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID) .setSmallIcon(R.drawable.ic_info_white_24dp) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primary)) .setContentTitle(name) .setTicker(message) .setContentText(message) .setAutoCancel(true) .setWhen(System.currentTimeMillis()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) builder.setCategory(Notification.CATEGORY_STATUS); notifyManager.notify(name.hashCode(), builder.build()); }
private void makeTorrentErrorNotify(String name, String message) { if (name == null || message == null) return; NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID) .setSmallIcon(R.drawable.ic_error_white_24dp) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primary)) .setContentTitle(name) .setTicker(getString(R.string.torrent_error_notify_title)) .setContentText(String.format(getString(R.string.error_template), message)) .setAutoCancel(true) .setWhen(System.currentTimeMillis()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) builder.setCategory(Notification.CATEGORY_ERROR); notifyManager.notify(name.hashCode(), builder.build()); }
private void makeTorrentAddedNotify(String name) { if (name == null) return; String title = getString(R.string.torrent_added_notify); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID) .setSmallIcon(R.drawable.ic_done_white_24dp) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primary)) .setContentTitle(title) .setTicker(title) .setContentText(name) .setAutoCancel(true) .setWhen(System.currentTimeMillis()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) builder.setCategory(Notification.CATEGORY_STATUS); notifyManager.notify(name.hashCode(), builder.build()); }
private void makeNatErrorNotify(String message) { if (message == null) return; NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID) .setSmallIcon(R.drawable.ic_error_white_24dp) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primary)) .setContentTitle(getString(R.string.nat_error_title)) .setTicker(getString(R.string.nat_error_title)) .setContentText(String.format(getString(R.string.error_template), message)) .setAutoCancel(true) .setWhen(System.currentTimeMillis()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) builder.setCategory(Notification.CATEGORY_ERROR); notifyManager.notify(NAT_ERROR_NOTIFICATION_ID, builder.build()); }
private void makeSessionErrorNotify(String message) { if (message == null) return; NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CHAN_ID) .setSmallIcon(R.drawable.ic_error_white_24dp) .setColor(ContextCompat.getColor(getApplicationContext(), R.color.primary)) .setContentTitle(getString(R.string.session_error_title)) .setTicker(getString(R.string.session_error_title)) .setContentText(String.format(getString(R.string.error_template), message)) .setAutoCancel(true) .setWhen(System.currentTimeMillis()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) builder.setCategory(Notification.CATEGORY_ERROR); notifyManager.notify(SESSION_ERROR_NOTIFICATION_ID, builder.build()); }
public void makenoti(String message) { Notification noti = new NotificationCompat.Builder(getApplicationContext(), MainActivity.id) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) //When the event occurred, now, since noti are stored by time. .setContentTitle("Service") //Title message top row. .setContentText(message) //message when looking at the notification, second row .setAutoCancel(true) //allow auto cancel when pressed. .build(); //finally build and return a Notification. //Show the notification nm.notify(NotID, noti); NotID++; } }
@Override public NotificationBuilder setWhen(final long when) { super.setWhen(when); return this; }
public void makenoti(String message) { Notification noti = new NotificationCompat.Builder(getApplicationContext(), MainActivity.id) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) //When the event occurred, now, since noti are stored by time. .setContentTitle("Service") //Title message top row. .setContentText(message) //message when looking at the notification, second row .setAutoCancel(true) //allow auto cancel when pressed. .build(); //finally build and return a Notification. //Show the notification nm.notify(NotID, noti); NotID++; }
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); builder.setWhen(0) .setContentTitle(getResources().getString(R.string.notice_activated)) .setDefaults(NotificationCompat.DEFAULT_LIGHTS)