Firebase.onnotification()在android中收到通知时不起作用

问题描述 投票:0回答:1

我正在为Android和iOS开发一款ionic4应用,我收到通知,但未调用Firebase.onnotification(),但是当我tap收到通知时,它将在中调用函数only app背景。但是,当我单击通知时,处于foreground(running)状态的应用程序没有任何反应。

// 此应用会在初始化时被调用

 initializeApp() {
       this.platform.ready().then(() => {
       this.statusBar.styleLightContent();

       this.splashScreen.hide();
       this.backButtonEvent();
       if (this.platform.is('cordova')) {
        // Initialize push notification feature
        if (this.platform.is('android') === true) {
          this.notificationSetup();
        }
      } else {
        console.log('Push notifications are not enabled since this is not a real device');
      } 
   }

此方法不会随时被调用

    private notificationSetup() {
    debugger
    console.log('notification setup');
    this.notificationsService.getToken();


    this.firebase.onNotification().subscribe(
      (msg: NotificationData) => {
        console.log('notification msg', JSON.stringify(msg));
        if (msg.wasTapped) {
         //this will work on background state  
          console.log('Received in background');

        } else {
         //this will never call
          console.log('Received in foreground');

        }
      }, error => console.error('Error with Push plugin', error));
  }

和通知服务类别

         export class NotificationsService {

  constructor(private firebase: FCM,
    private platform: Platform) { }

    async getToken() {
      let token;
      if (this.platform.is('android')) {
        token = await this.firebase.getToken();
        console.log('fcm token', token);
      }
     // this.saveToken(token);
    }



    topicSubscription(topic) {
      this.firebase.subscribeToTopic(topic).then((res:any) => {
        console.log('Subscribed to topic: ' + topic, res);
      });
    }

    topicUnsubscription(topic) {
      this.firebase.unsubscribeFromTopic(topic).then((res:any) => {
        console.log('Unsubscribed from topic: ' + topic, res)
      });
    }

    onNotifications() {
      console.log("onnotification method")
      debugger
      return this.firebase.onNotification();
    }
}

我正在使用此

   import { FCM } from '@ionic-native/fcm/ngx';

package.json文件-

               {
  "name": "Appname",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "~8.1.2",
    "@angular/core": "~8.1.2",
    "@angular/forms": "~8.1.2",
    "@angular/platform-browser": "~8.1.2",
    "@angular/platform-browser-dynamic": "~8.1.2",
    "@angular/router": "~8.1.2",
    "@ionic-native/android-permissions": "^5.21.5",
    "@ionic-native/calendar": "^5.0.0-beta.14",
    "@ionic-native/clipboard": "^5.20.0",
    "@ionic-native/core": "^5.17.1",
    "@ionic-native/date-picker": "^5.19.1",
    "@ionic-native/fcm": "^5.21.5",
    "@ionic-native/file": "^5.21.5",
    "@ionic-native/file-transfer": "^5.21.5",
    "@ionic-native/firebase": "^5.21.4",
    "@ionic-native/keyboard": "^5.17.1",
    "@ionic-native/native-storage": "^5.17.1",
    "@ionic-native/secure-storage": "^5.17.1",
    "@ionic-native/splash-screen": "^5.17.1",
    "@ionic-native/status-bar": "^5.17.1",
    "@ionic-native/wheel-selector": "^5.19.1",
    "@ionic/angular": "^4.11.5",
    "@ionic/storage": "^2.2.0",
    "@types/crypto-js": "^3.1.43",
    "@types/hammerjs": "^2.0.36",
    "bootstrap": "^4.4.1",
    "cordova": "^9.0.0",
    "cordova-android": "^8.1.0",
    "cordova-browser": "^6.0.0",
    "cordova-plugin-android-permissions": "^1.0.2",
    "cordova-plugin-androidx": "^1.0.2",
    "cordova-plugin-androidx-adapter": "^1.1.0",
    "cordova-plugin-datepicker": "^0.9.3",
    "cordova-plugin-fcm-with-dependecy-updated": "^4.1.1",
    "cordova-plugin-file": "^6.0.2",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-firebase-lib": "^5.1.1",
    "cordova-plugin-firebase-messaging": "4.0.1",
    "cordova-plugin-firebasex": "^7.0.2",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-nativestorage": "^2.3.2",
    "cordova-plugin-splashscreen": "^5.0.3",
    "cordova-support-android-plugin": "^1.0.1",
    "cordova-support-google-services": "^1.3.2",
    "cordova-wheel-selector-plugin": "^1.1.4",
    "core-js": "^2.6.11",
    "crypto-js": "^3.1.9-1",
    "hammerjs": "^2.0.8",
    "ionic": "^5.4.16",
    "ionic-long-press": "^2.0.2",
    "lodash": "^4.17.15",
    "native-run": "^0.3.0",
    "rxjs": "^6.5.4",
    "rxjs-compat": "^6.5.4",
    "tslib": "^1.9.0",
    "web-animations-js": "^2.3.2",
    "zone.js": "^0.9.1"
  },
  "devDependencies": {
    "@angular-devkit/architect": "~0.801.2",
    "@angular-devkit/build-angular": "^0.803.24",
    "@angular-devkit/core": "~8.1.2",
    "@angular-devkit/schematics": "~8.1.2",
    "@angular/cli": "~8.1.2",
    "@angular/compiler": "~8.1.2",
    "@angular/compiler-cli": "~8.1.2",
    "@angular/language-service": "~8.1.2",
    "@ionic/angular-toolkit": "~2.0.0",
    "@ionic/lab": "^2.0.19",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/lodash": "^4.14.149",
    "@types/node": "~8.9.4",
    "codelyzer": "^5.2.1",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-ionic-webview": "^4.1.3",
    "cordova-plugin-statusbar": "^2.4.3",
    "cordova-plugin-whitelist": "^1.3.4",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.5.1",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.4.3"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-ionic-webview": {
        "ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
      },
      "cordova-plugin-nativestorage": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-datepicker": {},
      "cordova-wheel-selector-plugin": {},
      "cordova-plugin-firebasex": {
        "FIREBASE_ANALYTICS_COLLECTION_ENABLED": "true",
        "FIREBASE_PERFORMANCE_COLLECTION_ENABLED": "true",
        "FIREBASE_CRASHLYTICS_COLLECTION_ENABLED": "true",
        "ANDROID_ICON_ACCENT": "#FF00FFFF",
        "ANDROID_PLAY_SERVICES_TAGMANAGER_VERSION": "17.0.0",
        "ANDROID_FIREBASE_ANALYTICS_VERSION": "17.2.1",
        "ANDROID_FIREBASE_MESSAGING_VERSION": "20.0.0",
        "ANDROID_FIREBASE_CONFIG_VERSION": "19.0.3",
        "ANDROID_FIREBASE_PERF_VERSION": "19.0.1",
        "ANDROID_FIREBASE_AUTH_VERSION": "19.1.0",
        "ANDROID_CRASHLYTICS_VERSION": "2.10.1",
        "ANDROID_CRASHLYTICS_NDK_VERSION": "2.1.1"
      },
      "cordova-plugin-firebase-lib": {},
      "cordova-plugin-firebase-messaging": {
        "FIREBASE_MESSAGING_VERSION": "20.1.+",
        "FIREBASE_ANALYTICS_VERSION": "17.2.+",
        "ANDROIDX_CORE_VERSION": "1.0.+"
      },
      "cordova-plugin-file": {},
      "cordova-plugin-file-transfer": {},
      "cordova-plugin-android-permissions": {}
    },
    "platforms": [
      "browser",
      "android"
    ]
  }
}

