本文主要是介绍通知Notification(可展开的大布局)使用,适配android8.0,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
补充修正:
2018-11-07 问题:Notification PendingIntent失效,每个通知都响应第一个PendingIntent
https://blog.csdn.net/u013370255/article/details/83791750
2018-08-16 问题:app版本更新,通知形式显示安装包下载进度
https://blog.csdn.net/u013370255/article/details/81737122
1、调用
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();
intent.putExtras(bundle);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);showNotification(getApplicationContext(),pi,"标题","标题","主要标题","标题",tuiSongMessage);
/*** 生成通知* @param context* @param pi* @param ticker 标题* @param contentTitle 标题* @param bigContentTitle 主要标题* @param summaryText 标题* @param tuiSongMessage 显示内容(消息体)*/private void showNotification(Context context,PendingIntent pi,String ticker,String contentTitle,String bigContentTitle,String summaryText,TuiSongResponse tuiSongMessage){//android O后必须传入NotificationChannelif(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"1");setNotification(builder,context,pi,ticker,contentTitle,bigContentTitle,summaryText,tuiSongMessage);notificationManager.notify(Constants.NOTIFICATIONID, builder.build());//ChannelId为"1",ChannelName为"Channel1"NotificationChannel channel = new NotificationChannel("1","Channel1", NotificationManager.IMPORTANCE_DEFAULT);channel.enableLights(true); //是否在桌面icon右上角展示小红点channel.setLightColor(Color.YELLOW); //小红点颜色channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知notificationManager.createNotificationChannel(channel);}else {NotificationCompat.Builder builder = new NotificationCompat.Builder(context,null);setNotification(builder,context,pi,ticker,contentTitle,bigContentTitle,summaryText,tuiSongMessage);NotificationManagerCompat managerCompat = NotificationManagerCompat.from(context);managerCompat.notify(Constants.NOTIFICATIONID, builder.build());}}
/*** 设置大布局通知参数* @param builder* @param context* @param pi* @param ticker* @param contentTitle* @param bigContentTitle* @param summaryText* @param tuiSongMessage*/private void setNotification(NotificationCompat.Builder builder,Context context,PendingIntent pi,String ticker,String contentTitle,String bigContentTitle,String summaryText,TuiSongResponse tuiSongMessage){builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.logo)).setTicker(ticker).setContentTitle(contentTitle).setWhen(System.currentTimeMillis()).setContentIntent(pi).setVibrate(new long[]{500, 500, 500, 500, 500, 500}).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND);//大布局通知在4.1以后才能使用,BigTextStyleNotificationCompat.BigTextStyle textStyle = null;if(android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {textStyle = new NotificationCompat.BigTextStyle();textStyle.setBigContentTitle(bigContentTitle)// 标题.setSummaryText(summaryText).bigText(tuiSongMessage.content.content);// 内容builder.setStyle(textStyle);}builder.setContentText(tuiSongMessage.content.content);if(SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {builder.setSmallIcon(R.drawable.logo_alpha);} else {builder.setSmallIcon(R.mipmap.logo);}}
这篇关于通知Notification(可展开的大布局)使用,适配android8.0的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!