Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Notification.PRIORITY_MAX已弃用在顶部显示通知的替代方案是什么?_Android_Android Notifications - Fatal编程技术网

Android Notification.PRIORITY_MAX已弃用在顶部显示通知的替代方案是什么?

Android Notification.PRIORITY_MAX已弃用在顶部显示通知的替代方案是什么?,android,android-notifications,Android,Android Notifications,如何在顶部显示android通知? setPriority(通知.优先级\u MAX) as Notification.PRIORITY_MAX已弃用有什么替代方案 NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentIntent(pendingIntent) .setSmallIcon(R.mipmap.ic

如何在顶部显示android通知?
setPriority(通知.优先级\u MAX)

as Notification.PRIORITY_MAX已弃用有什么替代方案

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notification Title")
                .setContentText("Notification Text")
                .setPriority(Notification.PRIORITY_MAX)
                .setAutoCancel(true);

在android O中引入了。特别是使用定义通道。在文档中,您可以看到重要性的概念,而这正是取代优先级的概念。

priority\u MAX
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

    // Configure the notification channel.
    notificationChannel.setDescription("Channel description");
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
    notificationChannel.enableVibration(true);
    notificationManager.createNotificationChannel(notificationChannel);
}


NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

notificationBuilder.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Hearty365")
        .setPriority(Notification.IMPORTANCE_HIGH)
        .setContentTitle("Default notification")
        .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
        .setContentInfo("Info");

notificationManager.notify(/*notification id*/1, notificationBuilder.build());
此常量在API级别26中被弃用。用高重要性代替

最高优先级

int优先级_最大值

此常量在API级别26中被弃用。 用高重要性代替

最高优先级,用于需要用户及时注意或输入的应用程序最重要的项目

常量值:2(0x00000002)


Android O通知只需遵循三个步骤

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "Hello";// The user-visible name of the channel.
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                mNotificationManager.createNotificationChannel(mChannel);
            }

mNotificationManager.notify(notificationId, notificationBuilder.build());

为所有android版本设置最大优先级的工作代码

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

    // Configure the notification channel.
    notificationChannel.setDescription("Channel description");
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
    notificationChannel.enableVibration(true);
    notificationManager.createNotificationChannel(notificationChannel);
}


NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

notificationBuilder.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Hearty365")
        .setPriority(Notification.IMPORTANCE_HIGH)
        .setContentTitle("Default notification")
        .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
        .setContentInfo("Info");

notificationManager.notify(/*notification id*/1, notificationBuilder.build());
从Android O(API 26)开始,通知级别的优先级已被弃用。它被通道级别的重要性所取代,通知现在必须放在特定的通道中

如果您在Android O或更高版本上运行,则必须创建一个频道并定义频道的重要性

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
            CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
如果您需要保持与旧Android版本的向后兼容性,那么您必须继续在通知级别定义优先级

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            ...
            .setPriority(Notification.PRIORITY_MAX)
            ...
不幸的是,它会生成关于优先级\最大折旧的警告

可以避免使用优先级_MAX常量的NotificationCompat版本

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        ...
        .setPriority(NotificationCompat.PRIORITY_MAX)
        ...

请注意,您不再具有“在顶部显示android通知”的完全控制权。@commonware您需要在通知频道本身中设置
重要性\u MAX
。检查我的答案。请专注于问题(优先级最高)。感谢您解释要做什么,但也要说明如何做(如何处理重要性)。感谢Neon Warge使用
NotificationManager.importance\u HIGH)
而不是
NotificationManager.importance\u MAX)