赞
踩
Android Notifaction 从4.0到7.0
图1 4.0 通知样式
图2 5.0 6.0 通知样式
图3 7.0 通知样式
这么多版本,如何兼容,没关系,交给
android.support.v7.app.NotificationCompat
NotificationManager nm = (NotificationManager) ctx.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
通过builder设置notifaction的属性
具体方法见:官方API,国内可用
就是这些style
NotificationCompat.BigPictureStyle
NotificationCompat.BigTextStyle
NotificationCompat.DecoratedCustomViewStyle
NotificationCompat.MessagingStyle
两种方式
1.builder.setPriority(Notification.PRIORITY_MAX);
2.builder.setFullScreenIntent(intent,false);
long[] pattern = {0,0};
builder.setVibrate(pattern);
builder.setLights(Color.rgb(0,0,0),0,0);
builder.setSound(null);
如果你是用glide图片加载框架,那么恭喜你中奖了,今天我介绍的就是glide,同样的Picasso也有类似的方法,用法基本差不多,就不多介绍了
常用的方法要不就是本地图片,要不就是先下载再显示通知,这里我们也说两种
1.直接显示,异步加载通知区域图片
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// Inflate and set the layout for the expanded notification view
RemoteViews expandedView = new RemoteViews(ctx.getPackageName(), R.layout.notification_expanded);
expandedView.addView(R.id.content_rl, notification.contentView.clone());
notification.bigContentView = expandedView;
NotificationTarget notificationTarget = new NotificationTarget(context.getApplicationContext(), expandedView, R.id.expanded_imageView, notification, id);
Glide.with(context.getApplicationContext()) // safer!
.load(bigPicture)
.asBitmap()
.into(notificationTarget);
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="right|top" android:orientation="vertical"> <RelativeLayout android:id="@+id/content_rl" android:layout_width="match_parent" android:layout_height="wrap_content"></RelativeLayout> <ImageView android:id="@+id/expanded_imageView" android:layout_width="match_parent" android:layout_height="152dp" android:contentDescription="@string/app_name" android:scaleType="centerCrop" />
2.先下载后显示
重写NotificationTarget,再获取到图片后再显示通知
builder.setFullScreenIntent(intent,false);
/** * An intent to launch instead of posting the notification to the status bar. * Only for use with extremely high-priority notifications demanding the user's * <strong>immediate</strong> attention, such as an incoming phone call or * alarm clock that the user has explicitly set to a particular time. * If this facility is used for something else, please give the user an option * to turn it off and use a normal notification, as this can be extremely * disruptive. * * <p> * The system UI may choose to display a heads-up notification, instead of * launching this intent, while the user is using the device. * </p> * * @param intent The pending intent to launch. * @param highPriority Passing true will cause this notification to be sent * even if other notifications are suppressed. * * @see Notification#fullScreenIntent */ public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { mFullScreenIntent = intent; setFlag(FLAG_HIGH_PRIORITY, highPriority); return this; }
大概意思就是:设置setFullScreenIntent后,说明这个通知的优先级跟来电话的优先级一样高,会直接显示给你,建议设置开关让用户可以自由选择
因此建议使用设置通知优先级来显示横幅
咋一看,貌似区别不大,实际区别如下:
在7.0以上,setContentInfo表示红框区域,setContentText表示黑框区域
/** * Set the large text at the right-hand side of the notification. */ public Builder setContentInfo(CharSequence info) { mContentInfo = limitCharSequenceLength(info); return this; } /** * Set the title (first row) of the notification, in a standard notification. */ public Builder setContentTitle(CharSequence title) { mContentTitle = limitCharSequenceLength(title); return this; } /** * Set the text (second row) of the notification, in a standard notification. */ public Builder setContentText(CharSequence text) { mContentText = limitCharSequenceLength(text); return this; } /** * Set the third line of text in the platform notification template. * Don't use if you're also using {@link #setProgress(int, int, boolean)}; * they occupy the same location in the standard template. * <br> * If the platform does not provide large-format notifications, this method has no effect. * The third line of text only appears in expanded view. * <br> */ public Builder setSubText(CharSequence text) { mSubText = limitCharSequenceLength(text); return this; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。