Android 桌面角標二三事

      我們廣大用戶對於通知消息欄和桌面角標都很熟悉,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,小菜研究不深,有錯誤的話請多多指導!

來源: 阿策小和尚

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章