下面列出了android.content.IntentSender.SendIntentException#android.app.NotificationManager 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 文本消息
*
* @param notifyId 消息ID
* @param title 标题
* @param summary 内容
* @param ticker 出现消息时状态栏的提示文字
* @param pendingIntent 点击后的intent
*/
public static void setMessageNotification(Context context, int notifyId, int smallIconId, CharSequence title, CharSequence summary, CharSequence ticker, PendingIntent pendingIntent) {
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(context, ID_HIGH_CHANNEL);
} else {
builder = new NotificationCompat.Builder(context);
}
builder.setSmallIcon(smallIconId)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.dk_doraemon))
.setContentTitle(title)
.setContentText(summary)
.setAutoCancel(true)
.setProgress(0, 0, false);// Removes the progress bar
if (!TextUtils.isEmpty(ticker)) {
builder.setTicker(ticker);
}
if (pendingIntent != null) {
builder.setContentIntent(pendingIntent);
} else {
builder.setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT));
}
NotificationManager manager = createNotificationManager(context);
manager.notify(notifyId, builder.build());
}
@Override
public void onCreate() {
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(mp -> {
if (musicModeActivity != null) {
musicModeActivity.next_img.performClick();
}
});
myApplication = (App) getApplication();
// handler.sendEmptyMessageDelayed(1002, 200);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
IntentFilter filter = new IntentFilter();
filter.addAction(PAUSE_BROADCAST_NAME);
filter.addAction(NEXT_BROADCAST_NAME);
filter.addAction(PRE_BROADCAST_NAME);
registerReceiver(mConrolBroadcast, filter);
}
/**
* 提醒锻炼通知栏
*/
private void remindNotify() {
//设置点击跳转
Intent hangIntent = new Intent(this, RunActivity.class);
PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
String plan = this.getSharedPreferences("share_date", Context.MODE_MULTI_PROCESS).getString("planWalk_QTY", "7000");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("今日步数" + CURRENT_STEP + " 步")
.setContentText("距离目标还差" + (Integer.valueOf(plan) - CURRENT_STEP) + "步,加油!")
.setContentIntent(hangPendingIntent)
.setTicker(getResources().getString(R.string.app_name) + "提醒您开始锻炼了")//通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示
.setPriority(Notification.PRIORITY_DEFAULT)//设置该通知优先级
.setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.mipmap.logo);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.notify(notify_remind_id, mBuilder.build());
}
@SuppressWarnings("deprecation")
public static void showNotification(Context context, int icon, String text) {
final Notification notifyDetails = new Notification(icon, text, System.currentTimeMillis());
notifyDetails.flags = Notification.FLAG_ONGOING_EVENT;
if (prefsSound(context)) {
notifyDetails.defaults |= Notification.DEFAULT_SOUND;
}
if (prefsVibrate(context)) {
notifyDetails.defaults |= Notification.DEFAULT_VIBRATE;
}
Intent notifyIntent = new Intent(context, adbWireless.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notifyIntent, 0);
notifyDetails.setLatestEventInfo(context, context.getResources().getString(R.string.noti_title), text, intent);
if (Utils.mNotificationManager != null) {
Utils.mNotificationManager.notify(Utils.START_NOTIFICATION_ID, notifyDetails);
} else {
Utils.mNotificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
}
Utils.mNotificationManager.notify(Utils.START_NOTIFICATION_ID, notifyDetails);
}
public void needNotificationPolicy(@NonNull final Activity act) {
if (act.isDestroyed())
return;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return;
}
NotificationManager nm = (NotificationManager) act.getSystemService(Context.NOTIFICATION_SERVICE);
pNotPolicy = nm.isNotificationPolicyAccessGranted();
if (!pNotPolicy) {
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
PackageManager packageManager = act.getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
act.startActivity(intent);
} else {
ActivityCompat.requestPermissions(act, new String[]{Manifest.permission.ACCESS_NOTIFICATION_POLICY}, 0);
}
}
}
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
Context context, String title, String content,
String summary, String channelId, String channelName,
String group, int color) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
return new NotificationCompat.Builder(context.getApplicationContext(), channelId)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(R.drawable.ic_notification)
.setColor(color)
.setStyle(new NotificationCompat.BigTextStyle()
.setSummaryText(summary)
.bigText(content))
.setGroup(group)
.setAutoCancel(true);
}
private void buildNotification(String title, String text, PendingIntent pIntent, int id) {
if (TextUtils.equals("", title) && TextUtils.equals("", text)) {
return;
}
// if you don't use support library, change NotificationCompat on Notification
Notification noti = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.freemp)//change this on your freemp
.setContentIntent(pIntent).build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, noti);
}
@Override
protected void onPause() {
unbindService(mSC);
if (mService != null) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("de.vier_bier.habpanelviewer.status", "Status", NotificationManager.IMPORTANCE_MIN);
channel.enableLights(false);
channel.setSound(null, null);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "de.vier_bier.habpanelviewer.status");
builder.setSmallIcon(R.drawable.logo);
mService.startForeground(42, builder.build());
}
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pauseWebview = prefs.getBoolean(Constants.PREF_PAUSE_WEBVIEW, false);
if (pauseWebview && !((PowerManager) getSystemService(POWER_SERVICE)).isInteractive()) {
mWebView.pause();
}
super.onPause();
}
public void createNotification(Context context, String registrationId) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"Registration successful", System.currentTimeMillis());
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, RegistrationResultActivity.class);
intent.putExtra("registration_id", registrationId);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
notification.setLatestEventInfo(context, "Registration",
"Successfully registered", pendingIntent);
notificationManager.notify(0, notification);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "ReminderReceiver");
//Calendar now = GregorianCalendar.getInstance();
Notification.Builder mBuilder = new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("广播接受者标题,小杨")
.setContentText("广播接受者内容,扯犊子")
.setAutoCancel(true);
Log.i(TAG, "onReceive: intent" + intent.getClass().getName());
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
//将该Activity添加为栈顶
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
public static void lowPredictAlert(Context context, boolean on, String msg) {
final String type = "bg_predict_alert";
if (on) {
if ((Pref.getLong("alerts_disabled_until", 0) < JoH.tsl()) && (Pref.getLong("low_alerts_disabled_until", 0) < JoH.tsl())) {
OtherAlert(context, type, msg, lowPredictAlertNotificationId, NotificationChannels.BG_PREDICTED_LOW_CHANNEL, false, 20 * 60);
if (Pref.getBooleanDefaultFalse("speak_alerts")) {
if (JoH.pratelimit("low-predict-speak", 1800)) SpeechUtil.say(msg, 4000);
}
} else {
Log.ueh(TAG, "Not Low predict alerting due to snooze: " + msg);
}
} else {
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(lowPredictAlertNotificationId);
UserNotification.DeleteNotificationByType(type);
}
}
/**
* {@hide}
*/
public static String importanceToString(int importance) {
switch (importance) {
case NotificationManager.IMPORTANCE_UNSPECIFIED:
return "UNSPECIFIED";
case NotificationManager.IMPORTANCE_NONE:
return "NONE";
case NotificationManager.IMPORTANCE_MIN:
return "MIN";
case NotificationManager.IMPORTANCE_LOW:
return "LOW";
case NotificationManager.IMPORTANCE_DEFAULT:
return "DEFAULT";
case NotificationManager.IMPORTANCE_HIGH:
case NotificationManager.IMPORTANCE_MAX:
return "HIGH";
default:
return "UNKNOWN(" + String.valueOf(importance) + ")";
}
}
private void showNotification(long cancelTime, String text) {
try {
Context app = getContext().getApplicationContext();
NotificationManager nm = (NotificationManager) app
.getSystemService(Context.NOTIFICATION_SERVICE);
final int id = Integer.MAX_VALUE / 13 + 1;
nm.cancel(id);
long when = System.currentTimeMillis();
Notification notification = new Notification(notifyIcon, text, when);
PendingIntent pi = PendingIntent.getActivity(app, 0, new Intent(), 0);
notification.setLatestEventInfo(app, notifyTitle, text, pi);
notification.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notification);
if (cancelTime > 0) {
Message msg = new Message();
msg.what = MSG_CANCEL_NOTIFY;
msg.obj = nm;
msg.arg1 = id;
UIHandler.sendMessageDelayed(msg, cancelTime, this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Put the message into a notification and post it.
* This is just one simple example of what you might choose to do with
* a GCM message.
* @param msg is the message to display in the notification.
*/
private void sendNotification(final String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MobileAssistant GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
@RequiresApi(Build.VERSION_CODES.O)
private void createChannelIdAudioBook() {
//用唯一的ID创建渠道对象
NotificationChannel firstChannel = new NotificationChannel(channelIdAudioBook,
getString(R.string.audio_book),
NotificationManager.IMPORTANCE_LOW);
//初始化channel
firstChannel.enableLights(false);
firstChannel.enableVibration(false);
firstChannel.setSound(null, null);
//向notification manager 提交channel
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.createNotificationChannel(firstChannel);
}
}
/**
* 设置下载进度通知
*
* @param notifyId 消息ID
* @param title 标题
* @param ticker 出现消息时状态栏的提示文字
* @param progress 进度(0-100)
* @param pendingIntent 点击后的intent
*/
public static void setProgressNotification(Context context, int notifyId, CharSequence title, CharSequence ticker, int progress, PendingIntent pendingIntent) {
NotificationCompat.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(context, ID_HIGH_CHANNEL);
} else {
builder = new NotificationCompat.Builder(context);
}
builder.setSmallIcon(android.R.drawable.stat_sys_download)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.dk_doraemon))
.setContentTitle(title)
.setProgress(100, progress, progress == 0)
.setOngoing(progress < 100)
.setAutoCancel(progress == 100);
if (pendingIntent != null) {
builder.setContentIntent(pendingIntent);
} else {
builder.setContentIntent(PendingIntent.getBroadcast(context, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT));
}
if (!TextUtils.isEmpty(ticker)) {
builder.setTicker(ticker);
}
NotificationManager manager = createNotificationManager(context);
manager.notify(notifyId, builder.build());
}
private void addToAdapter() {
List<com.cattong.entity.Status> listNewBlog = adapter.getListNewBlogs();
int cacheSize = listNewBlog.size();
//如果通知已经存在;
if (cacheSize > 0) {
NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.cancel(adapter.getAccount().getAccountId().intValue() * 100 + Skeleton.TYPE_MENTION);
}
if (cacheSize > 0 &&
listNewBlog.get(cacheSize - 1) instanceof LocalStatus
) {
cacheSize--;
}
adapter.refresh();
if (!isAutoUpdate && !isEmptyAdapter) {
Toast.makeText(
adapter.getContext(),
adapter.getContext().getString(R.string.msg_refresh_metion, cacheSize),
Toast.LENGTH_LONG
).show();
}
}
public static void lowPredictAlert(Context context, boolean on, String msg) {
final String type = "bg_predict_alert";
if (on) {
if ((Pref.getLong("alerts_disabled_until", 0) < JoH.tsl()) && (Pref.getLong("low_alerts_disabled_until", 0) < JoH.tsl())) {
OtherAlert(context, type, msg, lowPredictAlertNotificationId, false, 20 * 60);
} else {
Log.ueh(TAG, "Not Low predict alerting due to snooze: " + msg);
}
} else {
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(lowPredictAlertNotificationId);
UserNotification.DeleteNotificationByType(type);
}
}
public static void persistentHighAlert(Context context, boolean on, String msg) {
final String type = "persistent_high_alert";
if (on) {
if ((Pref.getLong("alerts_disabled_until", 0) < JoH.tsl()) && (Pref.getLong("high_alerts_disabled_until", 0) < JoH.tsl())) {
int snooze_time = 20;
try {
snooze_time = Integer.parseInt(Pref.getString("persistent_high_repeat_mins", "20"));
} catch (NumberFormatException e) {
Log.e(TAG, "Invalid snooze time for persistent high");
}
if (snooze_time < 1) snooze_time = 1; // not less than 1 minute
if (snooze_time > 1440) snooze_time = 1440; // not more than 1 day
OtherAlert(context, type, msg, persistentHighAlertNotificationId, NotificationChannels.BG_PERSISTENT_HIGH_CHANNEL, false, snooze_time * 60);
if (Pref.getBooleanDefaultFalse("speak_alerts")) {
if (JoH.pratelimit("persist-high-speak", 1800)) {
SpeechUtil.say(msg, 4000);
}
}
} else {
Log.ueh(TAG, "Not persistent high alerting due to snooze: " + msg);
}
} else {
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.cancel(persistentHighAlertNotificationId);
UserNotification.DeleteNotificationByType(type);
}
}
@Override
public boolean checkPermission(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
return notificationManager.areNotificationsEnabled();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return PermissionUtil.checkOpNoThrow(context, Const.OP_POST_NOTIFICATION);
}
return true;
}
public MyTask(Context context, int target) {
this.mContext = context;
number = target;
//Get the notification manager
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
public static void cancel(Context context,int notificationId){
try {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.NotificationDBHelper mDbHelper = new Notification.NotificationDBHelper(context);
SQLiteDatabase db = mDbHelper.getWritableDatabase();
db.delete(TABLE_NAME, com.allyants.notifyme.Notification.NotificationEntry._ID + " = " + notificationId, null);
db.close();
mNotificationManager.cancel(notificationId);
}catch (Exception e){
e.printStackTrace();
}
}
private void removeNotification(int notificationId) {
if (notificationId != NO_NOTIFICATION) {
NotificationManager notificationManager = (NotificationManager) getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
}
}
private void init() {
mNotificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationBuilder = new NotificationCompat.Builder(mContext);
mNotificationBuilder.setContentTitle(title);
mNotificationBuilder.setContentText(text);
if (bigText == null)
mNotificationBuilder.setContentInfo(text);
if (withLargeIcon()) {
mNotificationBuilder.setSmallIcon(small_icon);
Bitmap icon = BitmapFactory.decodeResource(mContext.getResources(), this.icon);
Bitmap newIcon = Bitmap.createBitmap(icon.getWidth(), icon.getHeight(), icon.getConfig());
Canvas canvas = new Canvas(newIcon);
canvas.drawColor(OResource.color(mContext, R.color.theme_primary));
canvas.drawBitmap(icon, 0, 0, null);
mNotificationBuilder.setLargeIcon(newIcon);
} else {
mNotificationBuilder.setSmallIcon(icon);
}
mNotificationBuilder.setAutoCancel(mAutoCancel);
mNotificationBuilder.setOngoing(mOnGoing);
mNotificationBuilder.setColor(OResource.color(mContext, notification_color));
if (bigText != null) {
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(title);
notiStyle.setSummaryText(text);
notiStyle.bigText(bigText);
mNotificationBuilder.setStyle(notiStyle);
}
if (bigPictureStyle != null) {
mNotificationBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigPictureStyle));
}
if (maxProgress != -1) {
mNotificationBuilder.setProgress(maxProgress, currentProgress, indeterminate);
}
}
public TaskListenerForNotifacation(Context context, DownloadManager manager) {
this.context = context;
this.downloadManager = manager;
notifiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificaions = new ConcurrentHashMap<Long, NotificationInfo>();
contentIntent = new Intent(context, DownLoadActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_ONE_SHOT);
}
public NotifyUtil0(Context context, int ID) {
this.NOTIFICATION_ID = ID;
mContext = context;
// 获取系统服务来初始化对象
nm = (NotificationManager) mContext
.getSystemService(Activity.NOTIFICATION_SERVICE);
cBuilder = new NotificationCompat.Builder(mContext);
}
@Override
public void onReceive(Context context, Intent intent)
{
Bundle msgNotifyBundle = intent.getExtras();
int notifyId = msgNotifyBundle.getInt("notifyId");
if (notifyId != -1) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notifyId);
}
context.startActivity(new Intent(context, QqPausedNotificationActivity.class));
}
@Override
public void onCreate() {
mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mProgressIntent = new Intent(ACTION_UPDATE);
Intent intent = new Intent(this, RebuildCacheService.class);
intent.setAction(ACTION_STOP);
int flag = PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent stop = PendingIntent.getService(this, 0, intent, flag);
intent.setAction(ACTION_SHOW);
PendingIntent show = PendingIntent.getService(this, 0, intent, flag);
mBuilder = createBuilder(this, R.string.rebuild_cache);
int icon = R.drawable.ic_notification_rebuild_cache;
Bitmap largeIcon = NotificationHelper.getLargeIcon(icon, getResources());
mBuilder.setSmallIcon(icon).setLargeIcon(largeIcon);
icon = R.drawable.ic_action_cancel_dark;
mBuilder.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(show)
.addAction(icon, getString(android.R.string.cancel), stop);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String title = getString(R.string.rebuild_cache);
mBuilder.setWhen(System.currentTimeMillis()).setContentTitle(title).setTicker(title);
startForeground(NOTIFICATION_ID, mBuilder.build());
}
mQueue = new LinkedList<>();
}
private static void sendNotification(String body, String title) {
if (Pref.getBooleanDefaultFalse("parakeet_status_alerts")) {
Intent intent = new Intent(xdrip.getAppContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(xdrip.getAppContext(), 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(xdrip.getAppContext(), NotificationChannels.PARAKEET_STATUS_CHANNEL)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(xdrip.getAppContext().getResources(), R.drawable.jamorham_parakeet_marker))
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
// .setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
if (!((PowerStateReceiver.is_power_connected()) && (Pref.getBooleanDefaultFalse("parakeet_charge_silent"))))
{
notificationBuilder.setSound(defaultSoundUri);
}
NotificationManager notificationManager =
(NotificationManager) xdrip.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Notifications.parakeetMissingId);
notificationManager.notify(Notifications.parakeetMissingId, XdripNotificationCompat.build(notificationBuilder));
} else {
Log.d(TAG, "Not sending parakeet notification as they are disabled: " + body);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= 26) {
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setContentText(getString(R.string.oneKeyFreeze));
NotificationChannel channel = new NotificationChannel("OneKeyFreeze", getString(R.string.oneKeyFreeze), NotificationManager.IMPORTANCE_NONE);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null)
notificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("OneKeyFreeze");
startForeground(2, mBuilder.build());
} else {
startForeground(2, new Notification());
}
boolean auto = intent.getBooleanExtra("autoCheckAndLockScreen", true);
String pkgNames = new AppPreferences(getApplicationContext()).getString(getString(R.string.sAutoFreezeApplicationList), "");
if (pkgNames != null) {
if (Build.VERSION.SDK_INT >= 21 && isDeviceOwner(getApplicationContext())) {
oneKeyActionMRoot(this, true, pkgNames.split(","));
checkAuto(auto, this);
} else {
oneKeyActionRoot(this, true, pkgNames.split(","));
checkAuto(auto, this);
}
}
return super.onStartCommand(intent, flags, startId);
}