补充修正:
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后必须传入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.drawable.logo_alpha);
} else {
builder.setSmallIcon(R.mipmap.logo);
}
}