public sendNotification(Intent intent, String title, String message,
int requestCode, int backgroundIcon) {
//用于通知用户发生的事件的类。这就是告诉用户后台发生了什么的方法。
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//NotificationCompat对象的Builder类。允许更轻松地控制所有标志,以及帮助构建典型的通知布局
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
//区分不同api
if (Build.VERSION.SDK_INT >= 26) {
//渠道id
channelId = "wear";
//渠道名字
channelName = "wearNotification";
//创建渠道
createNotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
notificationBuilder = new NotificationCompat.Builder(context, channelId);
}
notificationBuilder.setSmallIcon(backgroundIcon)//设置图标
.setContentTitle(title)//设置标题
.setContentText(message) //设置显示内容
.setAutoCancel(true) //是否取消
.setContentIntent(viewPendingIntent);//intent
notificationManager = NotificationManagerCompat.from(context);
//发送通知
notificationManager
.notify(requestCode, notificationBuilder.build());
}
/**
* 创建渠道
*/
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName, int importance) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
notificationManager.createNotificationChannel(channel);
}
Android8.0行为变更之Notification
猜你喜欢
转载自blog.csdn.net/sytandxly/article/details/81408205
今日推荐
周排行