/** * 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()); }
@Override protected void buildNotification(Context context, NotificationCompat.Builder notification, int notificationId) { super.buildNotification(context, notification, notificationId); notification.setPriority(NotificationCompat.PRIORITY_DEFAULT); notification.setContentTitle(context.getString(R.string.connection_error_command_title)); notification.setContentText(context.getString(R.string.connection_error_command_notification_desc)); notification.setContentIntent(PendingIntent.getActivity(context, notificationId, MainActivity.getLaunchIntent(context, null, null), PendingIntent.FLAG_CANCEL_CURRENT)); }
protected void buildNotification(Context context, NotificationCompat.Builder notification, int notificationId) { notification.setSmallIcon(R.drawable.ic_warning); notification.setPriority(NotificationCompat.PRIORITY_HIGH); notification.setContentTitle(context.getString(R.string.notification_action_required)); }
.setPriority(messagingStyleCommsAppData.getPriority())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) .setCategory(Notification.CATEGORY_REMINDER) .setPriority(bigTextStyleReminderAppData.getPriority()) .setVisibility(bigTextStyleReminderAppData.getChannelLockscreenVisibility()) .addAction(snoozeAction)
.addAction(replyAction) .setCategory(Notification.CATEGORY_SOCIAL) .setPriority(bigPictureStyleSocialAppData.getPriority()) .setVisibility(bigPictureStyleSocialAppData.getChannelLockscreenVisibility()) .extend(new NotificationCompat.WearableExtender()
.addAction(replyAction) .setCategory(Notification.CATEGORY_SOCIAL) .setPriority(Notification.PRIORITY_HIGH) .setVisibility(Notification.VISIBILITY_PRIVATE);
.setAutoCancel(true) .setDefaults(defaultFlags) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_EVENT) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
.setPriority(inboxStyleEmailAppData.getPriority())
.setContentIntent(intent) .setAutoCancel(true) .setPriority(rule.settings.priority + 1) .setSmallIcon(R.drawable.ic_notification_message) .setCustomContentView(notificationsView)
.setColor(getResources().getColor(R.color.colorPrimary)) .setCategory(Notification.CATEGORY_REMINDER) .setPriority(Notification.PRIORITY_HIGH) .setVisibility(Notification.VISIBILITY_PUBLIC) .addAction(snoozeAction)
.setContentTitle(getString(R.string.service_title)) .setContentText(b.toString()) .setPriority(NotificationCompat.PRIORITY_MIN) .setOnlyAlertOnce(true) .setContentIntent(PendingIntent.getActivity(this, IDLE_NOTIFICATION_ID, mainIntent, PendingIntent.FLAG_CANCEL_CURRENT))
/** * Show notification * * @param mId Notification Id */ @SuppressWarnings("SameParameterValue") private Notification showNotification(int mId) { if (Logger.DEBUG) { Log.d(TAG, "[showNotification " + mId + "]"); } final String channelId = String.valueOf(mId); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { createNotificationChannel(channelId); } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_stat_notify_24dp) .setContentTitle(getString(R.string.app_name)) .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_SERVICE) .setContentText(String.format(getString(R.string.is_running), getString(R.string.app_name))); Intent resultIntent = new Intent(this, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); Notification mNotification = mBuilder.build(); mNotificationManager.notify(mId, mNotification); return mNotification; }
/** * Refresh the state of the sleepy stuff. This will either show a notification if a notification * should be shown, or stop the service if it shouldn't. */ public void refreshState() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? powerManager.isInteractive() : powerManager.isScreenOn()) { AlarmData nextAlarm = getSleepyAlarm(alarmio); if (nextAlarm != null) { NotificationCompat.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (manager != null) manager.createNotificationChannel(new NotificationChannel("sleepReminder", getString(R.string.title_sleep_reminder), NotificationManager.IMPORTANCE_DEFAULT)); builder = new NotificationCompat.Builder(this, "sleepReminder"); } else builder = new NotificationCompat.Builder(this); startForeground(540, builder.setContentTitle(getString(R.string.title_sleep_reminder)) .setContentText(String.format(getString(R.string.msg_sleep_reminder), FormatUtils.formatUnit(this, (int) TimeUnit.MILLISECONDS.toMinutes(nextAlarm.getNext().getTimeInMillis() - System.currentTimeMillis())))) .setSmallIcon(R.drawable.ic_notification_sleep) .setPriority(NotificationCompat.PRIORITY_LOW) .build()); return; } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) stopSelf(); stopForeground(true); }
@TargetApi(Build.VERSION_CODES.N) private static void createSummaryGroupNotification( Context ctx, Account account, Set<String> notifications, boolean feedback) { int notificationId = FowlerNollVo.fnv1_32(account.getAccountHash().getBytes()).intValue(); NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, account.getAccountHash()) .setContentTitle(getNotificationSubText(account)) .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) .setColor(ContextCompat.getColor(ctx, R.color.primaryDark)) .setContentIntent( getViewAccountChangesPendingIntent(ctx, account, notificationId)) .setDeleteIntent( getDeleteAccountNotificationPendingIntent(ctx, account, notificationId)) .setOnlyAlertOnce(true) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC); NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle() .setSummaryText(getNotificationSubText(account)); builder.setStyle(style) .setNumber(notifications.size()) .setGroupSummary(true) .setGroup(NOTIFICATION_KEY_GROUP + account.getAccountHash()); publishNotification(ctx, builder.build(), notificationId); }
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 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 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(); } }
@Override public NotificationBuilder setPriority(final int pri) { super.setPriority(pri); mPriority = pri; return this; }