关于通知是否显示角标

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

<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>

创建了三个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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值