美文网首页
Android APP桌面启动图标未读信息小红点实现——在巨人的

Android APP桌面启动图标未读信息小红点实现——在巨人的

作者: Cabird | 来源:发表于2019-05-17 11:33 被阅读0次

让一个码农辛勤耕作的最大动力,是需求。 ——by Mr Lu

没错,需求就这样来了。
小红点,正式一点的叫法【图标角标】
目前,Android官方生态里没有完善的小红点实现方案,各厂商的情况大家都懂的,所以适配是很恶心而且绕不过的坎

我的方案是在GitHub上一个
5k+ stars项目https://github.com/leolin310148/ShortcutBadger
的基础上做了一些淌坑

ShortcutBadger项目最新版本身已经相对很完善了,但因为Android生态的混乱,可能现在这是一个完善的方案,过段时间就不一定了。所以在使用的过程中发现了一些问题,主要有一下两个:

1.华为手机(我手头是9.0的设备)不显示
解决方案,参考华为官方开发者文档https://developer.huawei.com/consumer/cn/devservice/doc/30802
我的实际操作是加上权限

<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE "/>

就好了,很明显,这是华为自定义的权限。

是时候引用一句名言了

官方文档是个好东西,如果有的话。 ——by Mr Lu

2.三星手机(Android8.0+)不显示
关于三星手机适配,ShortcutBadger已经做了很多工作,debug代码跟到底,一切ok,就是不显示。官方文档没找到,然后找项目issues,果然有人提到类似的问题,顺着看到了思路
https://developer.android.com/training/notify-user/badges#set_custom_notification_count
没错,google官方文档,核心思路,通过Notification实现,设置的消息个数可用在Badger角标

Notification notification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setContentTitle("New Messages")
        .setContentText("You've received 3 new messages.")
        .setSmallIcon(R.drawable.ic_notify_status)
        .setNumber(messageCount)
        .build();

实践证明,发通知后角标就显示了;进一步发现,只有通知栏有通知,ShortcutBadger统一设置角标的方法就生效了,说明三星8.0+显示角标必须有Notification

ShortcutBadger.applyCount(Context context, int badgeCount);

最终的处理

if (Build.MANUFACTURER.equalsIgnoreCase("samsung") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startService(new Intent(MainActivity.this, BadgeIntentService.class).putExtra("badgeCount", badgeCount));
}
ShortcutBadger.applyCount(context, badgeCount);

BadgeIntentService服务的作用是开启Notification,相关代码ShortcutBadger项目的demo中有

public class BadgeIntentService extends IntentService {

    private static final String NOTIFICATION_CHANNEL = "me.leolin.shortcutbadger.example";

    private int notificationId = 0;

    public BadgeIntentService() {
        super("BadgeIntentService");
    }

    private NotificationManager mNotificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            int badgeCount = intent.getIntExtra("badgeCount", 0);

//            mNotificationManager.cancel(notificationId);
            mNotificationManager.cancelAll();
            if (badgeCount==0) {
                return;
            }
            notificationId++;

            Notification.Builder builder = new Notification.Builder(getApplicationContext())
                    .setContentTitle("NewMessages")
                    .setContentText("You've received "+badgeCount+" messages.")
                    .setSmallIcon(R.drawable.ic_launcher);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                setupNotificationChannel();

                builder.setChannelId(NOTIFICATION_CHANNEL);
            }

            Notification notification = builder.build();
            ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount);
            mNotificationManager.notify(notificationId, notification);

        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void setupNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, "ShortcutBadger Sample",
                NotificationManager.IMPORTANCE_DEFAULT);

        mNotificationManager.createNotificationChannel(channel);
    }
}

总结

以上只是我实际开发遇到的一些坑,此外肯定还有其他各种各样的坑,其实issue本身并不可怕,关键是分析问题的思路,学会借助各种资源,官方文档、社区大佬等等。最后,感谢ShortcutBadger项目的作者# leolin310148

相关文章

网友评论

      本文标题:Android APP桌面启动图标未读信息小红点实现——在巨人的

      本文链接:https://www.haomeiwen.com/subject/fxrnaqtx.html