Android 桌面角標二三事

  • 2019 年 11 月 20 日
  • 筆記

我們廣大用戶對於通知消息欄和桌面角標都很熟悉,Google Android 是在 8.0 以後加入的,但是對於部分中國廠商較早就有嘗試,和尚今天對桌面角標進行簡單嘗試;

華為廠商

對於桌面角標的兼容處理,華為是最明確最容易處理的,官方文檔 清晰明了,和尚按照官方介紹嘗試如下:

集成方式

1. 許可權聲明
<uses-permission android:name="android.permission.INTERNET" />  <uses-permission android:name="com.huawei.android.launcher.permission.CHANGE_BADGE" />
2. 設置基本參數並將角標數傳遞給桌面應用
public static void setHuaweiBadge(Context context, int count) {      try {          Bundle badgeBundle = new Bundle();          badgeBundle.putString("package", "包名");          badgeBundle.putString("class", "啟動頁");          badgeBundle.putInt("badgenumber", count);          context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, badgeBundle);      } catch (Exception e) {          e.printStackTrace();      }  }
3. 在需要的位置調用設置桌面角標
setHuaweiBadge(getApplicationContext(), 10);

注意事項

  1. Bundle 參數中的 class 並非固定的 MainActivity 而是帶包名完整路徑的啟動 Activity,例如:com.test.name.SplashActivity;
  2. 建議在調用時添加 try-catch 異常處理,防止桌面不支援角標時介面拋異常;
  3. 若關閉角標顯示,可以發送 count = 0 的介面調用,可以根據需求在進入/退出 app 或前後台切換時進行角標的變更;

小米廠商

小米官網 提供了明確的角標處理方式,但小米的處理方式必須依賴通知欄消息才可以對桌面角標進行處理;

集成方式

1. 發送通知消息,注意 Android 8.0 以後需要設置通知渠道
private void notifyChannel() {      NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(          android.content.Context.NOTIFICATION_SERVICE);      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {          NotificationChannel channel =              new NotificationChannel(Constants.PUSH_CHANNEL_ID, "mi_push",                  NotificationManager.IMPORTANCE_HIGH);          if (notificationManager != null) {              notificationManager.createNotificationChannel(channel);          }      }      Notification notification = new NotificationCompat.Builder(this, "mi_push")          .setContentTitle("您收到一條消息")          .setContentText("設置桌面角標")          .setWhen(System.currentTimeMillis())          .setSmallIcon(R.drawable.icon)          .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))          .setAutoCancel(true)          .build();        getMiBadge(notification, 10);      notificationManager.notify(1, notification);  }
2. 在發送通知時,設置角標數
public static void getMiBadge(Notification notification, int count) {      try {          Field field = notification.getClass().getDeclaredField("extraNotification");          Object extraNotification = field.get(notification);          Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int.class);          method.invoke(extraNotification, count);      } catch (Exception e) {          e.printStackTrace();      }  }

注意事項

  1. 和尚測試時收到消息但是沒有更新桌面角標,認真閱讀官方文檔,發現消息類型不能是進度條樣式和常駐通知,注意需要 setOngoing(false);
  2. 小米的桌面角標在點擊當前推送消息或點擊應用圖標時,會自動取消角標數,不用單獨處理;
  3. 小米的桌面角標方式應用場景相對較少,須配合推送消息;

三星 / LG 廠商

三星和 LG 廠商的集成方式一致;都是通過廣播發送通知,然後再展示桌面角標;

集成方式

1. 許可權聲明
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />  <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
2. 發送廣播,參數傳遞內容基本與華為方式一致,注意 class_name 為啟動頁完整路徑
private static void setSumsungBadge(Context context, int count) {      Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");      intent.putExtra("badge_count", count);      intent.putExtra("badge_count_package_name", "包名");      intent.putExtra("badge_count_class_name", "啟動頁");      context.sendBroadcast(intent);  }
3. 在需要的位置調用設置桌面角標
setSumsungBadge(getApplicationContext(), 10);

聯想 ZUK 廠商

聯想 ZUK 廠商在開發者平台上提供了具體的角標展示方案,其中可設置 ids 對多圖標設置角標,和尚僅嘗試了主 app 圖標角標展示;

集成方式

1. 許可權聲明
<uses-permission android:name="android.permission.READ_APP_BADGE"/>
2. 可根據 ids 對具體圖標進行角標展示,也可以簡化直接設置主圖標角標
private static void setZUKBadge(Context context, int count) {      Bundle extra = new Bundle();      extra.putInt("app_badge_count", count);      context.getContentResolver().call(Uri.parse("content://com.android.badge/badge"), "setAppBadgeCount", null, extra);  }
3. 在需要的位置調用設置桌面角標
setZUKBadge(getApplicationContext(), 10);

OPPO 廠商

OPPO 開發者平台未提供處理桌面角標的方法,聯繫客服需要提供相關材料申請角標許可權,原則上只通過應用的 IM 類提醒功能角標申請;申請之後 OPPO 相關工作人員會單獨建群指導說明,和尚測試應用未通過要求,暫不支援;

VIVO / 魅族廠商

查閱 VIVO / 魅族 開發者平台,明確指出暫不支援桌面角標的展示,故和尚暫未嘗試;


為了良好的用戶體驗,桌面角標的展示應慎重合理,這也是部分嘗試暫不提供桌面角標展示的原因;而且在中國的應用大部分需要單獨集成各廠商的 SDK,和尚研究不深,有錯誤的話請多多指導!

Tags: