【问题标题】:IONIC 5 FCM Plugin onNotification not fired when app is killed/closed (IOS & Android)IONIC 5 FCM Plugin onNotification 应用程序被杀死/关闭时未触发(IOS 和 Android)
【发布时间】:2020-10-18 15:58:07
【问题描述】:

我在两个平台上都遇到了 FCM 插件 (https://ionicframework.com/docs/native/fcm) 的问题,他的问题是我无法在应用关闭/杀死时处理通知,onNotification () 方法没有被触发(它在后台和前台都能正常工作)

this.fcm.onNotification().subscribe((payload) => {
          if (payload.wasTapped) {
            console.log('Notification received in background');
          } else {
            console.log('Notification received in foreground');
          }
          console.log('Notification payload: ',payload);
  });

通知主体在后台和前台工作正常(两个平台):

{
 "to" : <device-key>,
"collapse_key" : "app-key",
 "notification" : {
     "body" : "body",
     "title": "title"
 },
 "data" : {"parameter":"1"},
 "android": {
    "notification": {
      "click_action": "FCM_PLUGIN_ACTIVITY"
    }
  }
}

IOS 环境的离子信息输出:

Ionic:

   Ionic CLI                     : 6.11.10 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.3.3
   @angular-devkit/build-angular : 0.901.12
   @angular-devkit/schematics    : 9.1.12
   @angular/cli                  : 9.1.12
   @ionic/angular-toolkit        : 2.3.3

Cordova:

   Cordova CLI       : 10.0.0
   Cordova Platforms : ios 6.1.1
   Cordova Plugins   : cordova-plugin-fcm-with-dependecy-updated: ^7.3.1

System:

   ios-sim : 8.0.2
   NodeJS  : v14.13.0 (/usr/local/Cellar/node/14.13.0/bin/node)
   npm     : 6.14.8
   OS      : macOS Catalina
   Xcode   : Xcode 12.0.1 Build version 12A7300

【问题讨论】:

  • Ionic 5 + Angular 仍然无法正常工作,您找到解决方案了吗?谢谢
  • 不幸的是我没有 -.-

标签: ionic-framework notifications ionic5 cordova-plugin-fcm


【解决方案1】:

我相信您的订阅是在构造函数内部或 onInit 函数内部。

无论应用程序是在前台还是后台,都没有关系,因为应用程序正在运行,所以订阅会被调用。

但是当应用程序被杀死时,订阅如何被调用?当您收到通知时,点击它。然后只有应用程序启动。仅在应用启动后,订阅才有效。

您要做的是,在应用启动时调用fcm.getInitialPushPayload()

将以下内容放在构造函数中。

this.platform.ready().then(() => {

    this.fcm.getInitialPushPayload().then((payload) => {
  
      if(payload.wasTapped) {
        console.log("Received FCM when app is closed -> ", JSON.stringify(payload));
        // call your function to handle the data
        this._handlePushNotificationData(payload);
      }                                 
      
    });

}

现在,当应用程序关闭时,当您点击收到的通知时,应用程序将启动并调用上述函数。然后您的数据将在回调中可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多