Local Notifications - sound

Hi all ! Could anyone suggest please, how to add sound to the local Notifications ? I use this example but sound сan not hear.

LocalNotifications.schedule({
  id: 1,
  title:'Mobile',
  text: 'Updating Cmplete!',
  sound: this.platform.is('Android') ? 'file://sound.mp3': 'file://beep.caf',
  badge: 1,
});

Hmm, I have it working using this setup.

export class HomePage {
  constructor(
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    public platform: Platform
  ) {
    this.platform.ready()
      .then(() => {
        console.log(this.platform.is('android'))
        LocalNotifications.schedule({
          id: 1,
          text: 'Single LocalNotification',
          sound: this.setSound(),
          data: { secret: 'hellloo' }
        });
      })
  }
  setSound() {
    if (this.platform.is('android')) {
      return 'file://assets/sounds/shame.mp3'
    } else {
      return 'file://assets/sounds/bell.mp3'
    }
  }
}

and it places the correct sound for each platform.

3 Likes

mhartington, thanks for answer. Can I download sound to Ionic for example src\assets\sounds ?
Update. All working, thanks.

mhartington, good day! Take this opportunity to ask … can we in local notifications plugin add some icon in status bar to show that something downloading now ? Like alarm clock icon or wi-fi ? Something like that ?

In my code it is not working


ionViewDidLoad(){
this.Platform.ready().then(() => {
console.log("-----------------in view did load-------------------");
this.localNotifications.hasPermission().then(function(granted) {
if (!granted) {
this.localNotifications.registerPermission();
}
});
});
}
SoundNotify(){
  this.localNotifications.schedule({
  id: 1,
  text: 'Single ILocalNotification',
  at: new Date(new Date().getTime() + 1000),
  sound: this.setSound(),
  data: { secret: "key" }
});
this.localNotifications.isPresent(1);
let myModal = this.modalCtrl.create(NotificationModalPage);
myModal.present();
}
setSound(){
 if (this.Platform.is('android')) {
      return 'file://assets/sounds/notify.wav'
    } else {
      return 'file://assets/sounds/shar.mp3'
    }
  }

it shows alarm in silent mode vibrate device but in general profile no sound no vibration.

2 Likes

Change file path
"/android_asset/www/assets/resources/sounds/alert.mp3"
to
"file://assets/resources/sounds/alert.mp3"

This work me

1 Like

Hi @mhartington,

This is my code.

this.localNotifications.schedule({
     id: 1,
     text: 'Single ILocalNotification',
     sound: 'file://assets/media/hangouts_incoming_call.ogg'
});

I used this code not inside the constructor. Used inside an another function. And didn’t check the platform. But sound is not working. Only notification displayed. But this sound worked well using ionic nativeaudio. Therefore audio has not the error.

What could be the reason.

Hi, can I know the path where you store the sound file?

Hi, I’m having the same issue. Have you solved it?