关于通知是否显示角标

应用发通知时,可以设置是否显示徽章:
设置接口:channel.setShowBadge(true);  修改为false既不显示。


411    private void createNotificationChannel() {
412        Log.d(TAG, "createNotificationChannel");
413        if (mNotificationManager == null) {
414            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
415            if (android.os.Build.VERSION.SDK_INT >= 26) {
416                String nameChannel = getString(R.string.app_name);
417                NotificationChannel channel;
418                channel = new NotificationChannel(CHANNEL_ID, nameChannel, NotificationManager.IMPORTANCE_LOW);
419                channel.setShowBadge(true);
420                channel.enableLights(false);
421                channel.enableVibration(false);
422                mNotificationManager.createNotificationChannel(channel);
423            }
424        }
425    }

构造 NotificationRankingUpdate获取:
frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java#makeRankingUpdateLocked
6242            showBadge.putBoolean(key, record.canShowBadge());

看看源码,当创建通知channel时:

frameworks/base/services/core/java/com/android/server/notification/RankingHelper.java

609    @Override
610    public void createNotificationChannel(String pkg, int uid, NotificationChannel channel,
611            boolean fromTargetApp, boolean hasDndAccess) {
612        Preconditions.checkNotNull(pkg);
613        Preconditions.checkNotNull(channel);
614        Preconditions.checkNotNull(channel.getId());
615        Preconditions.checkArgument(!TextUtils.isEmpty(channel.getName()));
616        Record r = getOrCreateRecord(pkg, uid);

getOrCreateRecord创建app通知channel record。

如可以adb pull data/syste<package name="com.jxh.shop.myapplication" show_badge="true" app_user_locked_fields="0" uid="10107">
<channel id="testNNN" name="testNN" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" />
<channel id="testNNNN" name="testNN" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
<channel id="testNN" name="testNN" importance="2" sound="content://settings/system/notification_sound" usage="5" content_type="4" flags="0" show_badge="true" />
</package>m/notification_policy.xml看看

创建了三个channel id,第一个设置不显示徽章,第二三个显示,show_badge="true"

这个XML是怎么保存的?

frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java

void readPolicyXml(InputStream stream, boolean forRestore)

将从XML读取通知记录

private void writePolicyXml(OutputStream stream, boolean forBackup)

将把通知数据写入到XML

这个XML文件怎么创建?

是在onStart()的调用init()函数,入参:

final File systemDir = new File(Environment.getDataDirectory(), "system");
new AtomicFile(new File(systemDir, "notification_policy.xml"), "notification-policy"),

猜你喜欢

转载自blog.csdn.net/snail201211/article/details/85679102