当用户点击来自android studio的通知时,[在后台运行应用程序时]的日志-

FCMPlugin: ==> FCMPluginActivity onCreate
2020-02-19 11:17:12.441 7860-7860/ D/FCMPlugin: ==> USER TAPPED NOTFICATION
2020-02-19 11:17:12.441 7860-7860/ D/FCMPlugin: Key: google.delivered_priority Value: high
2020-02-19 11:17:12.442 7860-7860/D/FCMPlugin: Key: google.sent_time Value: 1582091222156
2020-02-19 11:17:12.442 7860-7860/D/FCMPlugin: Key: google.ttl Value: 2419200
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: google.original_priority Value: high
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: alarmId Value: 15
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: msg Value: alarm assgned to you 
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: body Value: bbd started
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: from Value: /topics/all
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: title Value: dfbdfb CORDOVA
2020-02-19 11:17:12.442 7860-7860/ D/FCMPlugin: Key: google.message_id Value: 0:15
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: Key: notificationType Value: user
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: Key: collapse_key Value: xxx
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: ==> FCMPlugin sendPushPayload
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: notificationCallBackReady: true
2020-02-19 11:17:12.442 7860-7860/xxx D/FCMPlugin: gWebView: org.apache.cordova.CordovaWebViewImpl@5fff06b
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: msg => alarm assgned to you 
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: wasTapped => true
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.original_priority => high
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.sent_time => 1582091222156
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.delivered_priority => high
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: notificationType => user
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: body => bfdb started
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: title => fbdf CORDOVA
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.message_id => 0:1
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: collapse_key => xxx
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: alarmId => 15
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: google.ttl => 745
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: payload: from => /topics/all
2020-02-19 11:17:12.443 7860-7860/xxx D/FCMPlugin: Sent PUSH to view: javascript:FCMPlugin.onNotificationReceived({"msg":"alarm assgned to you ","wasTapped":true,"google.original_priority":"high","google.sent_time":15,"google.delivered_priority":"high","notificationType":"user","body":"RORM started","title":"784 CORDOVA","google.message_id":"0:1","collapse_key":"xxx","alarmId":"1","google.ttl":2,"from":"\/topics\/all"})

