(离子)如果应用程序被终止,则不会调用 handleNotificationReceived

(Ionic) handleNotificationReceived does not get called if the app is killed

我正在开发一个离子应用程序,我正在使用 onesignal 进行推送通知。

我面临的问题是,当应用程序未被终止(即从最近的应用程序中删除)时,handleNotifiactionRecieved() 不会在收到推送通知时被触发。虽然它在应用程序 运行 但在后台(不是 inFocus)时按预期工作。

设置代码是这样的:

if (this.platform.is('cordova')) {
        this.oneSignal.startInit('APP_ID');
        this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
        this.openOneSignalMessage();
        this.oneSignal.endInit();
      }

我的代码是这样的:

openOneSignalMessage() {
this.oneSignal.handleNotificationReceived().subscribe((data) => {
      this.MsgBody = data.payload.body
      console.log('newMessageOneSignal MsgBody', this.MsgBody);
 });
}

actual result: handleNotificationReceived() doesn't get called when the app is not running, that is killed.

expected result: handleNotificationReceived() should be called every time a push notification arrives even when the app is not open.

如何每次触发该方法?

提前致谢。

我在文档中找到了这个:

Sets a notification received handler. Only called if the app is running in the foreground or background at the time the notification was received.

可能您的应用无法在后台运行。

或者问题是您需要在 startinit 之后调用您的函数:

this.onesignal.startInit("YOUR_APPID")
this.onesignal.handleNotificationReceived() 

感谢您指出文档中的问题。它们已更新为正确的信息:

handleNotificationReceived(生成器方法)

设置通知接收处理程序。仅当收到通知时应用程序 运行 在 前景 时才被调用。

如果你想从杀死状态处理,请确保使用 handleNotificationOpened method