HeadsUp 的通知持续时间 [英] Notification Duration for HeadsUp

查看:131
本文介绍了HeadsUp 的通知持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将抬头通知的持续时间设置为无限制?现在它只显示 5 秒钟.已经尝试了不同的事情,比如改变类别.但持续时间总是 5 秒.

Is it possible to set the duration of the headsup notification to unlimited? right now its displayed just for 5 seconds. Already tried different things like changing the category. But the duration always is 5 seconds.

这是我的代码:

Notification notification =
            notificationBuilder
                    .setCategory(Notification.CATEGORY_CALL)
                    .setContentText("TKSE Werk Duisburg")
                    .setSmallIcon(R.drawable.ic_tk_individual_signet_logo)
                    .setOngoing(true)
                    .setAutoCancel(false)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setContentIntent(contentIntent)
                    .setCustomHeadsUpContentView(viewNotificationHeadsUp)                                                      .setCustomContentView(viewNotificationSmall)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setVibrate(new long[20]).build();

在此线程上尝试了相同的操作:Controlling Android Notification Duration for HeadsUp 但它对我没有帮助.

Tried the same things like on this thread: Controlling Android Notification Duration for HeadsUp but it did not help me.

有趣的事实:在我的开发者手机上,三星 S5 mini -> 显示没有时间限制在另一部开发者手机上,三星 S7 -> 显示 5 秒

Interesting fact: On my Developer Phone a Samsung S5 mini -> its displayed with no time limit On another Developer Phone a Samsung S7 -> its displayed 5 seconds

推荐答案

这应该有效.我添加了额外的 onGoing(true) &category call 用于通知类别.

This should work. I have added extra onGoing(true) & category call for notification category.

   NotificationManager notificationManager = (NotificationManager) this.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_MAX);

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

    // assuming your main activity
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setCategory(Notification.CATEGORY_CALL)
            .setOngoing(true)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Hearty365")
            .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle("Default notification")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .setFullScreenIntent(pendingIntent,true)
            .setContentInfo("Info");

    notificationManager.notify(/*notification id*/1, notificationBuilder.build());

附注.导入 android.app.Notification;用于设置通知类别(来电)

PS. import android.app.Notification; for setting notification category (call)

这篇关于HeadsUp 的通知持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