You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android锁屏状态下自定义通知实现方法咨询

实现Android锁屏状态下的自定义通知方案

嘿,我来帮你搞定锁屏下的自定义通知!结合你现有的NotifyService,咱们一步步来实现,覆盖不同Android版本的适配需求:

1. 先搞定基础配置:通知渠道与锁屏可见性

从Android 8.0(API 26)开始,所有通知必须绑定通知渠道,而且要设置通知在锁屏上的可见性,确保锁屏时能展示自定义内容:

首先在onCreate或者初始化的地方添加通知渠道创建逻辑:

private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "自定义锁屏通知渠道";
        String description = "用于展示锁屏下的自定义通知内容";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("LOCKSCREEN_NOTIFY_CHANNEL", name, importance);
        channel.setDescription(description);
        // 允许通知在锁屏显示
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

2. 创建自定义通知布局

res/layout下新建一个自定义布局文件,比如layout_custom_lockscreen_notification.xml,注意要使用系统兼容的控件(避免复杂自定义View,锁屏环境下可能不支持):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/tv_notify_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"
        android:text="自定义锁屏通知标题"/>

    <TextView
        android:id="@+id/tv_notify_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginTop="8dp"
        android:text="这是锁屏下显示的自定义通知内容"/>

</LinearLayout>

3. 在NotifyService中实现自定义锁屏通知

现在把这些整合到你的NotifyService里,替换原来的Intent部分,完整代码示例:

public class NotifyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();
        // 先创建通知渠道
        createNotificationChannel();
        sendLockscreenCustomNotification();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "自定义锁屏通知渠道";
            String description = "用于展示锁屏下的自定义通知内容";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("LOCKSCREEN_NOTIFY_CHANNEL", name, importance);
            channel.setDescription(description);
            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    private void sendLockscreenCustomNotification() {
        // 1. 构建自定义布局的RemoteViews
        RemoteViews customView = new RemoteViews(getPackageName(), R.layout.layout_custom_lockscreen_notification);
        // 可以动态修改布局内容
        customView.setTextViewText(R.id.tv_notify_title, "新的锁屏通知");
        customView.setTextViewText(R.id.tv_notify_content, "这是动态设置的锁屏通知内容");

        // 2. 点击通知后的跳转Intent
        Intent resultIntent = new Intent(this, YourTargetActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 3. 构建Notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "LOCKSCREEN_NOTIFY_CHANNEL")
                .setSmallIcon(R.drawable.ic_notify_small) // 必须设置小图标
                .setCustomContentView(customView) // 设置自定义内容
                .setCustomBigContentView(customView) // 适配展开状态
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) // 锁屏可见
                .setContentIntent(resultPendingIntent)
                .setAutoCancel(true);

        // 4. 发送通知
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(1001, builder.build());
    }
}

4. 关键注意事项

  • 权限要求:Android 13(API 33)及以上需要申请POST_NOTIFICATIONS权限,在Manifest中添加:
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    
    并在运行时动态申请该权限。
  • 锁屏权限:部分手机厂商可能有额外的锁屏通知限制,你可以引导用户到系统设置开启:
    Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
    startActivity(intent);
    
  • 布局兼容性:锁屏环境下不支持RecyclerViewWebView等复杂控件,尽量使用TextViewImageView等基础控件。

内容的提问来源于stack exchange,提问作者Derin S

火山引擎 最新活动