quickstart-android icon indicating copy to clipboard operation
quickstart-android copied to clipboard

Is there a way to customize the sound for notification message on Android O and above?

Open ZoroYouth opened this issue 6 years ago • 2 comments

The push message is like

"notification" : {
      "body" : "This is a Firebase Cloud Messaging",
      "title" : "FCM Message",
      "sound":"custom"
}

And the custom.mp3 is in /res/raw It works fine when my project target api 23, but it does not work when target api 26 on Android O. Both target api 23 and target api 26, the notifications are shown by the system if the app is in background. I don't know what has changed on Android O. I don't want to change the notification message to data message on the server side because it affects a lot. Is there an easy way to solve the issue? Thanks.

ZoroYouth avatar Dec 03 '18 02:12 ZoroYouth

Step 1: Create Notification Channel In APP. Step 2: Pass this channel id to notification

"notification" :
        {
      "body" : "This is a Firebase Cloud Messaging",
      "title" : "FCM Message",
      "sound":"custom",
     "android_channel_id":"channel id"
           }

chandreshandroid avatar Aug 27 '19 17:08 chandreshandroid

My custom ringtone is invalid. I wonder if it is because the android version is too high. Server code: "notification" : { "body" : "This is a Firebase Cloud Messaging", "title" : "FCM Message", "sound":"play", "channel_id":"channel id" } android code(res/raw/play.mp3):

val data = remoteMessage.data.toString() val channelId = remoteMessage.notification?.channelId?: getString(R.string.default_notification_channel_id) val title = remoteMessage.notification?.title val body = remoteMessage.notification?.body LogUtils.e(TAG, "data:$data") val intent = Intent(this, NotificationActivity::class.java) if (!TextUtils.isEmpty(data)) { var pushEntity = Gson().fromJson(Gson().toJson(remoteMessage.data), PushEntity()::class.java) intent.putExtra(AppConfig.NOTICAFTION.FROM_TYPE, "0") intent.putExtra(AppConfig.NOTICAFTION.EXTRAS, pushEntity) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK val pendingIntent = PendingIntent.getActivity(this, System.currentTimeMillis().toInt(), intent, PendingIntent.FLAG_ONE_SHOT) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationBuilder = NotificationCompat.Builder(this, channelId) val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) val channelName = getString(R.string.app_name) notificationBuilder.setSmallIcon(R.drawable.push) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .build() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH) channel.setSound(defaultSoundUri, Notification.AUDIO_ATTRIBUTES_DEFAULT) notificationManager.createNotificationChannel(channel) } notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build()) }

expect: The MP3 player,But it didn't work

wangyangke avatar Jan 19 '20 07:01 wangyangke