无法在生产版本ionic 3中获得本地通知声音

时间:2019-02-22 11:33:15

标签: ionic-framework ionic2 ionic3

我对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();*/
            });

1 个答案:

答案 0 :(得分:4)

尝试使用三元运算符分配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/