Nativescript 推送通知不触发导航

Nativescript Push Notification not triggering navigation

我正在使用 Firebase 插件创建推送通知,效果很好。我的想法是我收到通知 - 当我点击通知时,应用程序会打开一个对话框,当你点击其中一个选项时,你会导航到一个页面。但是,当应用程序最小化或关闭并且我收到通知时,导航无法成功运行。我在 Angular 8

中使用 Nativescript 6.3.1
  onMessageReceivedCallback: (message: firebase.Message) => {
    let meterNumber = message.data.meter;
    let peid = message.data.peid;
    let entid = message.data.entid;
    this.mainService.listMeterNumbers(peid, true).subscribe((res) => {
      let options = {
        title: "Don't remind me about '" + message.data.meter + "' within -",
        message: "",
        cancelButtonText: "Cancel",
        actions: ["1 Day", "1 Week", "1 Month", "1 Year"]
      }

      action(options).then((snooze)=> {
          this.mainService.setMeterNotificationReminder(entid, snooze).subscribe(() => {
            if(snooze !== 'Cancel') {
              this.loading.navigateFromNotification = true;
              this.loading.navigatedMeterNumber = meterNumber;
              this.loading.navigatedMeterNumberParentEntityId = peid;
              
              setTimeout(()=>{ // i've tried several different things here
                this.ngZone.run(() => { // and here
                  this.loading.showNotificationList = false; // to do this - this was at first a .navigate(['newpage'], id)
              });
            }, 1000)
          }
        });
      });
    });

如有任何帮助,我们将不胜感激

您的应用程序可能会在两种主要状态下收到推送通知:

1.) 应用程序在前台。

2.) 应用程序在后台。

在后台状态下,有可能是:

一个。您的应用程序不在视图中(因此在后台)但没有关闭并且仍然完全 运行ning。例如。你在另一个应用程序上。

乙。您的应用程序不在视图中(仍在后台)但已完全关闭且未完全 运行ning。只有推送通知订阅服务是 运行ning.

在您的情况下,您对 onMessageReceivedCallback 的代码处理无法完全正常工作的原因是 app 已关闭(案例 B),因此此处代码的一些上下文未定义。

例如,您的this.mainService是未定义的,只有当您运行应用程序或应用程序打开时,才可能构建此服务。

对此有多种解决方法,但主要是在您的 onMessageReceivedCallback 上,您应该通过以下方式分隔代码处理:

if (message.foreground) { 
   ... do stuff when app is foreground ... 
}
else { 
    ... handling if app is closed or in background. this code is executed when the user taps on the notification ... 
}

您必须将用户路由到 onMessageReceivedCallback 中的正确页面。可能在 'app loaded' 事件中。

你可以获得更多的想法here