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)构造函数。出于一个非常好的原因。