美文网首页我爱编程
Android O上Notifaction的正确用法

Android O上Notifaction的正确用法

作者: 一个搬砖小能手 | 来源:发表于2018-04-15 21:05 被阅读297次

    引言

    随着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

    相关文章

      网友评论

        本文标题:Android O上Notifaction的正确用法

        本文链接:https://www.haomeiwen.com/subject/uedjkftx.html