Javascript 本地通知按钮没有';我没有采取任何行动
在ionic 4中,我使用了以下的插件localNotifications: 我想实现“是”、“否”选项,并仅在按下“是”按钮时才执行操作,如果单击“否”按钮,只想关闭通知,我在真实项目中执行了此实现,操作按钮“工作”,但只有当我在应用程序中或应用程序挂起但可见时,这些按钮才能正常工作(可以关闭应用程序的菜单) 以下是详细信息:如果我按home按钮并尝试选择actions是或否,如果我选择yes(是),应用程序将正确启动(yes(是)按钮具有启动操作),但如果我尝试单击no(否)选项,并且由于某种原因应用程序不可见,则应用程序已关闭,并且不会出现在可以关闭应用程序的菜单中,(但当我再次打开应用程序时,应用程序将恢复到我所在的位置),有点奇怪 这很难调试,因为当应用程序处于非活动状态时,控制台不会记录任何内容(inspector控制台仅在应用程序处于活动状态时工作) 从字面上说,我已经有这个问题好几个小时了,所以我试着用这个插件做一个新的测试项目,在新的项目中,如果我以前做了一个“点击主页”按钮,按钮不会关闭应用程序,但是遇到了一个新问题:操作不会被触发 我已经尝试使用后台模式并在新项目中激活它,但这没有任何区别,我尝试的另一件事是在deviceready功能中启用(事件):Javascript 本地通知按钮没有';我没有采取任何行动,javascript,angular,typescript,ionic-framework,notifications,Javascript,Angular,Typescript,Ionic Framework,Notifications,在ionic 4中,我使用了以下的插件localNotifications: 我想实现“是”、“否”选项,并仅在按下“是”按钮时才执行操作,如果单击“否”按钮,只想关闭通知,我在真实项目中执行了此实现,操作按钮“工作”,但只有当我在应用程序中或应用程序挂起但可见时,这些按钮才能正常工作(可以关闭应用程序的菜单) 以下是详细信息:如果我按home按钮并尝试选择actions是或否,如果我选择yes(是),应用程序将正确启动(yes(是)按钮具有启动操作),但如果我尝试单击no(否)选项,并且由于
document.addEventListener('deviceready', ()=>{
this.isAcceptedObservable = this.localNotifications.on('yes').subscribe(res =>{
console.log('confirmed!');
});
this.isNotAcceptedObservable = this.localNotifications.on('no').subscribe(res =>{
console.log('denied!');
});
});
但是什么都没发生
我还试图退出“日程安排选项”中的“前景”选项,但这没有任何区别
以下是我的测试代码app.module:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { NgPipesModule } from 'ngx-pipes';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
import { BackgroundMode } from '@ionic-native/background-mode/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, NgPipesModule],
providers: [
StatusBar,
SplashScreen,
LocalNotifications,
BackgroundMode,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
home.page.html:
<ion-header>
<ion-toolbar>
<ion-title>
Ionic Blank
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-button expand = "full" (click) = "showNotification()">
<ion-label>
Show notification...
</ion-label>
</ion-button>
</ion-content>
home.page.ts:
import { Component } from '@angular/core';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
import { Observable } from 'rxjs';
import { Subscription } from 'rxjs';
import { BackgroundMode } from '@ionic-native/background-mode/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
public isAcceptedObservable: Subscription;
public isNotAcceptedObservable: Subscription;
constructor(private localNotifications: LocalNotifications,private backgroundMode: BackgroundMode){
this.backgroundMode.enable();
document.addEventListener('deviceready', ()=>{
this.isAcceptedObservable = this.localNotifications.on('yes').subscribe(res =>{
console.log('confirmed!');
});
this.isNotAcceptedObservable = this.localNotifications.on('no').subscribe(res =>{
console.log('denied!');
});
});
}
ionViewDidEnter(){
this.localNotifications.schedule({
id: 1,
title: 'Notification received!',
text: 'Haz recibido una invitación para ir a un lugar!',
actions: [
{ id: 'yes', title: 'Yes' , launch: true},
{ id: 'no', title: 'No', launch: false}
],
autoClear: true,
foreground: true
});
}
showNotification(){
this.localNotifications.schedule({
id: 1,
title: 'Notification received!',
text: 'Haz recibido una invitación para ir a un lugar!',
actions: [
{ id: 'yes', title: 'Yes' , launch: true},
{ id: 'no', title: 'No', launch: false}
],
autoClear: true,
foreground: true
});
}
ionViewWillLeave(){
this.isAcceptedObservable.unsubscribe();
this.isNotAcceptedObservable.unsubscribe();
}
}
当我点击两个按钮时,什么也没发生……还有一些重要的事情需要考虑:
- Android平台版本:android@6.4.0
- BackgroundMode插件版本(重要的是,我使用的是与最实际版本不同的版本,BackgroundMode实际插件与此版本的android不兼容),版本:0.7.2
- LocalNotification插件版本:最新版本或倒数第二版本(结果相同)
this.plt.ready().then(
() => {
this.localNotification.on('click').subscribe(
res => {}
);
this.localNotification.on('trigger').subscribe(
res => {}
);
}
);
另外,我在两个项目中都使用了cordova android support gradle版本。您解决了问题吗?没有-OP对于命名事件是正确的,这应该可以工作
this.plt.ready().then(
() => {
this.localNotification.on('click').subscribe(
res => {}
);
this.localNotification.on('trigger').subscribe(
res => {}
);
}
);