Android O上Notifaction的正确用法

引言

随着Android发展,很多老组件的用法也需要调整了。比如今天在升级某个个人项目时就碰到一个老工程的Notifiaction忽然出不来了。遂查资料,如果设置compileSdkVersion 26及以上需要传入一个NotifactionChannel类。那么NotificationChannel到底是个什么呢?谷歌在源码中如是说:

A representation of settings that apply to a collection of similarly themed notifications.

可以看到本质上就是对通知样式的一个描述集合。于是整理一个简单封装了一个DEMO类,抛砖引玉。

代码


import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

/**
 * Created by MoXiao on 2018/4/15.
 */

public class NotificationUtils {
    private static final String TAG = "NotificationUtils";
    private static final String DEFAULT_CHANNEL_ID = "default_id";
    private static final String DEFAULT_CHANNEL_NAME = "default_channel_name";
    private static final int DEFAULT_NOTIFY_ID = 1;
    /**
     * @param context
     * @param title
     * @param content
     * @param smallIcon 显示在状态栏上的小图标,必需
     */
    public static void notification(Context context, String title, String content, int smallIcon) {
        //无论对于哪个API Level,都需要
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 26) {
            boolean buildResult = notification_api_26_or_Above(notificationManager, DEFAULT_CHANNEL_ID);
            if (!buildResult) {
                Log.e(TAG, "Fail to build notifiactionChannel");
                return;
            }
        }
        //NotifyID,这个以前也需要
        int notifyID = DEFAULT_NOTIFY_ID;

        //ChannelID, 指明需要哪个channel。这里我们使用默认的。
        String CHANNEL_ID = DEFAULT_CHANNEL_ID;

        NotificationCompat.Builder nb = new NotificationCompat.Builder(context,CHANNEL_ID);
        Notification notification = nb.setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(smallIcon)
                .build();
        notificationManager.notify(notifyID, notification);
    }

    @TargetApi(26)
    private static boolean notification_api_26_or_Above(NotificationManager notificationManager, String channelId) {
        if (notificationManager == null && TextUtils.isEmpty(channelId)) {
            return false;
        }
        //Android O及以上必需的channelID
        String id = channelId;
        //Channel名,有说这个字段对用户可见。但我还没有找到可见的ROM/Android版本。有知道的同学麻烦告知一下
        String name = DEFAULT_CHANNEL_NAME;
        //Channel的描述
        String description = "我就是一个通知的channel";
        //通知的优先级
        int importance = NotificationManager.IMPORTANCE_LOW;
        
        NotificationChannel channel = createDefaultChannel(id, name, description, importance);
        
        //将NotificationChannel对象传入NotificationManager
        notificationManager.createNotificationChannel(channel);
        return true;
    }

    /**
     * 创建默认封装的通知,其他样子依瓢画葫芦就行
     * @param channelId    见上方注释
     * @param channelName  见上方注释
     * @param desc         见上方注释
     * @param imp          见上方注释
     * @return 生成好的默认行为的channel
     */
    @TargetApi(26)
    private static NotificationChannel createDefaultChannel(String channelId, String channelName, String desc, int imp) {

        NotificationChannel channel = new NotificationChannel(channelId, channelName, imp);

        channel.setDescription(desc);
        //是否使用指示灯
        channel.enableLights(true);
        //指示灯颜色,这个取决于设备是否支持
        channel.setLightColor(Color.RED);

        //是否使用震动
        channel.enableVibration(true);
        //震动节奏
        channel.setVibrationPattern(new long[]{100, 200});

        return channel;
    }
}

调用方式:

 //在需要通知处
 NotificationUtils.notification(this, "I am title", "I am content", R.mipmap.ic_av_timer_black_48dp);

可看到,在Android O以上版本的适配中,绝大部分都跟以前一致。区别就是NotificationChannel的创建而已。
Notifaction的更多相关知识点可以参考这篇文章:
Notification知识点总结

参考链接:
https://stackoverflow.com/questions/45668079/notificationchannel-issue-in-android-o

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,759评论 0 38
  • 关于IT的英语 win10 系统 win + x apps and features 应用和功能 feature:...
    我要写小说阅读 9,611评论 0 1
  • 实践:几位比我年长的朋友向我咨询行业内的问题,我直接蒙圈了,下意识求教领导。 反思:最主要的还是对专业知识掌握不够...
    蒋莹阅读 1,391评论 0 0
  • 我本来打算炒菜,消息一个接一个,忘记往锅里放菜了,直到我发现味道不对。。。。 你们谁请我吃早餐?[大哭]
    ENHUIhui阅读 1,265评论 0 1
  • 前几天,我在小区里玩,突然,有几滴“水”在我头上“安顿”下来,我抬头看了看天空,晴空万里,我就没太在意,玩了一...
    smile沐恩阅读 1,666评论 1 1

友情链接更多精彩内容