arnesson / cordova-plugin-firebase

Cordova plugin for Google Firebase
http://arnesson.github.io/cordova-plugin-firebase
MIT License
1.01k stars 1.56k forks source link

Custom sound... #986

Open fabiomig opened 5 years ago

fabiomig commented 5 years ago

I've tryed everythig (i think)

Nothing had worked so far...

I've added my sound to /res/raw and tryed with .mp3 and wav

nothing....

Anyone could help?

fabiomig commented 5 years ago

im in version 8.0.0 of android

DavidSousa commented 5 years ago

Since Android Oreo notifications require Notification Channel. If you want to use a custom sound you'll need to call the method setSound on your NotificationChannel object.

You'll need to change the definition of NotificationChannel in FirebasePluginMessagingService.java file:

if (!channelExists) {
  NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
  channel.enableLights(true);
  channel.enableVibration(true);
  channel.setShowBadge(true);

  if (lights != null) {
    Log.d(TAG, "lightArgb: " + Integer.toString(lightArgb));
    channel.setLightColor(lightArgb);
  }

  if (sound != null) {
    AudioAttributes attributes = new AudioAttributes.Builder()
      .setUsage(AudioAttributes.USAGE_NOTIFICATION)
      .build();
    Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
      + "://" + getPackageName() + "/raw/" + sound);
    channel.setSound(soundPath, attributes);
  }

  notificationManager.createNotificationChannel(channel);
}

If you're sending the notification using FCM API, you'll have to add "sound" inside "data", using only the file name, without the file extension.

Soft-IT commented 5 years ago

Hi

Im using the ringtone picker to allow the users to select their own sound. It used to work just fine right up until Android introduced these silly channels and now its a mess. The file path I am using is like this "content://media/internal/audio/media/27"

Will the channel be able to use this or am I being dumb and if so how can I make it work

fanals commented 5 years ago

As far as I know you cannot create channels with this plugin so I installed the push plugin to create channels: https://ionicframework.com/docs/v3/native/push/

this.push.createChannel({
        id: "your-custom-channel-id",
        description: "Your custom channel name",
        sound: "trumpet"
      }).then(() => console.log('Channel created'));

Sound is your audio file name without extension, I use trumpet.mp3. Once the channel as been created (in your app) you can send notification from your server specifying "your-custom-channel-id" and it will play the sound.

The problem is once the channel as been created you cannot change the sound. Even if you delete the channel and create it with a different sound it will keep the first one.

I have only 5 sounds so my solution was to create 5 channels. Based on the user choice I will update the user settings (on the server) so I know which channel to send the notification on.

fatenHd commented 5 years ago

@DavidSousa where exactly I should add the code you pasted above??

Bobisback commented 4 years ago

Is there a solution to this issue??