Android 9通知栏(基础)
发送通知
首先是一个最简单的通知
//1,获取通知管理器
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//2,通知渠道
NotificationChannel channel = new NotificationChannel("wendu", "温度", NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);//注册到通知管理器
Notification.Builder builder = new Notification.Builder(this, "wendu");//这里要和注册的渠道id要一致
builder.setSmallIcon(R.drawable.image)//这是必须的
.setChannelId("wendu")//在这里设置会覆盖上面的设置
.setContentTitle("测试")
.setContentText("这是测试内容");
notificationManager.notify(1, builder.build());
自定义视图
//自定义布局的通知
public void SelfNofition(View view) {
NotificationChannel notifici = new NotificationChannel("self", "自定义", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notifici);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notifici);//自定义的布局视图
views.setTextViewText(R.id.fazhi, "警告:当前阀值:200/120");
Notification.Builder builder = new Notification.Builder(this, "self");
builder.setSmallIcon(R.drawable.qq)//使用RemoteViews时,设置的是状态栏中的小图标,必须要设置
.setContent(views);
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
}
对自定义的布局中,最外层的布局似乎要用mach才能让通知栏滑动取消。
设置渠道分组
public class MainActivity extends AppCompatActivity {
//通知管理器
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1,获取通知管理器
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//设置群组(不是必须的)
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("group", "groupname"));
}
public void Send(View view) {
String id = "test";
String name = "ad";
//2,通知
//渠道id
Notification.Builder builder = new Notification.Builder(this, "ad");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//Android 版本本人为28,即Android9。 后面的是Android的版本
//3,通知渠道
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
Log.i("info123", mChannel.toString());
// NotificationChannel{mId='my_channel_01', mName=我是渠道名字,
// mDescription=, mImportance=2, mBypassDnd=false, mLockscreenVisibility=-1000,
// mSound=content://settings/system/notification_sound,
// mLights=false, mLightColor=0, mVibration=null, mU
mChannel.setGroup("group");
//将通知渠道创建到通知管理器
notificationManager.createNotificationChannel(mChannel);
//
RemoteViews views = new RemoteViews(getPackageName(), R.layout.notifici);//自定义的布局视图
views.setTextColor(R.id.mytext, Color.GREEN);
views.setTextViewText(R.id.mytext, "这是自定义的文字");
views.setImageViewResource(R.id.iamge, R.drawable.ic_launcher_background);
//设置通知属性
builder = new Notification.Builder(this, "ad");
builder.setSmallIcon(R.drawable.image)//使用RemoteViews时,设置的是状态栏中的小图标,必须要设置
.setAutoCancel(true)//设置是否点击通知后会自动消失
.setChannelId(id)
.setContent(views)
.setContentTitle("我是广告")
.setContentText("内容:恰饭时间到了,Oreo is Coming.")
.setNumber(4)//设置通知集合的数量
.setWhen(System.currentTimeMillis())//设置展示时间
.setTicker("通知来了");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
} else {
}
//通过通知管理器发送通知
//标识应用程序中此通知的唯一标识符。根据渠道ID发送通知
//一个对象对象,描述要向用户显示什么。不能为空。
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
}
}
通知栏从Android8.0开始,便需要加入渠道信息才能够正常显示通知了。
参考
参考:详细介绍,Demo
自定义通知布局
详细介绍
自定义布局设置布局文件数据
使在一组中的通知,进行数据的更替。在另一组中就打开新的通知
Android 8.0 后通知、通知渠道、通知渠道组使用简介
最后,一定要注意手机的通知权限。和通知的重要程度。