创建Notification的步骤
-
创建一个
NotificationManger
的管理类对象NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
-
Builder一个Notification用于设置Notification的内容以及属性
-
若使用系统布局的通知
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(this, channelID);
-
若使用自定义布局的通知则需加上
RemoteViews
对象RemoteViews small = new RemoteViews(getPackageName(),R.layout.custom_notification_item_small); RemoteViews large = new RemoteViews(getPackageName(),R.layout.custom_notification_item);
-
Builder
一个NotificationCompat
用于设置Notification
的相关属性 -
创建一个
Notification
并且通过notificationCompat.build()
Notification notification = notificationCompat.build();
-
创建
NotificationChannel
对象并设置其参数if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationChannel notificationChannel= new NotificationChannel("66",channelID,NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); }
第一个参数id任意值,第二个参数为任意字符串,第三个参数设置其优先级
-
最后调用·
NotificationManager
的notify()方法manager.notify(66,notification);