Android 推送静音

自Android8.0开始,推送的设置与过去的版本不一样了。

它的推送静音设置,并没有单独的静音选项来设置,而是通过对通知的重要性(Importance)来间接设置的。

重要性分为4类:Urgent、High、Medium、Low。其中Medium与Low就没有声音了。

而且,可以对推送进行分类,通过NotificationChannel这个来设置了。

这意味着,针对不同的账号权限,比如工作账号、浏览账号,评论通知、私信通知等,可以设置不同的channel(通过对channel的个性化设置,比如是否有声音之类的)。

channel的获取方式,如下:

NotificationManager nfm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// 获取所有的
List<NotificationChannel> channels = nfm.getNotificationChannels();

// 获取指定id的channel
nfm.getNotificationChannel("channelId");

这个是获取到的系统备份的channel,对它进行设置并不会更改到系统中的设置。

想要设置推送方式,还是需要去系统中设置,可以通过代码弹出系统设置页面,如下:

public void gotoNotificationSetting() {
        ApplicationInfo appInfo = context.getApplicationInfo();
        String pkg = context.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Intent intent = new Intent();
                //这种方案适用于 API 26, 即8.0(含8.0)以上可以用
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid);
                context.startActivity(intent);

            } else {
                // 展示 应用的详细设置页面
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", pkg, null);
                intent.setData(uri);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            // 展示 系统设置 页面
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            context.startActivity(intent);
        }
    }

当然,也可以通过 ACTION_CHANNEL_NOTIFICATION_SETTINGS 来打开特定channel的通知设置页面。

发布了14 篇原创文章 · 获赞 1 · 访问量 935

猜你喜欢

转载自blog.csdn.net/hankesi/article/details/103962607
今日推荐