关于通知是否显示角标

应用发通知时,可以设置是否显示徽章:
设置接口: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/system/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"),

在/RankingHelper.java的

public void writeXml(XmlSerializer out, boolean forBackup)

方法中,将会调用NotificationChannel的writeXml,将具体channel id的信息写入到xml

 

/frameworks/base/core/java/android/app/NotificationChannel.java

697    private void writeXml(XmlSerializer out, boolean forBackup, @Nullable Context context)
698            throws IOException {
699        Preconditions.checkArgument(!forBackup || context != null,
700                "forBackup is true but got null context");
701        out.startTag(null, TAG_CHANNEL);
702        out.attribute(null, ATT_ID, getId());
703        if (getName() != null) {
704            out.attribute(null, ATT_NAME, getName().toString());
705        }
706        if (getDescription() != null) {
707            out.attribute(null, ATT_DESC, getDescription());
708        }
709        if (getImportance() != DEFAULT_IMPORTANCE) {
710            out.attribute(
711                    null, ATT_IMPORTANCE, Integer.toString(getImportance()));
712        }

749        if (canShowBadge()) {
750            out.attribute(null, ATT_SHOW_BADGE, Boolean.toString(canShowBadge()));
751        }
761
762        out.endTag(null, TAG_CHANNEL);
763    }
764

 

你可能感兴趣的:(Android学习笔记,notification,badge)