Android8的通知

参考:https://blog.youkuaiyun.com/htwhtw123/article/details/79838742

1、通知较之前的最大不同是需要添加渠道信息

NotificationManager mNotificationManager = (NotificationManager)
   getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        //只在Android O之上需要渠道
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
                    CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                    //如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,
                    //通知才能正常弹出
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);


        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setAutoCancel(true);

        mNotificationManager.notify(notificationId, builder.build());

渠道的使用步骤

  • 初始化
NotificationChannel(String id, CharSequence name, int importance)
  • 添加渠道
NotificationManager的createNotificationChannel方法
  • 通知builder时添加渠道id
NotificationCompat.Builder(Context context, String channelId)

2、渠道属性

// 配置通知渠道的属性
notificationChannel .setDescription("渠道的描述");
// 设置通知出现时的闪灯(如果 android 设备支持的话)
notificationChannel .enableLights(true);
notificationChannel .setLightColor(Color.RED);
// 设置通知出现时的震动(如果 android 设备支持的话)
notificationChannel .enableVibration(true);
notificationChannel .setVibrationPattern(new long[]{1000, 2000, 1000,3000});
//如上设置使手机:静止1秒,震动2秒,静止1秒,震动3秒

3、设置里可以看到管理这些渠道(效果图在参考网址有)

4.参考:https://blog.youkuaiyun.com/y331271939/article/details/83342519

/*
* 判断App通知是否打开(总开关)
* true:开
*/
public boolean areNotificationsEnabled() {
    NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
    return notificationManagerCompat.areNotificationsEnabled();
}

/*
* 判断当前渠道通知是否打开
* true:开
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public boolean areChannelsEnabled(@NonNull String channelId) {
    NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
    if (notificationChannel != null && notificationChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
        return false;
    }
    return true;
}

/*
* 跳转到渠道设置页
*/
public void gotoChannelSetting(@NonNull String channelId, @NonNull Context context) {
    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
    intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
    context.startActivity(intent);
}

5、带进度条,参考:http://www.cnblogs.com/panhouye/p/6147332.html

主要是使用builder.setProgress(100, n/s,false);
        mNotificationManager.notify(notificationId,builder.build());
  //每次有变化就重绘更新
  true:不确定型进度条;false:确定型进度条;100是默认的,这样一般以百分比来进行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值