自己项目中走过的坑 请大家不要跳坑
因为当时看文章中介绍只适配了8.0 现在运行到8.0以下的手机出现一个错误
Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html
Android api 一直对通知栏进行升级! 包括7.0继续改善快捷通知栏,接下来介绍下通知栏不同版本的兼容适配.
Android JELLY_BEAN(16) 通知可以直接new Notification()
Notification notification = new Notification();
notification.icon = android.R.drawable.stat_sys_download_done;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// 设置点击事件的PendingIntent
notification.setLatestEventInfo(mContext, aInfo.mFilename, contentText, pendingIntent);
Android .LOLLIPOP_MR1(22) 通知可以通过Notification.Builder()
Notification notification = new Notification.Builder(mContext)
.setAutoCancel(false)
.setContentIntent(pi)// 设置pendingIntent
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setWhen(System.currentTimeMillis())
.build();
Android .LOLLIPOP_MR1(22)以上 也就从6.0开始 只能new NotificationCompat.Builder(mContext)
Notification notification = new NotificationCompat.Builder(mContext)
.setContentTitle(aInfo.mFilename)
.setContentText(contentText)
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(pi)// 设置pendingIntent
.build();
Android .O以上 也就从8.0开始 需要制定chanel属性
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message);
最后整合的代码
public void showNotification(){
final int NOTIFICATION_ID = 12234;
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//准备intent
Intent intent = new Intent();
String action = "com.tamic.myapp.action";
intent.setAction(action);
//notification
Notification notification = null;
String contentText;
// 构建 PendingIntent
PendingIntent pi = PendingIntent.getActivity(mContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//版本兼容
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
notification = new Notification();
notification.icon = android.R.drawable.stat_sys_download_done;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(mContext, aInfo.mFilename, contentText, pi);
} else if (Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O && Build.VERSION.SDK_INT >= LOLLIPOP_MR1) {
notification = new NotificationCompat.Builder(mContext)
.setContentTitle("Title")
.setContentText(contentText)
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentIntent(pi).build();
} else if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN &&
Build.VERSION.SDK_INT <= LOLLIPOP_MR1) {
notification = new Notification.Builder(mContext)
.setAutoCancel(false)
.setContentIntent(pi)
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setWhen(System.currentTimeMillis())
.build();
} else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setContentTitle("Title").build();
}
notificationManager.notify(NOTIFICATION_ID, notification);
}