Maneki Neko
Maneki Neko

Reputation: 1247

Notifications not showing on Oreo emulator

I have been trying to show a simple notification on an Oreo emulator. Strangely, I see nothing.

Let's eliminate obvious answers: I tried to check the notifications for the app, I tried the Notifications and the NotificationCompat path. I tried with or without channels, I tried with or without groups.

The code is elementary (yes, I use Kotlin but it's easy to understand):

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())
}
}

Needless to say that it's code that works perfectly on pre-Oreo. On the other hand, Gmail and Maps show notifications on that emulator. Anything I might have forgotten?

Thanks

Upvotes: 2

Views: 6947

Answers (2)

Dan Alboteanu
Dan Alboteanu

Reputation: 10232

I spent an hour to realize the emulator was on "Do Not Disturb" and was NOT showing notifications.

Google says: When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users. If you don't target Android 8.0 (API level 26) but your app is used on devices running Android 8.0 (API level 26), your app behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

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

Upvotes: 2

Maneki Neko
Maneki Neko

Reputation: 1247

As Tim Castelijns commented above... If you are using API26, YOU MUST USE CHANNELS

Bare in mind that NotificationCompat does not handle it properly (as for the 4th of September 2017) so your options are:

  • Use API level 25 or earlier
  • Use channels. Note that Google deprecated the Builder(context) constructor on API 26. For a very good reason.

Upvotes: 1

Related Questions