简体   繁体   English

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

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

I am making a type of alarm app.我正在制作一种警报应用程序。 I have a special alarm screen that I show when the alarm time comes.我有一个特殊的闹钟屏幕,可以在闹钟时间到来时显示。 That works fine.这很好用。 But I also want there to be an entry in the notifications list if the user snoozes the alarm.但我也希望在用户暂停闹钟时在通知列表中有一个条目。 But when I try to use a regular Notification using NotificationBuilder, it pops up on the screen at the same as my alarm activity (which is not full screen - uses transparency).但是,当我尝试使用 NotificationBuilder 使用常规通知时,它会与我的警报活动(不是全屏 - 使用透明度)同时在屏幕上弹出。 There must be a way to get the notification into the list, so the user will see it there, but not have it pop up at alarm time.必须有一种方法可以将通知放入列表中,这样用户就会在那里看到它,但不会在闹钟时弹出它。

You need to use a low priority for channel you created 您需要为创建的频道使用低优先级

NotificationManager.IMPORTANCE_LOW  or NotificationManager.IMPORTANCE_MIN 

From Android Oreo, you need to use notification channels for creating Notifications. 在Android Oreo中,您需要使用通知渠道来创建通知。

For Example : When you create a channel, with id "Alerts" , you can define its properties like, its importance , sound etc... 例如:当您创建一个ID为“ Alerts”的频道时,您可以定义其属性,例如重要性 ,声音等。

The various Importance Attributes used are as follows : 所使用的各种重要性属性如下

IMPORTANCE_DEFAULT: Default notification importance: shows everywhere, makes noise , but does not visually intrude. IMPORTANCE_DEFAULT:默认通知重要性: 显示在任何地方,会发出声音 ,但不会视觉干扰 Constant Value: 3 常数:3

IMPORTANCE_HIGH : Higher notification importance: shows everywhere, makes noise and peeks . IMPORTANCE_HIGH:较高的通知重要性:随处可见, 发出声音和窥视 May use full screen intents. 可以使用全屏意图。

[This will keep showing over other apps and again like whatsapp message notification] This works so because of its importance. [这将继续显示在其他应用程序上,并再次显示类似whatsapp消息通知的内容]由于其重要性,因此可以正常工作。 Constant Value: 4 常数:4

IMPORTANCE_LOW Low notification importance: shows everywhere, but is not intrusive. IMPORTANCE_LOW通知重要性低:到处显示,但不具干扰性。 Constant Value: 2 常数值:2

IMPORTANCE_MIN Min notification importance: only shows in the shade, below the fold. IMPORTANCE_MIN最低通知重要性:仅在折叠下方的阴影中显示。 Constant Value: 1 常数值:1

So once you define a channels properties it could be changed by only the user through notification intent. 因此,一旦定义了通道属性,只有用户可以通过通知意图对其进行更改。 Which i mentioned here : https://stackoverflow.com/a/54199316/7039593 我在这里提到的: https : //stackoverflow.com/a/54199316/7039593

Using a low priority / min min importance may solve your problem 使用低优先级/分钟/最低重要性可能会解决您的问题

This is done by setting the notification priority to MINIMUM. 这是通过将通知优先级设置为MINIMUM来完成的。 For example: 例如:

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);
}

Note for Oreo and higher you need to also set the priority of the notification channel: 对于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);
    }
}

I didn't check the behavior of every priority, but MINIMUM and IMPORTANCE_MIN work. 我没有检查每个优先级的行为,但是MINIMUM和IMPORTANCE_MIN起作用。 If you have more specific behaviors you want to get, you can look at the documentation and play with other priority levels. 如果您想要获得更多特定的行为,则可以查看文档并使用其他优先级。

you use setNotificationSilent()你使用setNotificationSilent()

like this:像这样:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM