Android 通知栏使用

不同版本通知栏的创建方式不尽相同,当前官方推荐使用 NotificationCompat 相关的API,兼容到Android 4.0,但是部分新功能,比如内嵌回复操作,旧版本是无法支持的。

一、设置通知内容

1 //CHANNEL_ID,渠道ID,Android 8.0及更高版本必须要设置 2 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) 3 //设置小图标 4 .setSmallIcon(R.drawable.notification_icon) 5 //设置标题 6 .setContentTitle(textTitle) 7 //设置内容 8 .setContentText(textContent) 9 //设置等级 10 .setPriority(NotificationCompat.PRIORITY_DEFAULT);

二、创建渠道

在 Android 8.0 及更高版本上提供通知,需要在系统中注册应用的通知渠道。

1 private void createNotificationChannel() { 2 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 3 CharSequence name = getString(R.string.channel_name); 4 String description = getString(R.string.channel_description); 5 //不同的重要程度会影响通知显示的方式 6 int importance = NotificationManager.IMPORTANCE_DEFAULT; 7 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); 8 channel.setDescription(description); 9 10 NotificationManager notificationManager = getSystemService(NotificationManager.class); 11 notificationManager.createNotificationChannel(channel); 12 } 13 }

上述代码应该在应用启动时立即执行,可以放在 Application 中进行初始化。

三、设置通知栏的点击操作

一般点击通知栏会打开对应的 Activity 界面,具体代码如下:

1 //点击时想要打开的界面 2 Intent intent = new Intent(this, AlertDetails.class); 3 //一般点击通知都是打开独立的界面,为了避免添加到现有的activity栈中,可以设置下面的启动方式 4 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 5 //创建activity类型的pendingIntent,还可以创建广播等其他组件 6 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 7 8 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) 9 .setSmallIcon(R.drawable.notification_icon) 10 .setContentTitle("My notification") 11 .setContentText("Hello World!") 12 .setPriority(NotificationCompat.PRIORITY_DEFAULT) 13 //设置pendingIntent 14 .setContentIntent(pendingIntent) 15 //设置点击后是否自动消失 16 .setAutoCancel(true);

四、显示通知

1 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); 2 //notificationId 相当于通知的唯一标识,用于更新或者移除通知 3 notificationManager.notify(notificationId, builder.build());
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0

Android蓝牙连接汽车OBD设备

//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");