`NotificationCompat.Builder` / `Notification` **無 `setDuration`、`setRepeatCount` 等屬性**。系統通知聲音/震動由**用戶系統設定/應用頻道重要性**決定,開發者**無法直接控制具體秒數/次數**。
public class TimedNotification {
private NotificationManager manager;
private int notifyId;
private Handler handler = new Handler(Looper.getMainLooper());
private Runnable cancelRunnable;
public void showTimed(Context ctx, int id, Notification notif, long durationMs) {
manager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notifyId = id;
manager.notify(id, notif);
// 定時取消
cancelRunnable = () -> manager.cancel(notifyId);
handler.postDelayed(cancelRunnable, durationMs);
}
public void showRepeating(Context ctx, int id, Notification notif, long intervalMs, int repeatCount) {
// 類似,配合 postDelayed 遞迴發送/更新
}
public void cancel() {
handler.removeCallbacks(cancelRunnable);
manager.cancel(notifyId);
}
}
Notification **無內建播放時長/次數控制**。需求精確控制 → **自行封裝 `Handler`/`AlarmManager` 定時 `cancel`/`notify`**,或**前台服務 + `MediaPlayer` 完全接管音頻播放**。