Search code examples
ionic-frameworkaudioandroid-8.0-oreolocalnotification

Ionic 4 local notification sound not working in Oreo+


I am building an Ionic 4 app and the Local Notification sound was working in Nougat, but is not working Oero. Is just playing the default sound, and ignoring the sound

I have read here: https://distriqt.github.io/ANE-PushNotifications/m.FCM-GCM%20Payload

Sounds on individual notifications were deprecated in Android O (API 26). You now have to set them on a channel.

Different notification sound not working in Oreo

that Channel is necessary

 this.localNot.schedule({
      id: id,
      // channel: id, // IS NOT Possible to set
      title: title,
      text: 'Some text',
      sound: `file://assets/audio/mySOUNDFILE.mp3`,
      icon: 'file://assets/img/logo/favicon.png', // TODO resident Img,
      color: colour
    });

But Like I had not field "channel" available in ionic I set an issue here, but was close. https://github.com/ionic-team/ionic/issues/19696

Do you have a suggestion how can I make it work?


Solution

  • I am using this "git+https://github.com/Steffaan/cordova-plugin-local-notifications.git",

    home.ts

    declare var cordova: Cordova;
    @Component({
      selector: 'app-home', ...
    

    and to send notification

        cordova.plugins.notification.local.schedule({
          id: Myid,
          title: tit,
          text: txt,
          priority: MyPriority,
          channel: 'MyChannel',
          sound: `file://assets/audio/${sound}.mp3`,
          foreground: true,
          lockscreen: true,
          vibrate: vib,
          wakeup: true,
        });
    

    Where

          priority: MyPriority,
          channel: 'MyChannel',
    

    must be different, for each different sound

    in file /node_modules/@types/cordova/index.d.ts I set:

    interface CordovaPlugins {
        notification: any;
    ...
    }