【问题标题】:local notification buttons doesn't made any action本地通知按钮不执行任何操作
【发布时间】:2020-05-13 17:50:46
【问题描述】:

对于 ionic 4,我正在使用插件 localNotifications:

https://github.com/katzer/cordova-plugin-local-notifications

我想实现“是”、“否”选项并仅在触摸“是”按钮时才执行操作,如果单击“否”按钮,只是想关闭通知,我在我的真正的项目,并且操作按钮“有效”,但只有当我在应用程序中或应用程序暂停但可见(您可以在其中终止应用程序的菜单)时,这些按钮才能正常工作。

这是详细信息:如果我触摸主页按钮并尝试选择操作是或否,如果我选择“是”,则应用程序将正确启动(是按钮具有启动操作),但如果我尝试单击在“否”选项中,并且由于某种原因该应用程序不可见,该应用程序已关闭并且不会出现在您可以杀死应用程序的菜单中,(但是当我再次打开该应用程序时,该应用程序将在我的位置恢复是),有点奇怪……

很难调试,因为当应用程序不活动时,控制台不会记录任何内容(检查器控制台仅在应用程序处于活动状态时工作)。

从字面上看,我已经有这个问题好几个小时了,所以我尝试创建一个新项目,仅用于使用这个插件进行测试,并且在新项目中,如果我之前点击主页按钮,按钮不会关闭应用程序,但出现了一个新问题:操作不会被触发...

我已经尝试使用后台模式并在新项目中激活它,但这没有任何区别,我尝试的另一件事是在 deviceready 函数中打开(事件):

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.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { HomePage } from './home.page';
import {NgPipesModule} from 'ngx-pipes';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    NgPipesModule,
    RouterModule.forChild([
      {
        path: '',
        component: HomePage
      }
    ])
  ],
  declarations: [HomePage],
  providers: [LocalNotifications]
})
export class HomePageModule {}

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 插件版本:最新版本或倒数第二个(结果相同)。

【问题讨论】:

标签: javascript angular typescript ionic-framework notifications


【解决方案1】:

您的活动名称不正确。使用

this.plt.ready().then(
            () => {
                this.localNotification.on('click').subscribe(
                    res => {}
                );
                this.localNotification.on('trigger').subscribe(
                    res => {}
                );
            }
        );

【讨论】:

  • 否 - OP 对于命名事件是正确的,这应该可以工作
猜你喜欢
  • 2020-07-11
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 2020-03-23
  • 2020-06-09
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多