先附上官网本地通知地址:https://ionicframework.com/docs/native/local-notifications/
再ionic3上面的本地推送有些内容不能使用,不如说官网提供的声音,查阅了一些资料也没找到原因
第一步:安装本地通知插件
ionic cordova plugin add cordova-plugin-local-notification
npm install --save @ionic-native/local-notifications
第二步:app.module.ts
import { LocalNotifications } from '@ionic-native/local-notifications';
providers: [
LocalNotifications
]
第三步:在需要用到本地通知的ts注入插件
import { LocalNotifications } from '@ionic-native/local-notifications';
constructor(private localNotifications: LocalNotifications) { }
然后写一个方法
i=1;
notice(){
this.localNotifications.schedule({
id: this.i,//将来清除,取消,更新或检索本地通知所需的唯一标识符默认值:0
title:"应用通知",
text: '新的订单'+this.i,
trigger: {at: new Date(new Date().getTime())},//何时触发通知
//声音设置了无效
sound: null,//显示警报时包含播放声音的文件的Uri默认值:res:// platform_default
launch:true,
//在我手机上也是无效的
lockscreen:true//仅限ANDROID如果设置为true,则通知将在所有锁定屏幕上完整显示。如果设置为false,则不会在安全锁屏上显示。
});
this.i++;
}
测试一下需要的功能是否有效,经测试有很多配置是无效的,不知道是不是手机问题
我需要的震动和声音提示是无效的,所有我用了单独的震动插件和声音插件
官网震动插件地址:https://ionicframework.com/docs/native/vibration/
1.安装震动插件
ionic cordova plugin add cordova-plugin-vibration
npm install --save @ionic-native/vibration
2.app.module.ts
import { Vibration } from '@ionic-native/vibration';
providers: [
Vibration
]
3.ts文件注入插件
import { Vibration } from '@ionic-native/vibration';
constructor(private vibration: Vibration) { }
然后在本地通知的方法里面加入震动的代码
this.vibration.vibrate(1000);
官网原生音频插件地址:https://ionicframework.com/docs/native/native-audio/
1.安装
ionic cordova plugin add cordova-plugin-nativeaudio
npm install --save @ionic-native/native-audio
2.app.module.ts
import { NativeAudio } from '@ionic-native/native-audio';
providers: [
NativeAudio
]
3.ts文件注入插件
import { NativeAudio } from '@ionic-native/native-audio';
constructor(private nativeAudio: NativeAudio) { }
找一个声音文件,然后在ts文件里面先加载声音
private onSuccess: any;
private onError: any;
constructor(private nativeAudio: NativeAudio) {
//uniqueId1为音频文件的唯一ID
//assetPath音频资产的相对路径或绝对URL(包括http://)
//官网还有更多的配置,这里只需要两个参数就行了,后面的回调记得带上
this.nativeAudio.preloadSimple('uniqueId1', 'assets/dd.mp3').then(this.onSuccess, this.onError);
}
然后再本地通知里面加上声音代码
this.nativeAudio.play('uniqueId1').then(this.onSuccess,this.onError);
完整的本地通知加震动加声音ts代码
import { Component } from '@angular/core';
import { NavController} from 'ionic-angular';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { Vibration } from '@ionic-native/vibration';
import { NativeAudio } from '@ionic-native/native-audio';
@Component({
selector: 'page-my',
templateUrl: 'my.html'
})
export class MyPage {
private onSuccess: any;
private onError: any;
constructor(public navCtrl: NavController,
public localNotifications:LocalNotifications,
public vibration:Vibration,
public nativeAudio:NativeAudio) {
//uniqueId1为音频文件的唯一ID
//assetPath音频资产的相对路径或绝对URL(包括http://)
//官网还有更多的配置,这里只需要两个参数就行了,后面的回调记得带上
this.nativeAudio.preloadSimple('uniqueId1', 'assets/dd.mp3').then(this.onSuccess, this.onError);
}
i=1;
notice() {
this.vibration.vibrate(1000);
this.nativeAudio.play('uniqueId1').then(this.onSuccess, this.onError);
this.localNotifications.schedule({
id: this.i,//将来清除,取消,更新或检索本地通知所需的唯一标识符默认值:0
title: "应用通知",
text: '新的订单' + this.i,
trigger: {at: new Date(new Date().getTime())},//何时触发通知
//声音设置了无效
sound: null,//显示警报时包含播放声音的文件的Uri默认值:res:// platform_default
launch: true,
//在我手机上也是无效的
lockscreen: true//仅限ANDROID如果设置为true,则通知将在所有锁定屏幕上完整显示。如果设置为false,则不会在安全锁屏上显示。
});
this.i++;
//定时器
// setTimeout(() => {
// this.mass();
// }, 10000);
}
}