有关android通知的学习

平时我们在工作中,通常会运用到通知的知识,现在我们来了解一下有关通知的知识。


一般通知

创建或者更新一个通知

 首先:要创建一个通知,将会用到一个类:NotificationManager;

这个NotificationManager是context(an activity 或者 a service)通过getSystemService()方法来获得。

NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

下面需要引入另一个概念:NotificationCompat.Builder 这个类 通过build()方法来创建一个Notification实体。同时可以利用PendingIntent 这个类来定义当用户选择了通知之后将要执行的操作。具体的代码如下:

 //准备当通知被选择这个操作触发之后的intent
        Intent intent = new Intent(this, MainActivity.class);
        //用系统的时间戳作为peddingIntent的唯一id
        PendingIntent pIntent = PendingIntent.getActivity(this, (int)System.currentTimeMillis(), intent, 0);
        // 创建notification
        // the addAction re-use the same intent to keep the example short
        Notification n  = new NotificationCompat.Builder(this)
                .setContentTitle("New mail from " + "test@gmail.com")//通知标题
                .setContentText("Subject")//中部通知内容
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)//点击通知后消失
//                .setProgress(100,0,true)
                .setFullScreenIntent(pIntent, true)//悬浮框
                .addAction(R.mipmap.ic_launcher, "Call", pIntent)
                .addAction(R.mipmap.ic_launcher, "More", pIntent)
                .addAction(R.mipmap.ic_launcher, "And more", pIntent).build();



        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //notify的参数是表示通知的id,通知本身
        notificationManager.notify(0, n);

 

 

Notificatoin.Builder 可以允许我们对这个通知定义三个按钮。

最后实现的结果如下所示:

通知取消

一定的环境下,需要取消掉通知,通知取消的代码如下:

 public void cancleNotification(View view) {
        notificationManager.cancel(0);
    }

这个就是指定通知的id来取消,当然我们也可以用cancleAll()来实现取消所有的通知。

Android 8.0之后的通知的改变

在8.0之后,通知发生了一些改变,就是添加了渠道的概念: 渠道就是将通知进行分类,如:兴趣,设置这些; 下面先上代码:

String channelId = "chananel_myron";
        String channelName = " 测试";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //在26版本以上才有渠道的概念
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            mManager.createNotificationChannel(channel);
        }
        //准备当通知被选择这个操作触发之后的intent
        Intent intent = new Intent(this, MainActivity.class);
        //用系统的时间戳作为peddingIntent的唯一id,这个id是判断点击
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);
        // 创建notification
        // the addAction re-use the same intent to keep the example short
        Notification n = new NotificationCompat.Builder(this,channelId)
                .setContentTitle("New mail from " + "test@gmail.com")//通知标题
                .setContentText("Subject")//中部通知内容
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .setAutoCancel(true)//点击通知后消失
//                .setProgress(100,0,true)
                .setFullScreenIntent(pIntent, true)//悬浮框
                .addAction(R.mipmap.ic_launcher, "Call", pIntent)
                .addAction(R.mipmap.ic_launcher, "More", pIntent)
                .addAction(R.mipmap.ic_launcher, "And more", pIntent).build();


        //notify的参数是表示通知的id,通知本身
        mManager.notify(0, n);

根据上面的代码,我们可以知道,创建渠道分为以下几个步骤:

1.初始化渠道

NotificationChannel(String id, CharSequence name, @Importance int importance)

现在对这三个参数进行讲解:

  •  id :就是渠道的id,在每一个package里面必须是唯一的
  • name:渠道的名称;
  • importance:渠道的重要性;

这里说一下importance这个参数:取值为NotificationManager的几个属性( IMPORTANCE_MIN 到 IMPORTANCE_MAX);

将渠道添加到NotificationManager中

 mManager.createNotificationChannel(channel);

NoticationCompat.Builder()选择系统推荐的构造器,即拥有两个参数的

 Notification n = new NotificationCompat.Builder(this,channelId);

就这样 就可以在android 8.0以上的手机运行通知了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值