Reputation: 81
Whenever I send a notification through fcm.
It says it will use the default from manifest or the android app, I also have mentioned my channel in manifest as default. It just says that the channel is not created by the app.
Log says
W/FirebaseMessaging: Notification Channel requested (message_notification) has not been created by the app. Manifest configuration, or default, value will be used.
This is my onMessageReceived function.
override fun onMessageReceived(p0: RemoteMessage) {
super.onMessageReceived(p0)
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+":/ /"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notofiction Nul", sound.toString())
if(p0.notification!=null){
val title = p0.data["title"]
val message = p0.data["body"]
Log.d("notottg",title+message)
buildNotificationChannel()
if(buildNotificationChannel()){
generateNotification(title!!,message!!)
}
}
else{
val title = p0.data["title"]
val message = p0.data["body"]
Log.d("notottg",title+message)
buildNotificationChannel()
if(buildNotificationChannel()){
generateNotification(title!!,message!!)
}
}
}
This is my createNotificationChannel function
private fun buildNotificationChannel():Boolean {
/*var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notisound",sound.toString())*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val CHANNEL_ID = "merchant_notification"
val CHANNEL_NAME = "Merchant Notification"
var notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
vibrationPattern=longArrayOf(400, 400, 400, 400, 500, 400, 400, 400, 400)
enableVibration(true)
}
notificationManager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
//notificationChannel.setSound(sound,audioAttributes)
}
return true
}
And this is my generateNotification function
fun generateNotification(title: String, message: String) {
val intent = Intent(this@CustomMessagingService, MainActActivity::class.java)
var pendingIntent = PendingIntent.getActivity(this@CustomMessagingService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
var builder: NotificationCompat.Builder =
NotificationCompat.Builder(this, "merchant_notification")
var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
Log.d("notisound",sound.toString())
builder.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setSound(sound)
notification = builder.build()
Log.d("notot", notification.toString() + " " + sound.toString())
notificationManager.notify(1, notification)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
var nb: Notification.Builder = Notification.Builder(this)
nb.setSmallIcon(R.mipmap.ic_launcher_round)
.setAutoCancel(true)
.setSound(sound, audioAttributes)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.build()
notification = nb.notification
notification.flags = Notification.FLAG_AUTO_CANCEL
notificationManager.notify(0, notification)
}
}
My Manifest Code
<service android:name=".classes.CustomMessagingService"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"
/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="merchant_notification" />
Upvotes: 0
Views: 492
Reputation: 1497
Sample code for generateNotification method.
private fun generateNotification(title: String, messageBody: String) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val channelId = "125"
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
Upvotes: 1