Android 通知持续时间

时间:2021-03-24 06:08:49

标签: android notifications notificationmanager

有没有办法让安卓通知在屏幕上停留超过 4 秒。

例如:

电话应用程序有来电通知,当此通知出现(来电)时,它不会离开屏幕,直到用户与通知互动(接听电话/拒绝电话/转到电话应用程序)。但是,我还没有看到让通知在屏幕(前台)上持续超过 4 秒的方法。即使我正在设置 FLAG_INSISTENT 和 FLAG_NO_CLEAR 通知最后在屏幕上可见 4 - 5 秒...

FLAG_INSISTENT - 在高优先级通知中持续发出哔哔声和振动,但通知仍会在 4 秒后从屏幕上消失,用户需要向下拖动栏才能“看到”通知。

我正在寻找一种方法,以与手机应用程序相同的方式放置通知,这意味着在用户与其交互之前将通知一直显示在屏幕上。

以下是主要活动的示例:

我的 MainActivity.java:

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private NotificationManagerCompat notificationManager;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager = NotificationManagerCompat.from(this);
        Intent YesReceive = new Intent(this, com.eddieharari.notifi.Data.class);
        YesReceive.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntentYes = PendingIntent.getActivity(this,12345, YesReceive,
                0);

        Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("My Notify TITLE")
                .setContentText("This is my notification Message")
                .setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setOngoing(false)
                .setAutoCancel(true)
                .setContentIntent(pendingIntentYes)
                .setPriority(Notification.PRIORITY_MAX)
                .build();
        notification.flags = notification.flags | Notification.FLAG_INSISTENT | Notification.FLAG_NO_CLEAR;
        notificationManager.notify(1, notification);
    }
}

我的 App.java(我在其中设置通知频道)

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;

public class App extends Application {
    public static final String CHANNEL_1_ID = "channel1";

    @Override
    public void onCreate() {
        super.onCreate();
        createNotificationChannels();
    }
    private void createNotificationChannels(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
            channel1.setDescription("This is channel 1");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我发现了以下内容:

  1. 通知内具有高优先级的全屏意图 = true(意图的第二个参数)将通知留在屏幕顶部,直到用户交互!

  2. 使用全屏意图时,不要忘记在清单中请求 fullScreenIntent 权限......