use of android.app.NotificationChannel in project network-monitor by caarmen.
the class NetMonNotification method createAlertNotificationChannel.
private static String createAlertNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
NotificationChannel notificationChannel = new NotificationChannel(context.getString(R.string.service_alert_notification_channel_id), context.getString(R.string.service_alert_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription(context.getString(R.string.service_alert_notification_channel_description));
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ActivityCompat.getColor(context, R.color.netmon_color));
notificationManager.createNotificationChannel(notificationChannel);
return notificationChannel.getId();
}
}
return "";
}
use of android.app.NotificationChannel in project GeometricWeather by WangDaYeeeeee.
the class ForecastNotificationUtils method buildForecastAndSendIt.
public static void buildForecastAndSendIt(Context context, Weather weather, boolean today) {
if (weather == null) {
return;
}
LanguageUtils.setLanguage(context, GeometricWeather.getInstance().getLanguage());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean fahrenheit = sharedPreferences.getBoolean(context.getString(R.string.key_fahrenheit), false);
String iconStyle = sharedPreferences.getString(context.getString(R.string.key_notification_icon_style), "material");
boolean tempIcon = sharedPreferences.getBoolean(context.getString(R.string.key_notification_temp_icon), false);
boolean backgroundColor = sharedPreferences.getBoolean(context.getString(R.string.key_notification_background), false);
boolean hideNotificationIcon = sharedPreferences.getBoolean(context.getString(R.string.key_notification_hide_icon), false);
String textColor = sharedPreferences.getString(context.getString(R.string.key_notification_text_color), "grey");
int mainColor;
int subColor;
switch(textColor) {
case "dark":
mainColor = ContextCompat.getColor(context, R.color.colorTextDark);
subColor = ContextCompat.getColor(context, R.color.colorTextDark2nd);
break;
case "grey":
mainColor = ContextCompat.getColor(context, R.color.colorTextGrey);
subColor = ContextCompat.getColor(context, R.color.colorTextGrey2nd);
break;
case "light":
default:
mainColor = ContextCompat.getColor(context, R.color.colorTextLight);
subColor = ContextCompat.getColor(context, R.color.colorTextLight2nd);
break;
}
// create channel.
NotificationManager manager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
if (manager == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
manager.createNotificationChannel(new NotificationChannel(CHANNEL_ID_FORECAST, context.getString(R.string.app_name) + " " + context.getString(R.string.forecast), hideNotificationIcon ? NotificationManager.IMPORTANCE_MIN : NotificationManager.IMPORTANCE_DEFAULT));
}
// get builder.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID_FORECAST);
// set notification level.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && hideNotificationIcon) {
builder.setPriority(NotificationCompat.PRIORITY_MIN);
} else {
builder.setPriority(NotificationCompat.PRIORITY_MAX);
}
// set notification visibility.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (sharedPreferences.getBoolean(context.getString(R.string.key_notification_hide_in_lockScreen), false)) {
builder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
} else {
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
}
}
// set small icon.
int iconTemp = today ? weather.dailyList.get(0).temps[0] : weather.dailyList.get(1).temps[0];
builder.setSmallIcon(tempIcon ? ValueUtils.getTempIconId(fahrenheit ? ValueUtils.calcFahrenheit(iconTemp) : iconTemp) : WeatherHelper.getNotificationWeatherIcon(today ? weather.dailyList.get(0).weatherKinds[0] : weather.dailyList.get(1).weatherKinds[0], true));
// set view
RemoteViews view = new RemoteViews(context.getPackageName(), R.layout.notification_forecast);
// set icon.
int[] imageIds = new int[2];
if (today) {
imageIds[0] = WeatherHelper.getWidgetNotificationIcon(weather.dailyList.get(0).weatherKinds[0], true, iconStyle, textColor);
imageIds[1] = WeatherHelper.getWidgetNotificationIcon(weather.dailyList.get(0).weatherKinds[1], false, iconStyle, textColor);
} else {
imageIds[0] = WeatherHelper.getWidgetNotificationIcon(weather.dailyList.get(0).weatherKinds[0], true, iconStyle, textColor);
imageIds[1] = WeatherHelper.getWidgetNotificationIcon(weather.dailyList.get(1).weatherKinds[0], true, iconStyle, textColor);
}
view.setImageViewResource(R.id.notification_forecast_icon_1, imageIds[0]);
view.setImageViewResource(R.id.notification_forecast_icon_2, imageIds[1]);
// set title.
String[] titles = new String[2];
if (today) {
titles[0] = context.getString(R.string.day) + " " + ValueUtils.buildCurrentTemp(weather.dailyList.get(0).temps[0], false, fahrenheit);
titles[1] = context.getString(R.string.night) + " " + ValueUtils.buildCurrentTemp(weather.dailyList.get(0).temps[1], false, fahrenheit);
} else {
titles[0] = context.getString(R.string.today) + " " + ValueUtils.buildCurrentTemp(weather.dailyList.get(0).temps[0], false, fahrenheit);
titles[1] = context.getString(R.string.tomorrow) + " " + ValueUtils.buildCurrentTemp(weather.dailyList.get(1).temps[0], false, fahrenheit);
}
view.setTextViewText(R.id.notification_forecast_title_1, titles[0]);
view.setTextViewText(R.id.notification_forecast_title_2, titles[1]);
// set content.
String[] contents = new String[2];
if (today) {
contents[0] = weather.dailyList.get(0).weathers[0];
contents[1] = weather.dailyList.get(0).weathers[1];
} else {
contents[0] = weather.dailyList.get(0).weathers[0];
contents[1] = weather.dailyList.get(1).weathers[0];
}
view.setTextViewText(R.id.notification_forecast_content_1, contents[0]);
view.setTextViewText(R.id.notification_forecast_content_2, contents[1]);
// set background.
if (backgroundColor) {
view.setViewVisibility(R.id.notification_forecast_background, View.VISIBLE);
} else {
view.setViewVisibility(R.id.notification_forecast_background, View.GONE);
}
// set text color.
view.setTextColor(R.id.notification_forecast_title_1, mainColor);
view.setTextColor(R.id.notification_forecast_title_2, mainColor);
view.setTextColor(R.id.notification_forecast_content_1, subColor);
view.setTextColor(R.id.notification_forecast_content_2, subColor);
builder.setContent(view);
// set intent.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, IntentHelper.buildMainActivityIntent(context, null), 0);
builder.setContentIntent(pendingIntent);
// set sound & vibrate.
builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
builder.setAutoCancel(true);
// set badge.
builder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
// commit.
manager.notify(NOTIFICATION_ID, builder.build());
}
use of android.app.NotificationChannel in project MusicLibrary by lizixian18.
the class CustomNotification method createNotificationChannel.
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel() {
if (mNotificationManager.getNotificationChannel(CHANNEL_ID) == null) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, mService.getString(R.string.notification_channel), NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription(mService.getString(R.string.notification_channel_description));
mNotificationManager.createNotificationChannel(notificationChannel);
}
}
use of android.app.NotificationChannel in project AppCenter-SDK-Android by Microsoft.
the class Distribute method notifyDownload.
/**
* Post notification about a completed download if we are in background when download completes.
* If this method is called on app process restart or if application is in foreground
* when download completes, it will not notify and return that the install U.I. should be shown now.
*
* @param releaseDetails release details to check state.
* @param intent prepared install intent.
* @return false if install U.I should be shown now, true if a notification was posted or if the task was canceled.
*/
synchronized boolean notifyDownload(ReleaseDetails releaseDetails, Intent intent) {
/* Check state. */
if (releaseDetails != mReleaseDetails) {
return true;
}
/*
* If we already notified, that means this check was triggered by application being resumed,
* thus in foreground at the moment the check download async task was started.
*
* We should not hold the install any longer now, even if the async task was long enough
* for app to be in background again, we should show install U.I. now.
*/
if (mForegroundActivity != null || getStoredDownloadState() == DOWNLOAD_STATE_NOTIFIED) {
return false;
}
/* Post notification. */
AppCenterLog.debug(LOG_TAG, "Post a notification as the download finished in background.");
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
/* Create or update notification channel (mandatory on Android 8 target). */
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, mContext.getString(R.string.appcenter_distribute_notification_category), NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
builder = new Notification.Builder(mContext, NOTIFICATION_CHANNEL_ID);
} else {
builder = getOldNotificationBuilder();
}
builder.setTicker(mContext.getString(R.string.appcenter_distribute_install_ready_title)).setContentTitle(mContext.getString(R.string.appcenter_distribute_install_ready_title)).setContentText(getInstallReadyMessage()).setSmallIcon(mContext.getApplicationInfo().icon).setContentIntent(PendingIntent.getActivities(mContext, 0, new Intent[] { intent }, 0));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setStyle(new Notification.BigTextStyle().bigText(getInstallReadyMessage()));
}
Notification notification = DistributeUtils.buildNotification(builder);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(DistributeUtils.getNotificationId(), notification);
PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
/* Reset check download flag to show install U.I. on resume if notification ignored. */
mCheckedDownload = false;
return true;
}
use of android.app.NotificationChannel in project Zom-Android by zom.
the class ImApp method initChannel.
public void initChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel nc = new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, getString(R.string.app_name), NotificationManager.IMPORTANCE_MIN);
nc.setShowBadge(false);
nm.createNotificationChannel(nc);
nc = new NotificationChannel(NOTIFICATION_CHANNEL_ID_MESSAGE, getString(R.string.notifications), NotificationManager.IMPORTANCE_HIGH);
nc.setLightColor(0xff990000);
nc.enableLights(true);
nc.enableVibration(false);
nc.setShowBadge(true);
nc.setSound(null, null);
nm.createNotificationChannel(nc);
}
}
Aggregations