Android 8.0适配之Notification

上一篇博客介绍了8.0系统中的应用图标适配,还不了解的可以看一下8.0 应用图标适配。

这篇博客主要介绍Android 8.0系统之后的Notificatioin,这也是8.0适配中的重要部分

Android 8.0 系统之后,Google推出了通知渠道的概念。那什么是通知渠道尼?通知渠道是开发人员在创建通知的时候为每个通知指定的渠道,你也可以理解成是通知类型。属于同一通知渠道的通知可以进行统一管理。每一个开发人员在开发App的时候都可以自由的创建通知渠道,但是这些通知渠道的控制权都是掌握在用户手里的,简单点说就是用户可以更改渠道的重要性,从而决定通知的提示状态,是弹出提示,震动提示,声音提示等等。这种操作也很好的提高了用户的体验,不会像之前不管是什么通知都会从顶部弹出提示。

开始适配

开始适配之前我们还是看一下已经适配了8.0系统的App通知管理的显示:Gmail

 可以看到Gamil分了两个通知渠道。

下面开始正式适配之旅:

准备工作:首先需要将Android Studio 的build.gradle(app)中 targetSdkVersion 设置成26或更高。如果你的手边没有8.0以上的手机,可以开启一个8.0以上系统的模拟器。

创建Channel

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "channelId";
            String channelName = "这是用来测试的notification";
            createNotificationChannel(channelId, channelName, NotificationManagerCompat.IMPORTANCE_HIGH);
        }

 @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.createNotificationChannel(channel);
    }

从上面的代码可以看出创建一个NotificationChannel对象需要三个参数,下面看一下系统对这三个参数的解释:

/**
 * Creates a notification channel.
 *
 * @param id The id of the channel. Must be unique per package. The value may be truncated if
 *           it is too long.
 * @param name The user visible name of the channel. You can rename this channel when the system
 *             locale changes by listening for the {@link Intent#ACTION_LOCALE_CHANGED}
 *             broadcast. The recommended maximum length is 40 characters; the value may be
 *             truncated if it is too long.
 * @param importance The importance of the channel. This controls how interruptive notifications
 *                   posted to this channel are.
 */

channelId:渠道 Id,全局必须是唯一的,并且长度不能太长,不能超过 MAX_TEXT_LENGTH = 1000,否则会被缩短;

channelName:渠道名称,这个是展示给用户看的,用来告诉用户这个渠道是干什么的;

importance:渠道重要等级,等级不同在手机桌面上的展示不同。需要注意的是代码中设置的渠道重要等级只是一个初始值,用户可以手动更改渠道的重要等级。具体有哪些等级以及对应等级的通知展现形式会在下面介绍。

注意:NotificationChannel是在8.0系统以后才出现的,所以一定要注意判断当前系统

发送一个通知

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private NotificationManager mManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    private void initView() {
        findViewById(R.id.btn_send_notification).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send_notification:
                sendNotification();
                break;
        }
    }

    private void sendNotification() {
        String channelId = "channelId";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "这是用来测试的notification";
            createNotificationChannel(channelId, channelName, NotificationManagerCompat.IMPORTANCE_HIGH);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo));
        builder.setContentTitle("title");
        builder.setContentText("contentText");
        Notification notification = builder.build();
        mManager.notify(1, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        mManager.createNotificationChannel(channel);
    }
}

 点击按钮发送通知,我这里设置的通知等级是 NotificationManagerCompat.IMPORTANCE_HIGH,通知会弹出并伴有提示音。在看一下App的通知管理中显示

 我们创建了一个channelName为“这是用来测试的notification”的通知渠道,点击进去:

 这个渠道的主要信息,点击"重要程度"可以修改渠道的重要等级。

我们在初始化渠道重要等级的时候可以设置哪些等级尼以及对应哪些桌面展示尼?

IMPORTANCE_NONE:   不提示,不展示
IMPORTANCE_MIN:    不提示,在通知下拉栏会展示,但是是收起的
IMPORTANCE_LOW:    会在状态栏中显示,但不会弹窗,通知下拉栏会展示
IMPORTANCE_DEFAULT:会在状态栏中显示,允许有声音提示,但不会弹窗,通知下拉栏会展示
IMPORTANCE_HIGH:   会弹窗提示,允许有提示音
IMPORTANCE_MAX:    会弹窗提示,允许有提示音,可以使用全屏

读取通知渠道配置权限:

如果我们的某个功能是必须按照指定要求来配置通知渠道才能使用,开发人员可以读取到通知渠道的配置信息,然后提示用户手动更改通知渠道配置。

 String channelId = "channelId";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "这是用来测试的notification";
            createNotificationChannel(channelId, channelName, NotificationManagerCompat.IMPORTANCE_HIGH);
            NotificationChannel channel = mManager.getNotificationChannel(channelId);
            if (channel.getImportance() == NotificationManagerCompat.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);
            }
        }

以上就是对8.0系统中通知的适配,下面介绍8.0系统后通知新增的功能

新增功能

1,慢慢拖动通知向左或是向右滑动,会出现两个按钮

 

 第一个时钟一样的按钮可以设置通知提醒延时,比如我可以将这条通知设置成一小时之后在弹出提醒

第二个设置形状的按钮可以用来对通知渠道进行屏蔽和配置,用户可以对App中的每一个通知渠道进行配置修改,从而来改变通知的提示状态。

2,显示未读角标

ios系统一致都有这个功能,终于在8.0之后Google也引入了,但不像苹果那样将App中未读通知的数量显示在应用图标上,而是在应用图标上有一个点提示。

 修改代码:

private void sendNotification() {
        String channelId = "channelId";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "这是用来测试的notification";
            createNotificationChannel(channelId, channelName, NotificationManagerCompat.IMPORTANCE_HIGH);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo));
        builder.setContentTitle("title");
        builder.setContentText("contentText");
        builder.setNumber(5);
        Notification notification = builder.build();
        mManager.notify(1, notification);
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        channel.setShowBadge(true);
        mManager.createNotificationChannel(channel);
    }

主要修改了两个地方:

channel.setShowBadge(true);
builder.setNumber(5);

setNumber(int number) ;传入未读消息的数量

长按应用图标:

 展示一个弹框,弹框中显示未读通知的数量,以及通知的信息。如果不需要这个功能,需要channel.setShowBadge(false);

3,通知超时

builder.setTimeoutAfter(long timeOut);

指定一个超时时间,如果到了这个时间这个通知还在,系统会制动将这个通知取消

4,通知清除回调

8.0系统之后可以根据清除回调来判断是用户清除还是应用自己清除

需要我们事项NotificaitonListenerService类的带有三个参数的onNotificationRemoved();方法

@RequiresApi(api = Build.VERSION_CODES.O)
public class RemoveNotificationService extends NotificationListenerService {

    private static final String TAG = "RemoveNotificationServi";

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
        super.onNotificationRemoved(sbn, rankingMap, reason);
        Log.e(TAG, "onNotificationRemoved: "+sbn.toString()+"  reason = " +reason +"  "+REASON_CANCEL+"    "+REASON_LISTENER_CANCEL);
    }
}

清单文件配置

 <service android:name=".RemoveNotificationService"
                 android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
               <action android:name="android.service.notification.NotificationListenerService"/>
            </intent-filter>

        </service>

最后一步,手动开启通知访问权限,设置>应用和通知>高级>特殊应用权限->通知使用权。好吧,这么复杂的操作用户应该会放弃吧。

 以上就是对8.0之后通知的适配与介绍,有不足的地方欢迎指正。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值