- Android 8.0通知栏适配 [ 学习 ]

目录

实践前检查

正式实践

让通知显示

管理通知渠道


从Android 8.0系统开始,Google引入了通知渠道这个概念。

什么是通知渠道呢?顾名思义,就是每条通知都要属于一个对应的渠道。每个App都可以自由地创建当前App拥有哪些通知渠道,但是这些通知渠道的控制权都是掌握在用户手上的。用户可以自由地选择这些通知渠道的重要程度,是否响铃、是否振动、或者是否要关闭这个渠道的通知。

拥有了这些控制权之后,用户就再也不用害怕那些垃圾推送消息的打扰了,因为用户可以自主地选择自己关心哪些通知、不关心哪些通知。

……

原博出自郭神的Android通知栏微技巧,8.0系统中通知栏的适配,郭神的博客永远干货满满,本篇博客只是为了记录记录整个通知栏适配的操作过程,开篇之前感谢郭神~

 

实践前检查

  • 请确保AS版本在3.0及以上
  • 请确保targetSdkVersion在26及以上

 

正式实践

按照郭神的思路,先一步步的操作,那么第一步,先简单的创造两个通知渠道:

package com.xxxx.notificationtest;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//如果当前版本大于等于26
            String id = "IMPORTANCE_HIGH TEST";
            CharSequence name = "测试 1";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotification(id, name, importance);

            id = "IMPORTANCE_DEFAULT TEST";
            name = "测试 2";
            importance = NotificationManager.IMPORTANCE_DEFAULT;;
            createNotification(id, name, importance);
        }

    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotification(String id, CharSequence name, int importance) {
        NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
        NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

 插入一点题外话,在createNotification()这个方法之上,一定要加上@TargetApi(Build.VERSION_CODES.O),否则代码就会报错 ——

主要也是因为考虑到Android的兼容性,NotificationChannel这个东西,是用在SDK26及以上版本的,但是我这里给的最小版本是SDK19,所以考虑到兼容,特别用@TargetApi(Build.VERSION_CODES.O)这个方法说明一下,说明createNotification()这个方法要在26才能使用,大概就是这个意思。

那么创造完之后,运行demo,查看这个App的应用信息,可以看到,通知渠道的类别已经被创建出来了:

 

让通知显示

实践的第二步,就是让通知栏能够出来

我们首先修改一下activity_main.xml,让它有两个按钮,用以测试高等级的通知和普通等级的通知。UI什么的就不要太在意了,反正就是个栗子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_test1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="测试 1:IMPORTANCE_HIGH" />

    <Button
        android:id="@+id/btn_test2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="测试 2:IMPORTANCE_DEFAULT" />
</LinearLayout>

然后我们再修改一下MainActivity的代码,触发按钮事件:

package com.xxxx.notificationtest;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button testHigh;
    Button testDefault;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//如果当前版本大于等于26
            String id = "IMPORTANCE_HIGH TEST";
            CharSequence name = "测试 1";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotification(id, name, importance);

            id = "IMPORTANCE_DEFAULT TEST";
            name = "测试 2";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotification(id, name, importance);
        }

    }

    private void initView() {
        testHigh = findViewById(R.id.btn_test1);
        testHigh.setOnClickListener(onClickListener);

        testDefault = findViewById(R.id.btn_test2);
        testDefault.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_test1:
                    testHighImportance();//测试高等级通知
                    break;
                case R.id.btn_test2:
                    testDefaultImportance();//测试普通等级通知
                    break;
                default:
                    break;
            }
        }
    };

    private void testHighImportance() {
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "IMPORTANCE_HIGH TEST")
                .setContentTitle("IMPORTANCE_HIGH TEST").setContentText("这是一条用于测试 IMPORTANCE_HIGH" +
                        " 的信息").setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable
                        .ic_launcher_foreground).setAutoCancel(true)
                .build();
        notificationManager.notify(1, notification);
    }

    private void testDefaultImportance() {
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "IMPORTANCE_DEFAULT " +
                "TEST").setContentTitle("IMPORTANCE_DEFAULT TEST").setContentText("这是一条用于测试 " +
                "IMPORTANCE_DEFAULT 的信息").setWhen(System.currentTimeMillis()).setSmallIcon(R
                .drawable.ic_launcher_foreground).setAutoCancel(true)
                .build();
        notificationManager.notify(2, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotification(String id, CharSequence name, int importance) {
        NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

我们按一下第一个按钮测试一下高等级的通知,可以看到,当按钮按下的时候,高等级的通知会直接在顶部弹出来一个通知消息,如图所示:

然后手势下滑,可以把整个通知栏打开可以看到: 

针对那条通知消息,轻轻向右滑动(注意这里要轻滑,因为用力滑的话,这个消息就会被删除掉,这里的用力左滑右滑都可以清除消息)可以看到,有一个“时间”图标和一个“设置”图标: 

那么这个“时间”的图标,是用于设置一个时间,设置这个时间的意义在于在你设置的15分钟/30分钟/2小时的这个时间点,这个通知会第二次的发送:

然后这个“设置”的图标,点击可以看到,它是一个简单版的设置,这里可以快捷设置是否关闭这个消息渠道的通知: 

而点击第二个按钮,测试普通等级的通知,可以看到,按下第二个按钮的时候,并没有通知弹出来,而是在最上方,有一个小小的提示,因为我这里给的默认图标比较小,所以对截图这块儿我特地红框标记出来:

 

 

管理通知渠道

有时候不小心关闭了一些比较重要的渠道通知,我们就可以在代码中增加对渠道的管理,如果不小心关闭了重要通知,就会提醒先开启通知,才能接收消息:

package com.xxxx.notificationtest;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button testHigh;
    Button testDefault;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//如果当前版本大于等于26
            String id = "IMPORTANCE_HIGH TEST";
            CharSequence name = "测试 1";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotification(id, name, importance);

            id = "IMPORTANCE_DEFAULT TEST";
            name = "测试 2";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotification(id, name, importance);
        }

    }

    private void initView() {
        testHigh = findViewById(R.id.btn_test1);
        testHigh.setOnClickListener(onClickListener);

        testDefault = findViewById(R.id.btn_test2);
        testDefault.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_test1:
                    testHighImportance();//测试高等级通知
                    break;
                case R.id.btn_test2:
                    testDefaultImportance();//测试普通等级通知
                    break;
                default:
                    break;
            }
        }
    };

    private void testHighImportance() {
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = notificationManager.getNotificationChannel
                    ("IMPORTANCE_HIGH TEST");
            manualOpeningNotification(channel);
        }

        Notification notification = new NotificationCompat.Builder(this, "IMPORTANCE_HIGH TEST")
                .setContentTitle("IMPORTANCE_HIGH TEST").setContentText("这是一条用于测试 IMPORTANCE_HIGH" +
                        " 的信息").setWhen(System.currentTimeMillis()).setSmallIcon(R.drawable
                        .ic_launcher_foreground).setAutoCancel(true)
                .build();
        notificationManager.notify(1, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)//手动开启通知
    private void manualOpeningNotification(NotificationChannel channel) {
        if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
            Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
            intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
            startActivity(intent);
            Toast.makeText(this, "检测到您的通知已被关闭,请先将通知打开", Toast.LENGTH_SHORT).show();
        }
    }

    private void testDefaultImportance() {
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "IMPORTANCE_DEFAULT " +
                "TEST").setContentTitle("IMPORTANCE_DEFAULT TEST").setContentText("这是一条用于测试 " +
                "IMPORTANCE_DEFAULT 的信息").setWhen(System.currentTimeMillis()).setSmallIcon(R
                .drawable.ic_launcher_foreground).setAutoCancel(true)
                .build();
        notificationManager.notify(2, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotification(String id, CharSequence name, int importance) {
        NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService
                (NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值