当用户点击通知时(来自Android Studio,当应用程序在前台时]的日志] >>

    2020-02-19 12:12:40.990 11337-11528/xxxx I/FirebasePlugin: Received message: notification
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: From: /topics/all
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Id: 0:5435325111132
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Title: gy8u CORDOVA
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Body: oyb started
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Sound: default
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Vibrate: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Light: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Color: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Icon: fcm_push_icon
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Channel Id: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Visibility: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: Priority: null
2020-02-19 12:12:40.990 11337-11528/xxxx D/FirebasePlugin: sendMessage(): messageType=notification; showNotification=true; id=0:475%; title=ou CORDOVA; body=uin started; sound=default; vibrate=null; light=null; color=null; icon=fcm_push_icon; channel=null; data={alarmId=1, msg=alarm assgned to you , body=uybub started, title=uyb CORDOVA, notificationType=user}
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Channel ID: fcm_default_channel
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Small icon: application
2020-02-19 12:12:40.991 11337-11528/xxxx D/FirebasePlugin: Large icon: application
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Color: default
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Visibility: 1
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: Priority: 2
2020-02-19 12:12:40.993 11337-11528/xxxx D/FirebasePlugin: show notification: Notification(channel=fcm_default_channel pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0xff00ffff vis=PUBLIC)
2020-02-19 12:13:10.016 11337-11337/xxxx D/FirebasePlugin: OnNotificationOpenReceiver.onReceive(): Bundle[{alarmId=96, messageType=notification, id=0:iu90%, msg=alarm assgned to you , tap=foreground, ttl=2419200, body=uvyy started, from=/topics/all, icon=fcm_push_icon, sound=default, title=vui CORDOVA, sent_time=1582094560869, notificationType=user, collapse_key=xxxx, show_notification=true}]
2020-02-19 12:13:10.019 11337-11337/xxxx D/CordovaActivity: Paused the activity.
2020-02-19 12:13:10.020 11337-11337/xxxx D/CordovaActivity: Resumed the activity.

基于前景模式下应用程序的日志,它不会在创建时调用FCMPluginActivity onCreate。但只能在后台模式下使用。请给我解决方案或建议

<< [

当前景中的应用程序可以使用时,请将其添加到将起作用的应用程序初始化方法(app.component.ts)中-
(<any>window).FirebasePlugin.onMessageReceived((message) => { try { console.log(`Notification received - ${message}`); } catch (error) { console.error('error notification receive', error); } });
android angular firebase firebase-cloud-messaging ionic4
1个回答
0
投票
(<any>window).FirebasePlugin.onMessageReceived((message) => { try { console.log(`Notification received - ${message}`); } catch (error) { console.error('error notification receive', error); } });
© www.soinside.com 2019 - 2024. All rights reserved.