Android O - 单线通知 - 比如“Android系统 - USB充电此设备”

问题描述 投票:22回答:4

我想继续通知我的ForegroundService需要尽可能小的地方。我喜欢“Android系统 - USB充电此设备”的风格,但我找不到任何实例如何实现这一点。 enter image description here

谁能指出我正确的方向?

Update

如果为频道指定了重要性IMPORTANCE_MIN,则会为通知指定样式。

看起来没有办法使用内置风格的Androids来通知IMPORTANCE_MINForegroundService一起使用。

以下是IMPORTANCE_MIN的描述:

最小通知重要性:仅在阴影下方显示。这不应该与Service.startForeground一起使用,因为前台服务应该是用户关心的东西,因此将其通知标记为最低重要性并不具有语义意义。如果您从Android版本Build.VERSION_CODES.O开始执行此操作,系统将显示有关您的应用在后台运行的更高优先级通知。

android android-notifications android-8.0-oreo
4个回答
5
投票

要显示紧凑的单行通知(如收费通知),您必须创建一个优先级为IMPORTANCE_MIN的Notification Channel

@TargetApi(Build.VERSION_CODES.O)
private static void createFgServiceChannel(Context context) {
   NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_MIN);
   NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   mNotificationManager.createNotificationChannel(channel);
}

然后创建一个持续的通知:

public static Notification getServiceNotification(Context context) {
   NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "channel_id");
   mBuilder.setContentTitle("One line text");
   mBuilder.setSmallIcon(R.drawable.ic_notification);
   mBuilder.setProgress(0, 0, true);
   mBuilder.setOngoing(true);
   return mBuilder.build();
}

注意

请注意,我用IntentService而不是Service测试它,它的工作原理。此外,我刚刚检查了设置一个15秒的Thread.sleep()并且通知显示完美,直到IntentService停止。

有一些图像(抱歉一些文本是西班牙语,但我认为图像仍然有用):

Single line ongoing notification

如果您向下拖动并打开通知,则显示如下:

Single line ongoing notification opened

额外

如果您发现Android系统显示通知,指示所有正在使用电池的应用程序(具有持续服务的应用程序),您可以降级此类通知的优先级,它将显示为一行通知,如收费通知。

看看这个:

Battery applications

只需长按此通知,然后选择所有类别:

Chennel notification for battery applications

并将重要性设置为LOW:

enter image description here

下次,此“电池消耗”通知将显示为充电通知。


2
投票

您需要将通知优先级设置为Min,将Notification Channel重要性设置为Min,并禁用显示Notification Channel Badge。

这是我如何做的一个例子。我已经包括创建完整的通知以供参考

private static final int MYAPP_NOTIFICATION_ID= -793531;

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

String CHANNEL_ID = "myapp_ongoing";
CharSequence name = context.getString(R.string.channel_name_ongoing);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_MIN);
    channel.setShowBadge(false);

    notificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_stat_notification_add_reminder)
        .setContentTitle(context.getString(R.string.app_name))
        .setContentText(context.getString(R.string.create_new))
        .setOngoing(true).setWhen(0)
        .setChannelId(CHANNEL_ID)
        .setPriority(NotificationCompat.PRIORITY_MIN);

// Creates an intent for clicking on notification
Intent resultIntent = new Intent(context, MyActivity.class);
...

// The stack builder object will contain an artificial back stack
// for the
// started Activity.
// This ensures that navigating backward from the Activity leads out
// of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

notificationManager.notify(MYAPP_NOTIFICATION_ID, mBuilder.build());

1
投票

回答原来的问题:

似乎没有内置的方法在Android O上获得单行,持续通知ForegroundService。人们可以尝试添加自定义设计,但由于不同的手机有不同的通知设计,因此该解决方案难度不大。

但是有希望:)

在Android P上,NotificationChannelIMPORTANCE_LOW中优先级为PRIORITY_LOW的通知即使对于ForegroundService也会被压缩为单行。是啊!


0
投票

我通过创建一个空的自定义视图,使前景服务通知的大小变小:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


</LinearLayout>

然后像这样创建通知:

RemoteViews notifiactionCollapsed = new RemoteViews(getPackageName(),R.layout.notification_collapsed);
    Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.drawable.eq_icon)
            .setCustomContentView(notifiactionCollapsed)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setShowWhen(false)
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setOngoing(true)
            .setVisibility(NotificationCompat.VISIBILITY_SECRET)
            .build();
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
            notification);

这有助于降低通知的高度,但我仍然不确定如何隐藏通知图标。

© www.soinside.com 2019 - 2024. All rights reserved.