Android应用设置角标

前言

应用的角标提醒,这个功能初始是苹果自带的,后来其他Android手机厂商看到了这个角标的优点,纷纷在自己的操作系统中做了各自的角标功能,所以如果我们需要修改角标,需要根据厂商的要求进行适配。
部分机型完成适配,代码可能在某些机型上不起作用。

区分厂商

Android中使用android.os.Build.MANUFACTURER方法来获取当前设备的厂商,并且使用equalsIgnoreCase("xiaomi")方法来判断是否是改厂商的设备。

设置角标

可以前往对应厂商的开发者平台查看对应的文档,里面都有,只不过有些写的很粗略。

小米

小米手机无法单独设置角标,需要在现实通知的时候携带角标数量

/**
 * count 角标数量
 */
private static void setBadgeOfMIUI(Context context, int count) {
    NotificationManager notificationManager = (NotificationManager) context.getSystemService
            (Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
        Log.e("BadgeUtil", "小米设备角标设置失败!");
        return;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // 8.0之后添加角标需要NotificationChannel
        NotificationChannel channel = new NotificationChannel("badge", "badge",
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setShowBadge(true);
        notificationManager.createNotificationChannel(channel);
    }
    // 点击消息通知后跳转的页面,可以携带参数
    Intent intent = new Intent(context, WebActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    Notification notification = new NotificationCompat.Builder(context, "badge")
            .setContentTitle("消息通知标题")
            .setContentText("消息通知内容")
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ihive02))
            .setSmallIcon(R.drawable.ihive02)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setChannelId("badge")
            .setNumber(count)
            .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).build();
    // 小米
    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();
    }
    notificationManager.notify((int) (System.currentTimeMillis() / 1000), notification);
}

vivo

需要申请权限,并且改权限会弹出对应的权限申请弹窗,同意即可

<!--vivo角标-->
<uses-permission android:name="com.vivo.notification.permission.BADGE_ICON" />

然后代码中设置角标

private static void setBadgeOfVIVO(Context context, int count) {
    try {
        Intent intent = new Intent("launcher.action.CHANGE_APPLICATION_NOTIFICATION_NUM");
        intent.putExtra("packageName", context.getPackageName());
        String launchClassName = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()).getComponent().getClassName();
        intent.putExtra("className", launchClassName);
        intent.putExtra("notificationNum", count);
        context.sendBroadcast(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

注:vivo手机如果需要开启角标显示的话,还需要用户手动开启对应权限,需要在代码中进行跳转到手动开启权限的页面。

Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);

在这里插入图片描述

华为和荣耀

华为和荣耀的一起写,是因为我这边测试出来使用同一个代码分别在华为和荣耀手机上面都可以生效
权限的申请

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

代码中设置角标

private static void setBadgeOfHuaWei(Context context, int count) {
    //权限检查
    try {
        Bundle bundle = new Bundle();
        bundle.putString("package", context.getPackageName());
        bundle.putString("class", getLauncherClassName(context));
        bundle.putInt("badgenumber", count);
        context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher.settings/badge/"), "change_badge", null, bundle);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static String getLauncherClassName(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setPackage(context.getPackageName());
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (info == null) {
        info = packageManager.resolveActivity(intent, 0);
    }
    return info.activityInfo.name;
}

OPPO

OPPO手机无法申请角标应用,也就是无法设置角标。
官方客服人员回应:由于平台运营策略调整暂停数字角标申请,具体恢复时间待定。

Android应用程序中设置角标(通知栏的小图),通常是在Activity、Fragment或者Service等组件的Manifest文件(AndroidManifest.xml)中配置`Notification`对象。你可以通过以下几个步骤来设置角标: 1. 首先,创建一个`NotificationCompat.Builder`实例,并指定你想显示的通知的基本信息,如题、内容和小图。 ```java import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import androidx.core.app.NotificationCompat; NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID); notificationBuilder.setContentTitle(title) .setContentText(message) .setSmallIcon(R.drawable.ic_notification_icon); // 替换为你想要的小图资源ID ``` 2. 创建一个通知通道(Notification Channel)如果系统版本较高,需要这个,以便管理通知的行为和外观。 ```java String CHANNEL_ID = "my_channel_id"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "My Notification Channel", NotificationManager.IMPORTANCE_DEFAULT); channel.enableLights(true); // 设置灯闪烁 channel.setLightColor(Color.RED); // 设置灯颜色 channel.enableVibration(true); // 开启振动 NotificationManager notificationManager = context.getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 3. 使用`notify()`方法发送通知。 ```java int notificationId = 1; // 唯一识符,每次更新都应增加 notificationManager.notify(notificationId, notificationBuilder.build()); ``` 如果你想要在运行时动态更改角标,可以在特定条件下更新`Notification`对象并再次调用`notify()`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值