Android篇 --Notification(消息通知)
消息通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望用户发出一些提示信息,而该应用又不在前台运行时,就可以借助通知来实现。
Android 8.0之前的Notification
我们对Notification简单使用方法是:
//定义一个PendingIntent点击Notification后启动一个Activity
Intent it = new Intent(mContext, OtherActivity.class);
PendingIntent pit = PendingIntent.getActivity(mContext, 0, it, 0);
/*
首先需要一个NotificationManager来对通知进行管理
调用Context的getSystemService()方法获取到。
getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。
*/
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
使用Builder构造器来创建Notification对象
*/
Notification.Builder notification = new Notification.Builder(MainActivity.this);
//指定通知的标题内容
notification.setContentTitle("This is content title")
//设置通知的内容
.setContentText("This is content text")
//指定通知被创建的时间
.setWhen(System.currentTimeMillis())
//设置通知的小图标
.setSmallIcon(R.drawable.ic_launcher_foreground)
//设置通知的大图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher_background))
//添加点击跳转通知跳转
.setContentIntent(pit)
//实现点击跳转后关闭通知
.setAutoCancel(true);
Notification notify1 = notification.build();
/*
调用NotificationManager的notify()方法将通知显示出来
传入的第一个参数是通知的id
传入的第二个参数是notification对象
*/
notificationManager.notify(NOTIFYID_1,notify1);
Notification8.0之后的方法在在8.0中实现时出现的状况
public Builder(Context context, String channelId) {
this(context, (Notification) null);
mN.mChannelId = channelId;
}
第一个参数是上下文对象
第二个参数是通知渠道的代码
Android 引入了 通知渠道(Notification Channels),以提供统一的系统来帮助用户管理通知,如果是针对 android 为目标平台时,必须实现一个或者多个通知渠道,以向用户显示通知。否则通知功能无法生效。
Android 8.0之后的Notification
/*
首先需要一个NotificationManager来对通知进行管理
调用Context的getSystemService()方法获取到。
getSystemService()方法接受的一个字符串参数用于确定系统的的哪一个服务。
*/
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/*
调用NotificationChannel创建通知渠道实例
并为它设置属性
*/
//通知渠道的ID
String id = "channel_01";
//用户可以看到的通知渠道的名字
CharSequence name = getString(R.string.clientid);
//用户可看到的通知描述
String description = getString(R.string.clientid);
//构建NotificationChannel实例
NotificationChannel notificationChannel =
new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
//配置通知渠道的属性
notificationChannel.setDescription(description);
//设置通知出现时的闪光灯
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//设置通知出现时的震动
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});
//在notificationManager中创建通知渠道
notificationManager.createNotificationChannel(notificationChannel);
//定义一个PendingIntent点击Notification后启动一个Activity
Intent it = new Intent(mContext, OtherActivity.class);
PendingIntent pit = PendingIntent.getActivity(mContext, 0, it, 0);
//设置图片,通知标题,发送时间,提示方式等属性
Notification notification = new NotificationCompat.Builder(MainActivity.this,id)
.setContentTitle("你好") //标题
.setContentText("Android新手报道~") //内容
.setSubText("——记住我叫Android") //内容下面的一小段文字
.setTicker("收到叶Android发送过来的信息~") //收到信息后状态栏显示的文字信息
.setWhen(System.currentTimeMillis()) //设置通知时间
.setSmallIcon(R.mipmap.ic_launcher) //设置小图标
.setLargeIcon(LargeBitmap) //设置大图标
.setAutoCancel(true) //设置点击后取消Notification
.setContentIntent(pit) //设置PendingIntent
.build();
notificationManager.notify(NOTIFYID_1, notification);
效果图
原文借鉴:https://blog.csdn.net/qq_36607515/article/details/81393794