Android Notifaction 从4.0到7.0

本文详细介绍了Android通知从4.0到7.0的变化,并提供了兼容不同版本的通知实现方案。通过使用NotificationCompat库简化了开发流程。此外,还探讨了高级样式如BigTextStyle及如何实现横幅通知和加载图片等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Notifaction 从4.0到7.0

图1 4.0 通知样式

4.4.4

图2 5.0 6.0 通知样式

5.0 6.0

图3 7.0 通知样式

7

兼容

这么多版本,如何兼容,没关系,交给
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.InboxStyle

NotificationCompat.MediaStyle

NotificationCompat.DecoratedCustomViewStyle

NotificationCompat.MessagingStyle

小技巧

横幅

两种方式

1.builder.setPriority(Notification.PRIORITY_MAX);

2.builder.setFullScreenIntent(intent,false);

通知无声音、无震动、无LED呼吸灯闪烁

        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,再获取到图片后再显示通知

总有些坑在等你跳

1.设置横幅后,锁屏后解开直接触发通知的PendingIntent事件

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后,说明这个通知的优先级跟来电话的优先级一样高,会直接显示给你,建议设置开关让用户可以自由选择
因此建议使用设置通知优先级来显示横幅

2.setContentInfo与setContentText

咋一看,貌似区别不大,实际区别如下:

在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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值