I want to play custom sound notifications in Android.
I have mentioned that in notification channel, but setSound is not working.
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
val existingChannel=notificationManager.getNotificationChannel(id)
if(existingChannel!=null)
{notificationManager.deleteNotificationChannel(id)}
val channel = NotificationChannel(id, name, importance)
channel.description = description
channel.enableLights(true)
channel.setShowBadge(true)
channel.setBypassDnd(dnd)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.lightColor = R.color.cornflower_blue_dark
channel.enableVibration(true)
channel.setSound(path,
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build())
notificationManager?.createNotificationChannel(channel)
}
Related
Method to create a Notification Channel
private fun createChannel(notificationManager: NotificationManager) {
val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setLegacyStreamType(AudioManager.STREAM_RING)
.build()
val incomingPhoneCallChannel = NotificationChannel(
mNotificationManager.INCOMING_PHONE_CALL_CHANNEL_ID, "Incoming phone call",
NotificationManager.IMPORTANCE_HIGH
).apply {
setSound(uri, audioAttributes)
vibrationPattern = mNotificationManager._VIBRATION_PATTERN
enableVibration(true)
}
notificationManager.createNotificationChannel(incomingPhoneCallChannel)
}
Method to notify about the call
override fun notify(context: Context) {
val notificationManager = mNotificationManager.notificationManager
val notificationChannel = notificationManager.getNotificationChannel(mNotificationManager.INCOMING_PHONE_CALL_CHANNEL_ID)?: createChannel(notificationManager)
val contentTitle = "Incoming Call"
val contentIntent = Intent(context, PhoneCallReceiver::class.java)
val pendingIntent =
PendingIntent.getBroadcast(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
val ringtone =RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)
var builder = NotificationCompat.Builder(
context,
mNotificationManager.INCOMING_PHONE_CALL_CHANNEL_ID
)
.setContentTitle(contentTitle)
.setContentIntent(pendingIntent)
.setContentText("phone call")
.setColor(ContextCompat.getColor(context, R.color.ic_launcher_background))
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_phone_black_24dp)
.setOngoing(true)
.setSound(ringtone)
builder.setVibrate(mNotificationManager._VIBRATION_PATTERN
val notification = builder.build()
notification.flags = Notification.FLAG_INSISTENT
notificationManager.notify("Incoming Call", mId, notification)
}
The above code does give me notification with a default notification that sounds like a single "TING!!". It doesn't change the sound even if I select different Ringtones from the notification channel settings and doesn't vibrate at all.
Make sure the audio permission is allowed:
// Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Change TYPE_RINGTONE to TYPE_NOTIFICATION
I am trying to show Call notification like whatsup with audio
I had implemented full screen notification for call using setFullScreenIntent and pending intents as below but in other devices I am getting notification to foreground except in Samsung device with OS 11.
var notificationBuilder: NotificationCompat.Builder? = null
val ringUri: Uri = Settings.System.DEFAULT_RINGTONE_URI
var contentView = RemoteViews(packageName, R.layout.call_notification_layout)
contentView.setOnClickPendingIntent(R.id.imgCallYes, receiveCallPendingIntent)
contentView.setOnClickPendingIntent(R.id.imgCallNo, cancelCallPendingIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = NotificationCompat.Builder(
applicationContext,
CHANNEL_ID
)
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(contentView)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setSound(ringUri)
.setFullScreenIntent(callDialogPendingIntent, true)
val notificationChannel = NotificationChannel(
CHANNEL_ID,
"My Notifications",
NotificationManager.IMPORTANCE_HIGH
)
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
// Configure the notification channel.
val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
notificationChannel.setSound(ringUri, att)
notificationChannel.description = "body"
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.RED
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
notificationManager.createNotificationChannel(notificationChannel)
} else {
notificationBuilder =
NotificationCompat.Builder(applicationContext, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContent(contentView)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setSound(ringUri)
.setFullScreenIntent(callDialogPendingIntent, true)
}
var incomingCallNotification: Notification? = null
if (notificationBuilder != null) {
incomingCallNotification = notificationBuilder.build()
}
startForeground(NOTIFICATION_ID, incomingCallNotification)
IN Android 12 I added PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT for pending intent, now notification is coming
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val existingChannel = notificationManager.getNotificationChannel(id)
if (existingChannel != null) {
notificationManager.deleteNotificationChannel(id)
}
val channel = NotificationChannel(id, name, importance)
channel.description = description
channel.enableLights(true)
channel.setShowBadge(true)
channel.setBypassDnd(dnd)
channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
channel.lightColor = R.color.cornflower_blue_dark
channel.enableVibration(true)
channel.setSound(path,
AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT)
.build()
)
notificationManager?.createNotificationChannel(channel)
}
I want to play custom sound notifications in Android. I have mentioned that in notification channel. But setSound is not working.
When the app is in background it wont call onMessageReceived().
And the channel.setSound() is also not working.
I want to have App Provided Sound for notifications.
I'm creating a channel with custom sound. The notifications are correctly displayed but the sound played is still the default one.
Creation of the channel
val audioAttribute = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build()
val sound = Uri.Builder().scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(packageName)
.path(R.raw.notifsound.toString()).build()
alertChannel.apply {
enableVibration(true)
description = "Signal alert channel"
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
setSound(sound, audioAttribute)
}
notificationManager.createNotificationChannels(listOf(locationNotificationChannel,gpsStatusChannel,alertChannel))
I checked the URI with this
(contentResolver as ContentResolver).openInputStream(sound)
And it can read the file.
I found the problem.
Once you create a notification channel, you can't change the sound of it.
So you have two solutions:
Uninstall the app and install again
Or change the notification channel id to create a new one with the right configuration.
Hope this help.
I solved the issue by uninstalling the app and reinstalling it
private fun sendNotification(remoteMessage: RemoteMessage) {
val fullScreenIntent = Intent(this, IncomingActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(Constants.ROOM_ID, remoteMessage.data[Constants.ROOM_ID])
putExtra(Constants.REMOTE_MSG_INVITER_TOKEN, remoteMessage.data[Constants.REMOTE_MSG_INVITER_TOKEN])
}
val fullScreenPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
// Add the intent, which inflates the back stack
addNextIntentWithParentStack(fullScreenIntent)
// Get the PendingIntent containing the entire back stack
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
val audioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build()
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(resources.getString(R.string.app_name), "Incoming call...", NotificationManager.IMPORTANCE_HIGH)
notificationChannel.description = "Someone is calling you..."
notificationChannel.setSound(Settings.System.DEFAULT_RINGTONE_URI, audioAttributes)
notificationManager.createNotificationChannel(notificationChannel)
}
val notificationBuilder =
NotificationCompat.Builder(this, resources.getString(R.string.app_name))
.setSmallIcon(R.drawable.icon)
.setContentTitle("Incoming call")
.setContentText("Someone is calling you...")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setAutoCancel(true)
.setSound(Settings.System.DEFAULT_RINGTONE_URI)
.setChannelId(resources.getString(R.string.app_name))
.setContentIntent(fullScreenPendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.icon))
val incomingCallNotification = notificationBuilder.build()
notificationManager.notify(0, incomingCallNotification)
}
I am using Firebase push notification in kotlin and below are the code snippet for showing push notification
mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannel(mNotifyManager)
val mBuilder = NotificationCompat.Builder(this, "bks-channel")
.setLargeIcon(largeIcon)
.setContentTitle("Bks")
.setSmallIcon(R.drawable.app_icon)
.setContentText(message)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
mNotifyManager.notify(getRandomNumber(), mBuilder.build())
And the createChannel() function is :
#TargetApi(26)
private fun createChannel(notificationManager: NotificationManager)
{
val name = "bks"
val description = "bks"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(name, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager.createNotificationChannel(mChannel)
}
Below are the server logs:
array(1) {
[9]=>
array(2) {
["name"]=>
string(14) "ABL Staff USER"
["fcm_response"]=>
array(2) {
["fields"]=>
array(3) {
["data"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
["registration_ids"]=>
array(11) {
[0]=>
string(152) "device_token_1"
}
["notification"]=>
array(4) {
["click_action"]=>
int(2)
["title"]=>
string(19) "Attendance Reminder"
["body"]=>
string(49) "Hi ABL Staff USER, Please mark your attendance ! "
["sound"]=>
string(7) "default"
}
}
Same are working for below android o in foreground and background but not in android o or above
FCM has sent two type of notification
Notification Message
Data Message
Send the data message to show a notification when the app in the background
For more details check the below link
Firebase data message
Are you creating channel by name "bks-channel" ? Looks like you are not creating bks-channel instead you are creating channel by name bks.
Update your create channel method.
#TargetApi(26)
private fun createChannel(notificationManager: NotificationManager) {
val name = "bks-channel"
val description = "bks"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(name, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.BLUE
notificationManager.createNotificationChannel(mChannel)
}