我有Ionic 3局部通知声音的问题。当我进行调试时,声音即将到来。
它不是在生产构建中进行的,我也尝试将声音值设置为" res://platform_default"(无法获得声音)。请帮我。预先感谢。
我的代码下方。
import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, Platform, AlertController} from 'ionic-angular';
import {LocalNotifications} from '@ionic-native/local-notifications';
@IonicPage()
@Component({
selector: 'page-notifications',
templateUrl: 'notifications.html',
})
export class NotificationsPage {
data = {title: '', description: '', date: '', time: ''};
constructor(public navCtrl: NavController,
public localNotifications: LocalNotifications,
public platform: Platform,
public alertCtrl: AlertController) {
}
submit() {
console.log(this.data);
var date = new Date(this.data.date + " " + this.data.time);
console.log(date);
this.localNotifications.requestPermission().then((permission) => {
this.localNotifications.schedule({
id: 0,
text: 'Delayed ILocalNotification',
trigger: {at: date},
foreground:true,
vibrate: true,
led: {color: '#FF00FF', on: 500, off: 500},
data: {mydata: 'My hidden message this is'},
// sound: 'res://platform_default',
sound: this.setSound(),
});
let alert = this.alertCtrl.create({
title: 'Congratulation!',
subTitle: 'Notification setup successfully at ' + date,
buttons: ['OK']
});
alert.present();
this.data = {title: '', description: '', date: '', time: ''};
});
}
setSound() {
if (this.platform.is('android')) {
return 'file://assets/sounds/Rooster.mp3'
} else {
return 'file://assets/sounds/Rooster.caf'
}
}
}
在仪表板页面
this.localNotifications.on('click').subscribe(
(datas: any) => {
alert('in_is');
alert(JSON.stringify(datas));
/*let alert = alertCtrl.create({
title: notification.title,
subTitle: json.mydata
});
alert.present();*/
});
尝试使用 ternary 操作员分配MP3声音,而无需调用该函数分配
this.localNotifications.schedule({
id: 0,
text: 'Delayed ILocalNotification',
trigger: {at: date},
foreground:true,
vibrate: true,
led: {color: '#FF00FF', on: 500, off: 500},
data: {mydata: 'My hidden message this is'},
sound: this.platform.is('android') ? 'file://assets/sounds/Rooster.mp3' : 'file://assets/sounds/Rooster.caf',
});
参考离子文档https://ionicframework.com/docs/native/local-notifications/