自定義 BroadcastReceiver 發送通知,彈出提示但**無鈴聲、無震動**。
未設置 Notification.defaults,系統不知道使用默認鈴聲/震動/燈光模式。
Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("ALERT!!!!")
.setContentText("Time Over")
.setContentIntent(contentIntent)
.setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE) // 關鍵
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
| 常量 | 效果 |
|---|---|
| DEFAULT_SOUND | 系統默認通知鈴聲 |
| DEFAULT_VIBRATE | 系統默認震動模式 |
| DEFAULT_LIGHTS | 系統默認 LED 燈光(需硬件支持) |
| DEFAULT_ALL | 以上三者合一 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Alarms", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Alarm notifications");
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}