在小米手機上用 ShortcutBadger 實作桌面圖示的數字標記
最近在做一個案子,需要讓 App 的圖示角落顯示數字標記。但是原生 Android 並沒有提供這個功能,而是各家廠商在他們的 Launcher 上面下功夫;直到 Android 8.0 才終於有了......一個小點標記。
下面是官方網站的示意圖
然而這個並不符合我的需求,於是就在網路上找到了一個滿有名的專案:ShortcutBadger
ShortcutBadger 支援的廠牌不少,使用也簡單。
而他的 GitHub Wiki 上有特別提到小米手機的 MIUI 6 以上的版本需要用比較特別的作法
https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support
因為 MIUI 6 以上的標記數字是跟該 App 的通知數量連動的,因此範例的作法是透過 IntentService 來發送通知。
其中這個方法裡面其實就是小米開發者網站上所教導的方法
https://dev.mi.com/console/doc/detail?pId=939
好不容易整合好後,卻發現了一個現象。那就是每當出現數字後,點擊 App 圖示進入再返回桌面後,數字就會消失,但通知列表卻還有 App 的通知。以此推斷,只要進入過 App 之後,MIUI 會視為已經讀取過通知而把數字移除。因此,這裡要做的就是在每次退出 App 之後都要更新通知數量。
另外,以上方法目前發現在 Android 8.0 + MIUI 10 沒有作用。
可能需要等官方再公佈新方法,或者請各位高手自行研究了~
下面是官方網站的示意圖
然而這個並不符合我的需求,於是就在網路上找到了一個滿有名的專案:ShortcutBadger
ShortcutBadger 支援的廠牌不少,使用也簡單。
而他的 GitHub Wiki 上有特別提到小米手機的 MIUI 6 以上的版本需要用比較特別的作法
https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support
因為 MIUI 6 以上的標記數字是跟該 App 的通知數量連動的,因此範例的作法是透過 IntentService 來發送通知。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
startService( | |
new Intent(MainActivity.this, BadgeIntentService.class).putExtra("badgeCount", badgeCount) | |
); |
https://dev.mi.com/console/doc/detail?pId=939
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); |
好不容易整合好後,卻發現了一個現象。那就是每當出現數字後,點擊 App 圖示進入再返回桌面後,數字就會消失,但通知列表卻還有 App 的通知。以此推斷,只要進入過 App 之後,MIUI 會視為已經讀取過通知而把數字移除。因此,這裡要做的就是在每次退出 App 之後都要更新通知數量。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 取消通知 | |
mNotificationManager.cancel(notificationId); | |
//notificationId++; | |
... | |
// 發出通知 | |
ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); | |
mNotificationManager.notify(notificationId, notification); |
可能需要等官方再公佈新方法,或者請各位高手自行研究了~
留言
張貼留言