下面列出了android.content.IntentSender.SendIntentException#android.app.Notification 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification postedNotification = sbn.getNotification();
if (postedNotification.tickerText == null ||
sbn.isOngoing() || !sbn.isClearable() ||
isInBlackList(sbn)) {
return;
}
if (mAppList.isInQuietHour(sbn.getPostTime())) {
// The first notification arrived during quiet hour will unregister all sensor listeners.
mNotificationPeek.unregisterEventListeners();
return;
}
mNotificationHub.addNotification(sbn);
if (AccessChecker.isDeviceAdminEnabled(this)) {
mNotificationPeek
.showNotification(sbn, false, mPeekTimeoutMultiplier, mSensorTimeoutMultiplier,
mShowContent);
}
}
private void createBaseNotify(Intent intent, String title, String content, int notifyId) {
Notification.Builder builder =
getMsgNotificationBuilder().setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setOngoing(false);
if (!StringUtils.isEmpty(content)) {
builder.setContentText(content);
}
PendingIntent pendingIntent =
PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
mNotificationManager.notify(notifyId, notification);
}
private ProgressInfo verifyNotification(StatusBarNotification statusBarNotif) {
if (statusBarNotif == null)
return null;
String id = getIdentifier(statusBarNotif);
if (id == null)
return null;
Notification n = statusBarNotif.getNotification();
if (n != null &&
(SUPPORTED_PACKAGES.contains(statusBarNotif.getPackageName()) ||
n.extras.getBoolean(ModLedControl.NOTIF_EXTRA_PROGRESS_TRACKING))) {
ProgressInfo pi = getProgressInfo(id, n);
if (pi != null && pi.hasProgressBar)
return pi;
}
return null;
}
private void postDownloadedNotification(int i, String name, File puzFile) {
try {
String contentTitle = "Downloaded " + name;
Intent notificationIntent = new Intent(Intent.ACTION_EDIT,
Uri.fromFile(puzFile), context, PlayActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification not = new NotificationCompat.Builder(context, ShortyzApplication.PUZZLE_DOWNLOAD_CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentTitle(contentTitle)
.setContentText(puzFile.getName())
.setContentIntent(contentIntent)
.setWhen(System.currentTimeMillis())
.build();
if (this.notificationManager != null) {
this.notificationManager.notify(i, not);
}
} catch(Exception e){
e.printStackTrace();
}
}
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();
}
}
void updateNotifyModeAndPostNotification(Notification notification) {
int newNotifyMode;
if (service.isPlaying()) {
newNotifyMode = NOTIFY_MODE_FOREGROUND;
} else {
newNotifyMode = NOTIFY_MODE_BACKGROUND;
}
if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) {
service.stopForeground(false);
}
if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
service.startForeground(NOTIFICATION_ID, notification);
} else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
notificationManager.notify(NOTIFICATION_ID, notification);
}
notifyMode = newNotifyMode;
}
@TargetApi(26)
private synchronized void createNotificationChannels() {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
Log.e(TAG, "Could not create notification channels. NotificationManager is null");
stopSelf();
}
NotificationChannel statusChannel = new NotificationChannel("status", "Status", NotificationManager.IMPORTANCE_LOW);
statusChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(statusChannel);
NotificationChannel errorChannel = new NotificationChannel("error", "Errors", NotificationManager.IMPORTANCE_HIGH);
errorChannel.enableLights(true);
errorChannel.setLightColor(Color.RED);
errorChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(errorChannel);
}
@Override
public void onCreate() {
super.onCreate();
NotificationManager service = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = createNotificationChannel();
}
Notification notification = new NotificationCompat.Builder(MainApplication.mainApplication, channelId)
.setContentTitle("前台任务")
.setContentText("正在下载")
.setSmallIcon(R.mipmap.download_default)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.download_default)).build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static Notification createNotification(Context c) {
NotificationManager nm = c.getSystemService(NotificationManager.class);
String channelId = "foreground";
if (nm.getNotificationChannel(channelId) == null) {
nm.createNotificationChannel(new NotificationChannel(channelId, c.getString(R.string.appName), NotificationManager.IMPORTANCE_MIN));
}
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, c.getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(c, channelId);
builder.setContentIntent(pendingIntent);
builder.setSmallIcon(R.drawable.ic_abicon);
builder.setContentText(c.getString(R.string.clickToDisableNotification));
builder.setPriority(NotificationCompat.PRIORITY_MIN);
builder.setWhen(0); //show as last
return builder.build();
}
@Override
protected void onHandleIntent(Intent workIntent) {
Intent notificationIntent = new Intent(this, ClientCertificateActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_GEN)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.generate_key_working))
.setContentTitle(getString(R.string.generate_key_working_notification))
.setPriority(NotificationCompat.PRIORITY_LOW)
.setContentIntent(pendingIntent)
.build();
startForeground(NOTIFICATION_ID_GEN, notification);
RsaHelper.initialiseRsaKeyAndCert(getApplicationContext());
currentlyGenerating = false;
stopForeground(true);
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_ACTION));
}
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createNotificationChannels(Context appCtx) {
try {
final NotificationManager notificationManager = (NotificationManager) appCtx.getSystemService(Context.NOTIFICATION_SERVICE);
// Notification with sound channel
CharSequence name = appCtx.getString(R.string.export_notification);
String description = appCtx.getString(R.string.export_notification_description);
NotificationChannel channel = new NotificationChannel("export_sound", name, NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(description);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
} catch (Exception e)
{
e.printStackTrace();
}
}
private void buildNotification(int resId,String tiile,String contenttext){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,UNLOCK_NOTIFICATION_CHANNEL_ID);
// 必需的通知内容
builder.setContentTitle(tiile)
.setContentText(contenttext)
.setSmallIcon(resId);
Intent notifyIntent = new Intent(this, MediaProjectionActivity.class);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(notifyPendingIntent);
Notification notification = builder.build();
//常驻状态栏的图标
//notification.icon = resId;
// 将此通知放到通知栏的"Ongoing"即"正在运行"组中
notification.flags |=Notification.FLAG_ONGOING_EVENT;
// 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_NO_CLEAR;
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID_ICON, notification);
startForeground(NOTIFICATION_ID_ICON, notification);
}
/**
* 折叠式
*
* @param context
*/
public static void shwoNotify(Context context,int flag) {
//先设定RemoteViews
RemoteViews view_custom = new RemoteViews(context.getPackageName(), R.layout.notification);
//设置对应IMAGEVIEW的ID的资源图片
view_custom.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
view_custom.setTextViewText(R.id.bbs_type_Title, "今日头条");
view_custom.setTextColor(R.id.text, Color.BLACK);
view_custom.setTextViewText(R.id.text, "金州勇士官方宣布球队已经解雇了主帅马克-杰克逊,随后宣布了最后的结果。");
view_custom.setTextColor(R.id.bbs_type_Title, Color.BLACK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContent(view_custom)
.setContentIntent(PendingIntent.getActivity(context, 4, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
.setTicker("有新资讯")
.setPriority(Notification.PRIORITY_HIGH)// 设置该通知优先级
.setOngoing(false)//不是正在进行的 true为正在进行 效果和.flag一样
.setSmallIcon(R.mipmap.ic_launcher);
Notification notify = mBuilder.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notificationManager.notify(flag, notify);
}
/**
* 通知経由でのActivity起動を行う(Pending Intent)
* @param context コンテキスト
* @param notificationId 通知のID
* @param requestCode Activity起動のリクエストコード
* @param intent インテント
* @param contentText 通知に表示するテキスト
*/
public static void notify(Context context, int notificationId, int requestCode, Intent intent, String contentText) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if(notificationManager != null) {
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentText(contentText)
.setContentTitle(NOTIFICATION_CONTENT_TITLE)
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_action_labels)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_labels))
.setStyle(new Notification.BigTextStyle().setBigContentTitle(NOTIFICATION_CONTENT_TITLE).bigText(contentText))
.setContentIntent(pendingIntent)
.build();
notificationManager.notify(notificationId, notification);
}
}
private Notification createNotification(DeviceConnection devConn, boolean connected) {
String ticker;
String message;
if (connected) {
ticker = "Connection Established";
message = "Connected to "+getConnectionString(devConn);
}
else {
ticker = "Connection Terminated";
message = "Connection to "+getConnectionString(devConn)+" failed";
}
return new NotificationCompat.Builder(getApplicationContext())
.setTicker("Remote ADB Shell - "+ticker)
.setSmallIcon(R.drawable.notificationicon)
.setOnlyAlertOnce(true)
.setOngoing(connected)
.setAutoCancel(!connected)
.setContentTitle("Remote ADB Shell")
.setContentText(message)
.setContentIntent(createPendingIntentForConnection(devConn))
.build();
}
@Override
protected void onHandleIntent( Intent intent ) {
NotificationCompat.Builder builder = new NotificationCompat.Builder( this );
builder.setSmallIcon( R.drawable.ic_launcher );
builder.setDefaults( Notification.DEFAULT_ALL );
builder.setOngoing( true );
int transitionType = LocationClient.getGeofenceTransition( intent );
if( transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ) {
builder.setContentTitle( "Geofence Transition" );
builder.setContentText( "Entering Geofence" );
mNotificationManager.notify( 1, builder.build() );
}
else if( transitionType == Geofence.GEOFENCE_TRANSITION_EXIT ) {
builder.setContentTitle( "Geofence Transition" );
builder.setContentText( "Exiting Geofence" );
mNotificationManager.notify( 1, builder.build() );
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_kolabnotes_breeze)
.setContentTitle(context.getResources().getString(R.string.imported))
.setStyle(new NotificationCompat.BigTextStyle().bigText(s))
.setAutoCancel(true).build();
notificationManager.notify(0, notification);
reloadData();
}
@Override
public void onCreate() {
super.onCreate();
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) || new AppPreferences(getApplicationContext()).getBoolean("useForegroundService", false)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder mBuilder = new Notification.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_notification);
mBuilder.setContentText(getString(R.string.backgroundService));
NotificationChannel channel = new NotificationChannel("BackgroundService", getString(R.string.backgroundService), NotificationManager.IMPORTANCE_NONE);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null)
notificationManager.createNotificationChannel(channel);
mBuilder.setChannelId("BackgroundService");
Intent resultIntent = new Intent(getApplicationContext(), Main.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
startForeground(1, mBuilder.build());
} else {
startForeground(1, new Notification());
}
}
}
private Notification notification() {
Intent intent = new Intent(mContext, Home.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(Home.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder b=new NotificationCompat.Builder(mService);
b.setOngoing(true);
b.setCategory(Notification.CATEGORY_SERVICE);
// Hide this notification "below the fold" on L+
b.setPriority(Notification.PRIORITY_MIN);
// Don't show this notification on the lock screen on L+
b.setVisibility(Notification.VISIBILITY_SECRET);
b.setContentTitle("xDrip is Running")
.setContentText("xDrip Data collection service is running.")
.setSmallIcon(R.drawable.ic_action_communication_invert_colors_on);
b.setContentIntent(resultPendingIntent);
return(b.build());
}
/**
* 更新通知
*/
private void updateNotification(int state, String msg) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, MApplication.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_network_check)
.setOngoing(true)
.setContentTitle(getString(R.string.check_book_source))
.setContentText(msg)
.setContentIntent(getActivityPendingIntent());
builder.addAction(R.drawable.ic_stop_black_24dp, getString(R.string.cancel), getThisServicePendingIntent());
if (recommendIndexBeanList != null) {
builder.setProgress(recommendIndexBeanList.size(), state, false);
}
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
Notification notification = builder.build();
startForeground(notificationId, notification);
}
private void showSuccessfulNotification(final WeiboSendTask task) {
Notification.Builder builder = new Notification.Builder(SendRepostService.this)
.setTicker(getString(R.string.send_successfully))
.setContentTitle(getString(R.string.send_successfully)).setOnlyAlertOnce(true).setAutoCancel(true)
.setSmallIcon(R.drawable.send_successfully).setOngoing(false);
Notification notification = builder.getNotification();
final int id = tasksNotifications.get(task);
NotificationUtility.show(notification, id);
handler.postDelayed(new Runnable() {
@Override
public void run() {
NotificationUtility.cancel(id);
stopServiceIfTasksAreEnd(task);
}
}, 3000);
LocalBroadcastManager.getInstance(SendRepostService.this).sendBroadcast(
new Intent(AppEventAction.buildSendRepostSuccessfullyAction(oriMsg)));
}
@Override
public WearableExtender clone() {
WearableExtender that = new WearableExtender();
that.mActions = new ArrayList<Action>(this.mActions);
that.mFlags = this.mFlags;
that.mDisplayIntent = this.mDisplayIntent;
that.mPages = new ArrayList<Notification>(this.mPages);
that.mBackground = this.mBackground;
that.mContentIcon = this.mContentIcon;
that.mContentIconGravity = this.mContentIconGravity;
that.mContentActionIndex = this.mContentActionIndex;
that.mCustomSizePreset = this.mCustomSizePreset;
that.mCustomContentHeight = this.mCustomContentHeight;
that.mGravity = this.mGravity;
return that;
}
private RemoteViews addButtonToNotification(Notification n) {
// Create a new RV with a Stop button.
RemoteViews rv = new RemoteViews(
getPackageName(), R.layout.notification_with_stop_button);
PendingIntent pendingIntent = PendingIntent.getService(
this,
0,
newStopIntent(this, R.string.stop_reason_notification),
PendingIntent.FLAG_CANCEL_CURRENT);
rv.setOnClickPendingIntent(R.id.stop_button, pendingIntent);
// Pre-render the original RV, and copy some of the colors.
RemoteViews oldRV = getContentView(this, n);
final View inflated = oldRV.apply(this, new FrameLayout(this));
final TextView titleText = findTextView(inflated, getString(R.string.app_name));
final TextView defaultText = findTextView(inflated, getString(R.string.notification_text));
rv.setInt(R.id.divider, "setBackgroundColor", defaultText.getTextColors().getDefaultColor());
rv.setInt(R.id.stop_button_square, "setBackgroundColor", titleText.getTextColors().getDefaultColor());
// Insert a copy of the original RV into the new one.
rv.addView(R.id.notification_insert, oldRV.clone());
return rv;
}
public static Notification getNotification(Context context, PendingIntent pendingIntent, ClsTrack track, boolean isPlaying) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
notificationBuilder
.setAutoCancel(true)
.setSmallIcon(R.drawable.freemp)
.setContentTitle(track != null ? track.getArtist() : "")
.setContentText(track != null ? track.getTitle() : "");
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
notification.contentIntent = pendingIntent;
if (track != null) {
notification.contentView = getNotificationViews(track, context, isPlaying, R.layout.notification);
} else {
//notification.contentView = null;
//notification.setLatestEventInfo(context, "", "", pendingIntent);
notification = null;
}
return notification;
}
private void handleMessageWithTemplate( final Context context, Intent intent, AndroidPushTemplate androidPushTemplate, final int notificationId )
{
Bundle newBundle = PushTemplateHelper.prepareMessageBundle( intent.getExtras(), androidPushTemplate, notificationId );
Intent newMsgIntent = new Intent();
newMsgIntent.putExtras( newBundle );
if( !this.onMessage( context, newMsgIntent ) )
return;
if( androidPushTemplate.getContentAvailable() != null && androidPushTemplate.getContentAvailable() == 1 )
return;
Notification notification = PushTemplateHelper.convertFromTemplate( context, androidPushTemplate, newBundle, notificationId );
PushTemplateHelper.showNotification( context, notification, androidPushTemplate.getName(), notificationId );
}
void showNotification(String notification, Intent intent){
PendingIntent pendingIntent = PendingIntent.getActivity(
ctx,
NOTIFICATION_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx,"Nirbheek");
Notification mNotification = builder.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentText(notification)
.setContentTitle("Nirbheek")
.build();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotification.defaults |= Notification.DEFAULT_SOUND;
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, mNotification);
}
@Override
public void onCreate(){
super.onCreate();
if(callIShouldHavePutIntoIntent!=null && Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationsController.checkOtherNotificationsChannel();
Notification.Builder bldr=new Notification.Builder(this, NotificationsController.OTHER_NOTIFICATIONS_CHANNEL)
.setSmallIcon(R.drawable.notification)
.setContentTitle(LocaleController.getString("VoipOutgoingCall", R.string.VoipOutgoingCall))
.setShowWhen(false);
startForeground(ID_ONGOING_CALL_NOTIFICATION, bldr.build());
}
}
private void createOptInNotification(boolean highPriority) {
PendingIntent pendingIntent = createOptInIntent();
int priority = highPriority ? NotificationCompat.PRIORITY_HIGH
: NotificationCompat.PRIORITY_MIN;
// Get values to display.
Resources resources = mContext.getResources();
String title = resources.getString(R.string.physical_web_optin_notification_title);
String text = resources.getString(R.string.physical_web_optin_notification_text);
Bitmap largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.app_icon);
// Create the notification.
Notification notification = new NotificationCompat.Builder(mContext)
.setLargeIcon(largeIcon)
.setSmallIcon(R.drawable.ic_physical_web_notification)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(pendingIntent)
.setPriority(priority)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setAutoCancel(true)
.setLocalOnly(true)
.build();
mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID_PHYSICAL_WEB,
notification);
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Notification notification = NotificationHelper.getInstance(context)
.sendDownloadProgressNotification(title, soFarBytes - soFar, totalBytes - total, false, false);
DownloadHelper.getInstance().setForeground(notification);
soFar = soFarBytes;
total = totalBytes;
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
YTConfig config = (YTConfig) intent.getSerializableExtra("addJob");
pendingJobs.add(config);
/** Setting Notification */
notificationManagerCompat = NotificationManagerCompat.from(context);
notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setContentTitle("Download")
.addAction(R.mipmap.ic_launcher, "Cancel", cancelIntent)
.setContentText(pendingJobs.size() + " files downloading")
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentIntent(contentIntent)
.setOngoing(true)
.setPriority(Notification.PRIORITY_LOW)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(FOREGROUND_ID, notification);
} else {
notificationManagerCompat.notify(FOREGROUND_ID, notification);
}
return super.onStartCommand(intent, flags, startId);
}