下面列出了android.app.Notification#FLAG_AUTO_CANCEL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Notification getServiceNotification(Context context) {
Notification.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification.Builder(context, mChannelServiceId);
} else {
builder = new Notification.Builder(context);
}
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, new Intent(context, HomeActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(StringUtils.getTextFromResId(R.string.service_notify_title))
.setContentText(StringUtils.getTextFromResId(R.string.service_notify_content))
.setContentIntent(pendingIntent)
.build();
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
return notification;
}
private static void sendChatNotificationEvent(Context context, Intent intent, NotificationEvent notificationEvent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannelIfNotExist(notificationManager);
}
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ONE_ID)
.setSmallIcon(getNotificationIcon())
.setContentTitle(notificationEvent.getTitle())
.setColor(context.getResources().getColor(R.color.accent))
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationEvent.getSubject()))
.setContentText(notificationEvent.getBody())
.setAutoCancel(true)
.setContentIntent(contentIntent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
notificationManager.notify(NOTIFICATION_ID, notification);
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
// notification.defaults |= Notification.DEFAULT_SOUND;
// notification.sound = Uri.parse("android.resource://" +
// context.getPackageName() + "your_sound_file_name.mp3");
// Vibrate if vibrate is enabled
// notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
/**
* 显示下载完成的通知,点击进行安装
*
* @param context 上下文
* @param icon 图标
* @param title 标题
* @param content 内容
* @param authorities Android N 授权
* @param apk 安装包
*/
public static void showDoneNotification(Context context, int icon, String title, String content,
String authorities, File apk) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//不知道为什么需要先取消之前的进度通知,才能显示完成的通知。
manager.cancel(requireManagerNotNull().getNotifyId());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(context, authorities, apk);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
uri = Uri.fromFile(apk);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = builderNotification(context, icon, title, content)
.setContentIntent(pi);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(requireManagerNotNull().getNotifyId(), notification);
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message, String type, int activity_num) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
if (type.equals("ODM"))
notificationIntent.setData(Uri.parse("https://github.com/Fmstrat/odm/raw/master/latest/odm.apk"));
else
notificationIntent.setData(Uri.parse("https://github.com/Fmstrat/odm-web"));
PendingIntent intent = PendingIntent.getActivity(context, activity_num, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(activity_num, notification);
}
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);
}
/**
* Show Notification
*/
public void showNotification(String content, String apkUrl) {
Notification noti;
Intent myIntent = new Intent(mContext, DownloadService.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra(Constants.APK_DOWNLOAD_URL, apkUrl);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int smallIcon = mContext.getApplicationInfo().icon;
noti = new NotificationCompat.Builder(mContext)
.setTicker(getString(R.string.newUpdateAvailable))
.setContentTitle(getString(R.string.newUpdateAvailable))
.setContentText(content).setSmallIcon(smallIcon)
.setContentIntent(pendingIntent).build();
noti.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
/**
* Show Notification
*/
private void showNotification(Context context, String content, String apkUrl) {
Intent myIntent = new Intent(context, DownloadService.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.putExtra(Constants.APK_DOWNLOAD_URL, apkUrl);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int smallIcon = context.getApplicationInfo().icon;
Notification notify = new NotificationCompat.Builder(context)
.setTicker(context.getString(R.string.android_auto_update_notify_ticker))
.setContentTitle(context.getString(R.string.android_auto_update_notify_content))
.setContentText(content)
.setSmallIcon(smallIcon)
.setContentIntent(pendingIntent).build();
notify.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notify);
}
public void startNotyfy(int id) {
Intent intent = new Intent();
intent.setClass(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
mPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_copy_file);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setContent(mRemoteViews);
mBuilder.setAutoCancel(true).setOngoing(false);
mBuilder.setContentIntent(mPendingIntent);
mBuilder.setTicker("正在复制...").setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_DEFAULT);
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE).setSmallIcon(R.drawable.ic_launcher);
mNotification = mBuilder.build();
mNotification.defaults &= ~Notification.DEFAULT_VIBRATE;
mNotification.flags = Notification.FLAG_AUTO_CANCEL;
mManager.notify(id, mNotification);
}
/**
* @see android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Boolean hdmi = intent.getBooleanExtra("autohdmi", false);
if (prefs.getBoolean("twostageenable", false) && !hdmi)
{
Logger.Log(context, "Two stage triggered");
Notification turnOffNotification = new Notification();
turnOffNotification.icon = R.drawable.ic_launcher;
turnOffNotification.tickerText = "Standby ready";
Intent notificationIntent = new Intent(context,
StandbyService.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0,
notificationIntent, 0);
if (!prefs.getBoolean("twostagepersistence", false))
turnOffNotification.flags |= Notification.FLAG_AUTO_CANCEL;
turnOffNotification.setLatestEventInfo(context, "Screen standby ready", "Do your stuffs, then click here to turn screen off", contentIntent);
notificationManager.notify("SCREENSTANDBY_READY", 0, turnOffNotification);
}
else
{
Logger.Log(context, intent);
context.startService(new Intent(context, StandbyService.class));
}
}
public void send(String ssid, int flog) {
String ticker, title, text;
ticker = (String) mcContext.getText(R.string.not_ticker);
title = (String) mcContext.getText(R.string.not_title);
text = (String) mcContext.getText(R.string.not_text);
// 创建一个启动其他Activity的Intent
Intent intent = new Intent(mcContext,
GosCheckDeviceWorkWiFiActivity.class);
intent.putExtra("softssid", ssid);
PendingIntent pi = PendingIntent.getActivity(mcContext, 0, intent, 0);
Notification notify = new Notification();
// 设置通知图标
notify.icon = R.drawable.ic_launcher;
// 设置显示在状态栏的通知提示信息
notify.tickerText = ticker;
notify.when = System.currentTimeMillis();
// 设置打开该通知,该通知自动消失
notify.flags = Notification.FLAG_AUTO_CANCEL;
// 构造通知内容布局
RemoteViews rv = new RemoteViews(mcContext.getPackageName(),
R.layout.view_gos_notification);
// 设置通知内容的标题
rv.setTextViewText(R.id.tvContentTitle, title);
// 设置通知内容
rv.setTextViewText(R.id.tvContentText, ssid + text);
// 加载通知页面
notify.contentView = rv;
// 设置通知将要启动的Intent
notify.contentIntent = pi;
// TODO 发送通知
// nm.notify(flog, notify);
}
private Notification createUpdateNotification(int type, String version, int nid) {
PendingIntent contentPendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(UPDATE_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deletePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(DELETE_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
int title_text_id;
switch(type) {
case 1:
title_text_id = R.string.ma_hasdataupdate;
break;
default:
title_text_id = R.string.ma_hasupdate;
break;
}
String title = getStringWithLocale(title_text_id).replace("(%s)", "").trim();
String content = version;
Bitmap updateBitmap = KcaUtils.decodeSampledBitmapFromResource(getResources(),
getId("ic_update_" + String.valueOf(type), R.mipmap.class), NOTI_ICON_SIZE, NOTI_ICON_SIZE);
NotificationCompat.Builder builder = createBuilder(getApplicationContext(), alarmChannelList.peek())
.setSmallIcon(R.mipmap.ic_stat_notify_1)
.setLargeIcon(updateBitmap)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setTicker(title)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(contentPendingIntent);
builder = setSoundSetting(builder);
Notification Notifi = builder.build();
Notifi.flags = Notification.FLAG_AUTO_CANCEL;
return Notifi;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
void startActivityNotification(Intent intent, int decisionId, String certName) {
Notification notification;
final PendingIntent call = PendingIntent.getActivity(master, 0, intent,
0);
final String mtmNotification = master.getString(R.string.mtm_notification);
final long currentMillis = System.currentTimeMillis();
final Context context = master.getApplicationContext();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
@SuppressWarnings("deprecation")
// Use an extra identifier for the legacy build notification, so
// that we suppress the deprecation warning. We will latter assign
// this to the correct identifier.
Notification n = new Notification(android.R.drawable.ic_lock_lock,
mtmNotification,
currentMillis);
setLatestEventInfoReflective(n, context, mtmNotification, certName, call);
n.flags |= Notification.FLAG_AUTO_CANCEL;
notification = n;
} else {
notification = new Notification.Builder(master)
.setContentTitle(mtmNotification)
.setContentText(certName)
.setTicker(certName)
.setSmallIcon(android.R.drawable.ic_lock_lock)
.setWhen(currentMillis)
.setContentIntent(call)
.setAutoCancel(true)
.getNotification();
}
notificationManager.notify(NOTIFICATION_ID + decisionId, notification);
}
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);
}
@SuppressLint("NewApi")
protected void showNotify(String title, String content, String info, Class<?> jumpClass) {
if(!Conf.isNotify) return;
Intent intent = new Intent(mContext, jumpClass);
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,0);
//获得通知管理器
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//构建一个通知对象(需要传递的参数有三个,分别是图标,标题和 时间)
Notification notification = new Notification.Builder(mContext)
.setContentText(content)
.setContentTitle(title)
.setContentInfo(info)
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL; // 自动销毁
notification.defaults |= Notification.DEFAULT_LIGHTS;
if(Conf.isNotifySound) {
notification.defaults |= Notification.DEFAULT_SOUND;
}
if(Conf.isNotifyVibrate) {
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
manager.notify(0, notification);//发动通知,id由自己指定,每一个Notification对应的唯一标志
}
private Notification createMoraleNotification(int idx, String kantaiName, int nid) {
PendingIntent contentPendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(CLICK_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deletePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(DELETE_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
String title = KcaUtils.format(getStringWithLocale(R.string.kca_noti_title_morale_recovered), idx + 1);
String content = KcaUtils.format(getStringWithLocale(R.string.kca_noti_content_morale_recovered), kantaiName);
Bitmap moraleBitmap = KcaUtils.decodeSampledBitmapFromResource(getResources(), R.mipmap.morale_notify_bigicon, NOTI_ICON_SIZE, NOTI_ICON_SIZE);
NotificationCompat.Builder builder = createBuilder(getApplicationContext(), alarmChannelList.peek())
.setSmallIcon(R.mipmap.morale_notify_icon)
.setLargeIcon(moraleBitmap)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setTicker(title)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(contentPendingIntent);
builder = setSoundSetting(builder);
Notification Notifi = builder.build();
Notifi.flags = Notification.FLAG_AUTO_CANCEL;
if (sHandler != null) {
bundle = new Bundle();
bundle.putString("url", KCA_API_UPDATE_FRONTVIEW);
bundle.putString("data", "");
sMsg = sHandler.obtainMessage();
sMsg.setData(bundle);
sHandler.sendMessage(sMsg);
}
return Notifi;
}
private Notification createAkashiRepairNotification(int nid) {
PendingIntent contentPendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(CLICK_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent deletePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, KcaAlarmService.class).setAction(DELETE_ACTION.concat(String.valueOf(nid))), PendingIntent.FLAG_UPDATE_CURRENT);
String title = getStringWithLocale(R.string.kca_noti_title_akashirepair_recovered);
String content = getStringWithLocale(R.string.kca_noti_content_akashirepair_recovered);
Bitmap akashiRepairBitmap = KcaUtils.decodeSampledBitmapFromResource(getResources(), R.mipmap.docking_akashi_notify_bigicon, NOTI_ICON_SIZE, NOTI_ICON_SIZE);
NotificationCompat.Builder builder = createBuilder(getApplicationContext(), alarmChannelList.peek())
.setSmallIcon(R.mipmap.docking_notify_icon)
.setLargeIcon(akashiRepairBitmap)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(false)
.setTicker(title)
.setDeleteIntent(deletePendingIntent)
.setContentIntent(contentPendingIntent);
builder = setSoundSetting(builder);
Notification Notifi = builder.build();
Notifi.flags = Notification.FLAG_AUTO_CANCEL;
if (sHandler != null) {
bundle = new Bundle();
bundle.putString("url", KCA_API_UPDATE_FRONTVIEW);
bundle.putString("data", "");
sMsg = sHandler.obtainMessage();
sMsg.setData(bundle);
sHandler.sendMessage(sMsg);
}
return Notifi;
}
/**
* 通知栏提醒
* @param messageItem 消息信息
*/
@SuppressWarnings("deprecation")
private void showMessage(ChatMessageItem messageItem) {
SettingSPUtil setSP = MyApp.getInstance().getSettingSPUtil();
if (!setSP.isChatNotiOnBar()){
return;
}
String messageContent = messageItem.getNickName()+":"+messageItem.getMessage();
MyApp.getInstance().setNewMsgCount(MyApp.getInstance().getNewMsgCount()+1); //数目+1
Notification notification = new Notification(R.drawable.ic_launcher,messageContent, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
if (setSP.isChatRing()){
// 设置默认声音
notification.defaults |= Notification.DEFAULT_SOUND;
}
if (setSP.isChatVibrate()){
// 设定震动(需加VIBRATE权限)
notification.defaults |= Notification.DEFAULT_VIBRATE;
}
Intent intent = new Intent(MyApp.getInstance(),ChatRoomAty.class);
intent.putExtra("TrainId", messageItem.getTrainId());
PendingIntent contentIntent = PendingIntent.getActivity(MyApp.getInstance(), 0, intent, 0);
String contentText = null;
if(MyApp.getInstance().getNewMsgCount()==1){
contentText = messageContent;
}else{
contentText = MyApp.getInstance().getNewMsgCount()+"条未读消息";
}
notification.setLatestEventInfo(MyApp.getInstance(), "车友聊天室", contentText, contentIntent);
NotificationManager manage = (NotificationManager) MyApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
manage.notify(NOTIFICATION_ID, notification);
}
private static void notify(Context context, Announcements announcements) {
Intent notificationIntent = new Intent(context, EventActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, announcements.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = getBaseNotification(context)
.setChannelId(context.getString(R.string.notifications_announcements_unique_id))
.setContentTitle(announcements.title)
.setContentText(announcements.text.replace('\n', ' '))
.setSubText(context.getString(R.string.notifications_announcements_sub_text) + " • " + announcements.author)
.setStyle(new NotificationCompat.BigTextStyle().bigText(announcements.text))
.setGroup(context.getString(R.string.notifications_announcements_unique_id))
.setContentIntent(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setCategory(Notification.CATEGORY_EVENT);
}
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManagerCompat.from(context).notify(context.getString(R.string.notifications_announcements_unique_id), announcements.id, notification);
}
/**
* @param context Context of the app
* @param event Event to notify
* @param eventsUsers EventUser associated with this event
* @param activeAction If actions will be shown
* @param autoCancel If force notification to auto-cancel in 5s (after subscription)
*/
public static void notify(final Context context, final Events event, EventsUsers eventsUsers, boolean activeAction, boolean autoCancel) {
Intent notificationIntent = EventActivity.getIntent(context, event);
PendingIntent intent = PendingIntent.getActivity(context, event.id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final NotificationCompat.Builder notificationBuilder = getBaseNotification(context)
.setContentTitle(event.name.trim())
.setContentText(event.description.replace('\n', ' '))
.setWhen(event.beginAt.getTime())
.setStyle(new NotificationCompat.BigTextStyle().bigText(event.description))
.setGroup(context.getString(R.string.notifications_events_unique_id))
.setChannelId(context.getString(R.string.notifications_events_unique_id))
.setContentIntent(intent);
if (event.kind != null)
notificationBuilder.setSubText(event.kind.name());
if (autoCancel) {
notificationBuilder.addAction(R.drawable.ic_event_black_24dp, context.getString(R.string.notification_auto_clear), null);
Handler h = new Handler();
long delayInMilliseconds = 5000;
h.postDelayed(() -> NotificationManagerCompat.from(context).cancel(context.getString(R.string.notifications_events_unique_id), event.id), delayInMilliseconds);
} else if (activeAction) {
Intent notificationIntentAction = new Intent(context, IntentEvent.class);
if (eventsUsers != null) {
notificationIntentAction.putExtra(IntentEvent.ACTION, IntentEvent.ACTION_DELETE);
notificationIntentAction.putExtra(IntentEvent.CONTENT_EVENT_USER_ID, eventsUsers.id);
notificationIntentAction.putExtra(IntentEvent.CONTENT_EVENT_ID, eventsUsers.eventId);
} else {
notificationIntentAction.putExtra(IntentEvent.ACTION, IntentEvent.ACTION_CREATE);
notificationIntentAction.putExtra(IntentEvent.CONTENT_EVENT_ID, event.id);
}
// intentEvent.putExtra(EventActivity.ARG_EVENT, ServiceGenerator.getGson().toJson(event));
PendingIntent intentAction = PendingIntent.getService(context, 1000000 + event.id, notificationIntentAction, PendingIntent.FLAG_UPDATE_CURRENT);
if (eventsUsers == null) {
notificationBuilder.addAction(R.drawable.ic_event_black_24dp, context.getString(R.string.event_subscribe), intentAction);
} else if (!event.beginAt.after(new Date()))
notificationBuilder.addAction(R.drawable.ic_event_black_24dp, context.getString(R.string.event_unsubscribe), null);
else
notificationBuilder.addAction(R.drawable.ic_event_black_24dp, context.getString(R.string.event_unsubscribe), intentAction);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setCategory(Notification.CATEGORY_EVENT);
}
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManagerCompat.from(context).notify(context.getString(R.string.notifications_events_unique_id), event.id, notification);
}