程式碼邏輯正確,NotificationManager.notify() 呼叫無誤,但狀態列完全不出現通知。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Description");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
// 建構通知時指定相同 channelId
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id");
缺少 Channel 會導致通知靜默丟棄,無任何錯誤訊息。
startActivity(new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()));setSmallIcon() 必須設定,**且圖示必須為純白透明 PNG**(Android 規範),否則可能顯示為白方塊或完全不顯示。mipmap/ic_launcher 等彩色圖示作為 SmallIcon。notify(id, notification) 若同一 ID 連續呼叫,後者會覆蓋前者。測試時改用遞增 ID 或 System.currentTimeMillis()。startForeground() 並提供通知,否則系統可能限制。NotificationManager 可能延遲。測試時關閉電池優化或使用 setPriority(NotificationCompat.PRIORITY_HIGH)。startActivity(intent) 在 notify() **之前**呼叫會啟動 Activity 而非顯示通知。移除該行或改用 PendingIntent 包裝。PendingIntent.getActivity(..., PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) (API 23+ 需 FLAG_IMMUTABLE)。NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "test_channel")
.setSmallIcon(R.drawable.ic_notification) // 純白圖示
.setContentTitle("測試標題")
.setContentText("測試內容")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManagerCompat.from(this).notify(1, builder.build());
90% 的「通知不顯示」源於 **缺少 NotificationChannel**、**圖示不合規** 或 **使用者關閉權限**。按上述清單逐項驗證即可定位。