通知Notification(可展开的大布局)使用,适配android8.0
2018-03-27 20:51
621 查看
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后必须传入NotificationChannel if(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以后才能使用,BigTextStyle NotificationCompat.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 4000 .drawable.logo_alpha); } else { builder.setSmallIcon(R.mipmap.logo); } }
相关文章推荐
- Android Notification使用系统通知栏布局出现的图标问题
- Android 通知栏发送消息Notification(可以使用自定义的布局)
- Android Notification 通知的使用
- Android中通知的使用-----Notification详解
- android中通知栏notification的使用
- android Notification 的使用(锁定通知栏)
- android 如何自定义通知栏图标(不使用自定义布局情况下)
- Android Notification 使用详解之二:可更新进度的通知
- android Notification(通知) 的使用
- android使用Notification进行通知时有时候通知栏中的项背景是灰白的的,而不是根据手机系统的颜色而定
- Android中通知的使用-----Notification详解
- Android的通知(Notification)使用详解
- Android下Notification(通知栏)的使用
- Android中通知的使用-----Notification详解
- Android中通知(Notification)的简单使用
- Android下Notification(通知栏)的使用
- android通知(Notification)的使用
- Android Notification 使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- 【Android】使用广播BroadcastReceiver拦截通知Notification
- 使用android push notification service 实现即时通知