CodeX
CodeX

Reputation: 58

No Heads-Up / Sound when firing Notification on API 25 or lower

When using the NotificationCompat on Android, the notifications only work as expected on API level 26 or higher.

Code:

val chan2 = NotificationChannel(SECONDARY_CHANNEL,
    getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH)
manager.createNotificationChannel(chan2)

fun getNotification2(title: String, body: String): NotificationCompat.Builder {
    return NotificationCompat.Builder(applicationContext, SECONDARY_CHANNEL)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(smallIcon)
            .setAutoCancel(true)
}

fun notifySecondaryChannel(id: Int, notification: NotificationCompat.Builder) {
    notification.priority = NotificationCompat.PRIORITY_MAX
    manager.notify(id, notification.build())
}

Dependencies:

Full code on github (forked and updated google sample).

Upvotes: 1

Views: 480

Answers (1)

Elletlar
Elletlar

Reputation: 3234

A sound can be played by using:

builder.setDefaults(Notification.DEFAULT_SOUND) or 
builder.setDefaults(Notification.DEFAULT_ALL)

or one of the overrides of setSound. For exmaple:

public Notification.Builder setSound (Uri sound, 
                int streamType)

Upvotes: 2

Related Questions