Ionic4 OneSignal 打开推送通知重定向页面

Ionic4 OneSignal open push notification redirect page

希望实现打开OneSignal推送通知时重定向到App中的某个页面。以下是我重定向到名为 positions 的页面的代码,但它似乎没有用。打开推送通知后,它仍会在 InAppBrowser 中打开 url。知道出了什么问题吗?提前致谢。

      if (this.appConfig.Onesignal_Enable == true) {
        this.oneSignal.startInit(this.appConfig.OneSignal_AppID, this.appConfig.GCM_SenderID);
        this.oneSignal.handleNotificationReceived().subscribe(() => {
          // do something when notification is received
        });
        this.oneSignal.handleNotificationOpened().subscribe((data) => {
          // do something when a notification is opened
          // the following two lines pass data I send with the push notification so the app knows what to open
          let pushaction = data.notification.payload.additionalData.action;
          let pushactionvalue = data.notification.payload.additionalData.actionvalue;

          // this fires up the tab-switching
          this.runNotificationAction(pushaction, pushactionvalue);
        });
        this.oneSignal.endInit();
      }

  runNotificationAction(pushaction, pushactionvalue){

    // this is the data passed the the other page
    let data = {"action": pushaction, "value:": pushactionvalue};

 
    this.navCtrl.navigateForward('positions');

}

你好,我也在使用 v4 ionic,我在我的项目中通过这个实现实现了这一点:

let self = this;
var notificationOpenedCallback = async function(jsonData) {
  //I use info data previous saved, but you can use jsonData
  switch (self.user.role) {
    case "customer":
      self.router.navigate(["history-customer"]);
      break;
    case "provider":
      self.router.navigate(["history-provider"]);
  }
};

window["plugins"].OneSignal.startInit(
  "0*************7",
  "1*********2"
)
  .handleNotificationOpened(notificationOpenedCallback)
  .endInit();

window["plugins"].OneSignal.setSubscription(true);

我用

let self = this

因为 startInit 收到回调所以有必要这样做,在你的情况下我不知道内部订阅是否也需要使用 "self" 并且我使用 Router 在页面之间导航。

import { Router } from "@angular/router";

还有我的 app-routing.module.ts

{ path: 'history-provider', loadChildren: './pages/history-provider/history-provider.module#HistoryProviderPageModule' },
{ path: 'history-customer', loadChildren: './pages/history-customer/history-customer.module#HistoryCustomerPageModule' }