讓一個碼農(nóng)辛勤耕作的最大動力,是需求。 ——by Mr Lu
沒錯,需求就這樣來了。
小紅點,正式一點的叫法【圖標(biāo)角標(biāo)】
目前,Android官方生態(tài)里沒有完善的小紅點實現(xiàn)方案,各廠商的情況大家都懂的,所以適配是很惡心而且繞不過的坎
我的方案是在GitHub上一個
5k+ stars項目https://github.com/leolin310148/ShortcutBadger
的基礎(chǔ)上做了一些淌坑
ShortcutBadger項目最新版本身已經(jīng)相對很完善了,但因為Android生態(tài)的混亂,可能現(xiàn)在這是一個完善的方案,過段時間就不一定了。所以在使用的過程中發(fā)現(xiàn)了一些問題,主要有一下兩個:
1.華為手機(我手頭是9.0的設(shè)備)不顯示
解決方案,參考華為官方開發(fā)者文檔https://developer.huawei.com/consumer/cn/devservice/doc/30802
我的實際操作是加上權(quán)限
<uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE "/>
就好了,很明顯,這是華為自定義的權(quán)限。
是時候引用一句名言了
官方文檔是個好東西,如果有的話。 ——by Mr Lu
2.三星手機(Android8.0+)不顯示
關(guān)于三星手機適配,ShortcutBadger已經(jīng)做了很多工作,debug代碼跟到底,一切ok,就是不顯示。官方文檔沒找到,然后找項目issues,果然有人提到類似的問題,順著看到了思路
https://developer.android.com/training/notify-user/badges#set_custom_notification_count
沒錯,google官方文檔,核心思路,通過Notification實現(xiàn),設(shè)置的消息個數(shù)可用在Badger角標(biāo)
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();
實踐證明,發(fā)通知后角標(biāo)就顯示了;進(jìn)一步發(fā)現(xiàn),只有通知欄有通知,ShortcutBadger統(tǒng)一設(shè)置角標(biāo)的方法就生效了,說明三星8.0+顯示角標(biāo)必須有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服務(wù)的作用是開啟Notification,相關(guān)代碼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);
}
}
總結(jié)
以上只是我實際開發(fā)遇到的一些坑,此外肯定還有其他各種各樣的坑,其實issue本身并不可怕,關(guān)鍵是分析問題的思路,學(xué)會借助各種資源,官方文檔、社區(qū)大佬等等。最后,感謝ShortcutBadger項目的作者# leolin310148