(离子 4)当应用程序处于前台时无法使用 firebaseX 接收通知

(Ionic 4) Can't receive notification with firebaseX when app is in foreground

我使用 FirebaseX 在我的应用程序中接收通知。 我在后台正确收到通知。但是当应用程序在前台时,没有任何反应......

我已经搜索过这个问题,但对我没有任何帮助。 我尝试在数据内容中包含以下数据(当服务器调用 url 发送通知时) [notification_foreground] => 真 [点击] => 真

这是我的插件列表:

cordova-plugin-androidx-adapter 1.1.0 "cordova-plugin-androidx-adapter"
cordova-plugin-app-version 0.1.9 "AppVersion"
cordova-plugin-badge 0.8.8 "Badge"
cordova-plugin-device 2.0.3 "Device"
cordova-plugin-dialogs 2.0.2 "Notification"
cordova-plugin-fcm-with-dependecy-updated 3.0.0 "Cordova FCM Push Plugin"
cordova-plugin-file 6.0.2 "File"
cordova-plugin-firebasex 5.0.0 "Google Firebase Plugin"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 4.1.1 "cordova-plugin-ionic-webview"
cordova-plugin-local-notification 0.9.0-beta.2 "LocalNotification"
cordova-plugin-speechrecognition 1.1.2 "Speech Recognition"
cordova-plugin-splashscreen 5.0.3 "Splashscreen"
cordova-plugin-statusbar 2.4.3 "StatusBar"
cordova-plugin-whitelist 1.3.4 "Whitelist"

离子信息:

Ionic:

   Ionic CLI                     : 5.2.8 
   Ionic Framework               : @ionic/angular 4.9.0
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.1.1, (and 23 other plugins)

Utility:

   cordova-res : 0.5.1
   native-run  : 0.2.7

System:

   NodeJS : v10.16.0 
   npm    : 6.10.1
   OS     : Windows 10

订阅消息的代码...

this.firebase.onMessageReceived().subscribe(notification => {
            console.log('NOTIFICATION !!!!');
            console.log(notification);
            alert( JSON.stringify(notification) );
            if (notification['tap']) {
                this.navCtrl.navigateRoot('/test');
            } else {
                alert( JSON.stringify(notification) );
            }
        });

当应用程序在后台时,我会收到通知,当我点击它时,我会看到警告消息和控制台日志。

但是当应用程序在前台时,没有任何显示。

感谢您的帮助。

使用 --prod 似乎无需执行任何其他操作即可工作... 它处于无法正常工作的调试模式

来自https://github.com/dpa99c/cordova-plugin-firebasex#background-notifications

如果应用程序在前台 运行 时通知消息到达,默认情况下它不会显示为系统通知。相反,通知消息有效负载将传递给插件处理的 onMessageReceived 回调(不会设置 tap)。

如果您在数据负载中包含 notification_foreground 键,当应用程序在前台 运行 时,插件也会在收到通知消息时显示系统通知。例如:

{ "name": "my_notification", "notification":{ "body": "Notification body", "title": "Notification title" }, "data":{ "notification_foreground": "true", } }

.麦盖斯

cordova-plugin-firebasex 插件无法与 cordova-plugin-local-notification 插件一起使用我已经遇到过这个问题进入我当前的 ionic ios 应用程序,但使用 android,它运行完美

根据@dpa99c

问题是这个插件和 cordova-plugin-local-notifications 都实现了相同的 UNUserNotificationCenterDelegate willPresentNotification 委托,并且由于委托与其父 class 有 1:1 关系,所以只有一个委托被调用 - 在这种情况下,cordova-plugin-firebasex 委托响应由 cordova-plugin-local-notifications 创建的通知。

详细说明请查看下方link

https://github.com/dpa99c/cordova-plugin-firebasex/issues/230

解法: 请将 this 本地通知插件与 cordova-plugin-firebasex 一起使用它在 android 和 iOS 两个平台

中都能完美运行

这与产品、QA 或调试模式无关。对于触发的前台消息,您需要在通知负载中的数据对象下合并“notification_foreground”:“true”。

示例:

{
 "to" : "your device token",
 "notification" :{
     "body" : "your message body",
     "title": "your title for notification"
 },
  "data":{
       "notification_foreground": "true"
  }
 }