首页 > 解决方案 > Ionic 在一个信号推送通知上没有警报

问题描述

我正在使用 ionic 和 OneSignal 进行推送通知。到目前为止,我可以收到通知,但如果我点击它,什么也不会发生。如果应用程序显示“通知已打开”之类的警报,那将是很好的测试。我的目标是,应用程序路由到特定页面或模式,但这现在并不重要。


import { Component } from '@angular/core';

import { Platform,AlertController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})
export class AppComponent {

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,private alertCtrl: AlertController) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
      
      //Remove this method to stop OneSignal Debugging 
      window["plugins"].OneSignal.setLogLevel({logLevel: 6, visualLevel: 0});
            
      // Set your iOS Settings
      var iosSettings = {};
      iosSettings["kOSSettingsKeyAutoPrompt"] = false;
      iosSettings["kOSSettingsKeyInAppLaunchURL"] = false;
      
      window["plugins"].OneSignal
        .startInit("my key is in here")
        .handleNotificationOpened(function(openResult) 
        {
          this.showAlert('Notification opened', 'You already read this before',JSON.stringify(openResult));
          console.log('Notification opened: ' + JSON.stringify(openResult));   
        })
        .iOSSettings(iosSettings)
        .inFocusDisplaying(window["plugins"].OneSignal.OSInFocusDisplayOption.Notification)
        .endInit();
      
      // The promptForPushNotificationsWithUserResponse function will show the iOS push notification prompt. We recommend removing the following code and instead using an In-App Message to prompt for notification permission (See step 6)
      window["plugins"].OneSignal.promptForPushNotificationsWithUserResponse(function(accepted) {
        console.log("User accepted notifications: " + accepted);
      });
    });
  }

  async showAlert(title, msg, task) {
    const alert = await this.alertCtrl.create({
      header: title,
      subHeader: msg,
      buttons: [
        {
          text: `Action: ${task}`,
          handler: () => {
            // E.g: Navigate to a specific screen
          }
        }
      ]
    })
    alert.present();
  }
}


有人可以帮助我吗?为什么我看不到任何警报?谢谢

标签: angularionic-frameworkonesignal

解决方案


推荐阅读