firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.82k stars 7.32k forks source link

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

Open ZoroYouth opened 5 years ago

ZoroYouth commented 5 years ago

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.

chandreshandroid commented 5 years ago

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"
           }
wangyangke commented 4 years ago

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