简体   繁体   中英

Android Notification duration

Is there a way to make android notification stay on screen for over 4 seconds.

For example:

Phone App has notification for incoming call, when this notification shows up (Incoming call) it does not leave the screen until the user is interacting with the notification (Accept call / deny call / go to phone app). However I have not seen a way to make notification last on screen (foreground) for over 4 seconds. Even when i am setting the FLAG_INSISTENT and FLAG_NO_CLEAR the notification last visible on screen for 4 - 5 seconds...

FLAG_INSISTENT - keeps beeping and vibrate on high priority notification but still the notification is going off the screen after 4 seconds and the user need to drag bar down in order to "see" the notification.

I am looking for a way to put a notification the same way that the phone app is doing it meaning keep the notification ON SCREEN until the user interacts with it.

Here is an example of main activity:

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

My App.java (where i set Notification Channel)

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

I FOUND OUT THE FOLLOWING:

  1. Fullscreen intent inside the notification with high priority = true (second argument for the intent) leaves the notification on top of the screen till user interaction !

  2. When using FullScreen intents don't forget to ask for fullScreenIntent permissions within the manifest....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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