0

我一直試圖在奧利奧模擬器上顯示一個簡單的通知。奇怪的是,我什麼都看不到。奧利奧模擬器上沒有顯示通知

讓我們消除明顯的答案:我試圖檢查應用程序的通知,我嘗試了Notifications和NotificationCompat路徑。我嘗試了或不帶頻道,我嘗試過或不加分組。

代碼爲基礎(是的,我用科特林但它很容易理解):

class MainActivity : Activity() { 

var id = 0 

override fun onCreate(savedInstanceState: Bundle?) { 
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_main) 
    val button:View = findViewById(R.id.button) 
    button.setOnClickListener(this::onAddNotification) 
} 

private fun onAddNotification(v: View) { 
    id++ 
    val builder = Notification.Builder(this).setSmallIcon(R.drawable.ic_notifications_none) 
      .setContentTitle("Content #$id").setContentText("Content text for $id") 

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 
    notificationManager.notify(id, builder.build()) 
} 
} 

不用說,它的代碼是在預奧利奧完美的作品。另一方面,Gmail和Google地圖會在該模擬器上顯示通知。 任何我可能已經忘記的東西?

感謝

+0

你*** ***必須使用渠道 –

+0

嗯,沒錯的。要麼是「還沒有使用API​​ 26」。 NotificationCompat尚不支持它。 Uncool,Google,非常酷! –

回答

0

谷歌說: 當你的目標是Android 8.0(API等級26),你必須實現一個或多個通知渠道,以顯示通知給你的用戶。如果您不瞄準Android 8.0(API級別26),但您的應用在運行Android 8.0(API級別26)的設備上使用,則您的應用的行爲與在運行Android 7.1(API級別25)或更低級別的設備上的行爲相同。

https://developer.android.com/guide/topics/ui/notifiers/notifications.html

0

蒂姆Castelijns上述評論...如果你正在使用API​​26,必須使用渠道

裸記住,NotificationCompat不處理得當(作爲第四2017年9月),這樣你的選擇是:

  • 使用API​​級別25或更早
  • 使用渠道。請注意,Google已棄用API 26上的Builder(context)構造函數。出於一個非常好的原因。