指定在Android Studio中发送通知的时间延迟?

问题描述 投票:0回答:1

我在Android Studio中设置了通知通道,用于发送通知。

到目前为止,单击按钮时我可以发送通知。

但是,我希望在发送通知时增加延迟。例如,在20秒后发送通知。

我知道AlarmManager中有一个用于System.getTimeInMillis的功能,该功能与此相关,但不确定从何处去。

这是我的代码:

public class MyNotificationPublisher extends Application {

    public static final String CHANNEL_1_ID = "channel1";
    public static final String CHANNEL_2_ID = "channel2";

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

            NotificationChannel channel2 = new NotificationChannel(
                    CHANNEL_2_ID,
                    "Channel 2",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel2.setDescription("This is Channel 2");

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);
            manager.createNotificationChannel(channel2);
        }
    }
}
public class EmailActivity extends AppCompatActivity {
    private Button btnSend;
    private NotificationManagerCompat notificationManager;
    private long tenSeconds = 10000L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);

        notificationManager = NotificationManagerCompat.from(this);

        btnSend = findViewById(R.id.button_send);
    }

    public void sendOnChannel1(View v) {


        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Hi")
                .setContentText("Test")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();

        notificationManager.notify(1, notification);
    }

}
java android android-notifications
1个回答
0
投票

您可以使用处理程序来延迟通知的发送

这样更新代码

 public void sendOnChannel1(View v) {


        Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Hi")
                .setContentText("Test")
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
        notificationManager.notify(1, notification);
        }
    }, 20000);

    }
© www.soinside.com 2019 - 2024. All rights reserved.