安卓-Notification简单操作
一、基本的通知
public void basicNotify(View view) { // 注意:这里如果:Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 才设置channelId Notification.Builder builder = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { builder = new Notification.Builder(this, "channelId"); } else { builder = new Notification.Builder(this); } // 这里对builder进行简单的设置操作 builder.setSmallIcon(R.mipmap.ic_launcher); builder.setAutoCancel(true); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); builder.setContentTitle("basic notifications"); builder.setContentText("i am a basic"); builder.setSubText("realdy basic"); // 通过intent创建要跳转的页面,因为不是立即执行所以使用pendingIntent来封装一层 // 将pendingIntent设置到Notification.Builder中 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); builder.setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 注意:这里的channel设置条件跟builder的设置条件一样,并且channelId 要跟builder的channelId一样 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channelId","channelName",NotificationManager.IMPORTANCE_LOW); notificationManager.createNotificationChannel(channel); } notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build()); }
对于基本的通知操作,如上已经在代码的备注中做了说明,点击之后会在通知栏显示该条通知,如果有多条通知则会以最新的一条通知为准
注意:channelId和channelName的最长字符是40个字符,如果多于40个字符则会被截断;同一个app里面,每个channelId需要设置的不一样否则新的通知会覆盖之前相同channelId的通知
二、折叠通知
在1的基础上,builder增加如下的设置即可
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); contentView.setTextViewText(R.id.textView, "show me when collapsed"); builder.setCustomContentView(contentView); RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_expanded); builder.setCustomBigContentView(bigContentView);
对应的notification和notification_expanded的xml根据需求进行设置即可
三、悬挂式的通知
悬挂式和非悬挂式的通知区别主要在builder的设置pendingIntent
非悬挂式使用builder.setContentIntent(pendingIntent); 设置pendingIntent
悬挂式使用builder.setFullScreenIntent(pendingIntent, true); 设置pendingIntent
NotificationChannel 的importance设置重要程度
IMPORTANCE_NONE 关闭通知
IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
在调试的时候遇到了希望弹出悬挂式的通知,但是一直没有弹出的现象;排查原因发现是手机的通知设置那边设置通知的类别没有选择横幅通知。
四、等级的通知
有三个等级的通知,如下所示
VISIBILITY_PRIVATE——表明只有当没有锁屏的时候会显示
VISIBILITY_PUBLIC——表明在任何情况下都会显示
VISIBILITY_SECRET——表明在pin、password等安全锁和没有锁屏的情况下才能够显示
通过在builder进行设置等级的类别
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setVisibility(Notification.VISIBILITY_PRIVATE);
builder.setVisibility(Notification.VISIBILITY_SECRET);
通知栏信息整理:https://www.cnblogs.com/zhangmiao14/p/7080937.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!