如何制作 Android 不弹出的本地通知,只显示在通知列表中?

How do I make an Android local notification that doesn't pop-up, just shows up on the notification list?

我正在制作一种闹钟应用程序。我有一个特殊的闹钟屏幕,在闹钟时间到来时显示。那很好用。但如果用户暂停闹钟,我也希望在通知列表中有一个条目。但是,当我尝试使用 NotificationBuilder 使用常规通知时,它会与我的闹钟 activity 一样在屏幕上弹出(不是全屏 - 使用透明度)。必须有一种方法可以将通知放入列表中,以便用户可以在那里看到它,但不会在警报时间弹出。

这是通过将通知优先级设置为最低来完成的。 例如:

NotificationCompat.Builder b = new NotificationCompat.Builder(context, "MyReminder");
b.setPriority(NotificationCompat.PRIORITY_MIN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String id = MyApp.getNotificationChannel(context);
    b.setChannelId(id);
}

对于 Oreo 及更高版本的注意事项,您还需要设置通知渠道的优先级:

final private static String MyChannelID = "MyChannel_1";
private static NotificationChannel channel;

static private String getNotificationChannel(Context context) {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (channel == null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = "My Reminders";
            String description = "My Reminders pre-scheduled using My app";
            int importance = NotificationManager.IMPORTANCE_MIN;
            channel = new NotificationChannel(MyChannelID, name, importance);
            channel.setDescription(description);
            channel.enableLights(true);
            // Sets the notification light color for notifications posted to this
            // channel, if the device supports this feature.
            channel.setLightColor(Color.RED);

            channel.enableVibration(true);

            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            getNotificationManager().createNotificationChannel(channel);
        }
    }
    return MyChannelID;
}


static public NotificationManager getNotificationManager() {
    Context context = App.getContext();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getSystemService(NotificationManager.class);
    }
    else {
        return (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    }
}

我没有检查每个优先级的行为,但 MINIMUM 和 IMPORTANCE_MIN 工作。如果您有更具体的行为想要获得,您可以查看文档并尝试其他优先级。

您创建的频道需要使用低优先级

NotificationManager.IMPORTANCE_LOW  or NotificationManager.IMPORTANCE_MIN 

来自 Android Oreo,您需要使用通知渠道来创建通知。

例如:当您创建一个频道时,id为"Alerts", 您可以定义它的属性,例如 importance 、声音等...

使用的各种重要性属性如下:

IMPORTANCE_DEFAULT: 默认通知重要性:无处不在,发出噪音,但不影响视觉。 常数值:3

IMPORTANCE_HIGH : 更高的通知重要性:无处不在,吵闹偷看。 可以使用全屏意图。

[这将继续显示在其他应用程序之上,并再次像 whatsapp 消息通知一样] 这之所以有效,是因为它很重要。 常数值:4

IMPORTANCE_LOW 通知重要性低:无处不在,但不打扰。 常数值:2

IMPORTANCE_MIN 最低通知重要性:仅在阴影下显示,低于首屏。 常数值:1

因此,一旦您定义了频道属性,它就只能由用户通过通知意图进行更改。 我在这里提到的:

使用低优先级/最小重要性可能会解决您的问题

你用setNotificationSilent()

像这样:

new NotificationCompat.Builder(this)
    .setContentTitle("dasf")
    .setStyle(new NotificationCompat.BigTextStyle().bigText("adsf"))
    .setContentText(getText(R.string.text))
    .setSubText("adsf" + " " + getString(R.string.bdt))
    .setNotificationSilent()
    .setSmallIcon(R.drawable.ic_notification);