适配Android O的通知
创建类
public class NotificationUtils extends ContextWrapper {
private NotificationManager manager;
public static final String id = "channel_1";
public static final String name = "channel_name_1";
public NotificationUtils(Context context){
super(context);
}
public void createNotificationChannel(){
NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
getManager().createNotificationChannel(channel);
}
private NotificationManager getManager(){
if (manager == null){
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
return manager;
}
public Notification.Builder getChannelNotification(String title, String content){
return new Notification.Builder(getApplicationContext(), id)
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true);
}
public NotificationCompat.Builder getNotification_25(String title, String content){
return new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(title)
.setContentText(content)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true);
}
public void sendNotification(String title, String content){
if (Build.VERSION.SDK_INT>=26){ //api>=26时
createNotificationChannel();
Notification notification = getChannelNotification
(title, content).build();
getManager().notify(1,notification);
}else{
Notification notification = getNotification_25(title, content).build();
getManager().notify(1,notification);
}
}
}
使用
NotificationUtils notificationUtils = new NotificationUtils(view.getContext().getApplicationContext());
notificationUtils.sendNotification("测试标题", "测试内容");
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· mysql8.0无备份通过idb文件恢复数据过程、idb文件修复和tablespace id不一致处
· 使用 Dify + LLM 构建精确任务处理应用