EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 444 forks source link

When I subscribe topics in ios. First start is ok, but second is error. #1237

Closed khhily closed 5 years ago

khhily commented 5 years ago

I create a project with nativescript-angular. And I found a strange issue in my project. The firebase subscribe topics success when I run tns platform remove ios, tns platform add ios and tns run ios. but when I change some code, or I kill the process in iOS, it will restart the app, then the subscribe is error. I don't know why is this problem occurring.

package.json:

"dependencies": {
    "@angular/animations": "~7.1.0",
    "@angular/common": "~7.1.0",
    "@angular/compiler": "~7.1.0",
    "@angular/core": "~7.1.0",
    "@angular/forms": "~7.1.0",
    "@angular/http": "~7.1.0",
    "@angular/platform-browser": "~7.1.0",
    "@angular/platform-browser-dynamic": "~7.1.0",
    "@angular/router": "~7.1.0",
    "@danvick/ngx-translate-nativescript-loader": "^2.0.0",
    "@ngx-translate/core": "^11.0.1",
    "crypto-js": "^3.1.9-1",
    "nativescript-angular": "~7.1.0",
    "nativescript-appavailability": "^1.3.1",
    "nativescript-clipboard": "^1.1.7",
    "nativescript-code-push": "^1.0.1",
    "nativescript-exit": "^1.0.1",
    "nativescript-facebook": "^3.0.0",
    "nativescript-feedback": "^1.3.5",
    "nativescript-hex-string": "0.0.4",
    "nativescript-imagepicker": "^6.1.2",
    "nativescript-localstorage": "^2.0.0",
    "nativescript-microsoft-appcenter": "^1.2.1",
    "nativescript-plugin-firebase": "^8.2.0",
    "nativescript-social-share": "^1.5.1",
    "nativescript-theme-core": "^1.0.4",
    "nativescript-toast": "^2.0.0",
    "nativescript-urlhandler": "^1.2.3",
    "nativescript-wechat-share-plugin": "0.0.1",
    "nativescript-windowed-modal": "^5.0.6",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~6.3.3",
    "rxjs-compat": "^6.4.0",
    "tns-core-modules": "^5.1.0",
    "tns-platform-declarations": "^5.1.2",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/core": "~7.1.0",
    "@angular/cli": "^7.1.0",
    "@angular/compiler-cli": "~7.1.0",
    "@nativescript/schematics": "~0.4.0",
    "@ngtools/webpack": "^7.1.4",
    "nativescript-dev-sass": "^1.6.0",
    "nativescript-dev-typescript": "~0.7.0",
    "nativescript-dev-webpack": "^0.19.0",
    "tns-android": "^5.1.0",
    "tns-ios": "^5.1.1",
    "typescript": "~3.1.1",
    "uglifyjs-webpack-plugin": "^2.1.1"
  },

I add require("nativescript-plugin-firebase"); in my main.ts.

I init and subscribe topics use these code:

private registerForPushNotifications() {
    const option = {
      showNotifications: true,
      showNotificationsWhenInForeground: true,
      onMessageReceivedCallback: (msg: firebase.Message) => {
        console.log('I received message: ' + JSON.stringify(msg));
        if (msg.data) {
          console.log(msg.data.type);
          let event = this.messageReceivedEvents.get(msg.data.type);
          if (!event) {
            event = this.onMessageReceived(msg.data.type);
          }
          let data: any = msg.data.c;
          try {
            data = JSON.parse(data);
          } catch (e) {
          }
          event && event.emit(data);
        }
      },
    };
    return new Promise((resolve, reject) => {
      console.log('start init firebase');
      firebase.init(option).then(() => {
        resolve();
      }, err => {
        resolve();
      });
      console.log('init firebase end');
    });
  }

  async subscribe(topic: string) {
    return firebase.subscribeToTopic(topic).then(() => {
      this.trace.setEvent('LogEvent', [{ key: 'Subscribe topic', value: `Subscribe topic: ${topic}` }]);
    }, err => {
      this.trace.setEvent('LogEvent', [{ key: 'Subscribe topic ' + topic + ' error', value: `${err}` }]);
    });
  }

and the error is :

EVENT - LogEvent - {"subscribe topic gameend error":"The operation couldn’t be completed. (com.google.fcm error 0.)"}
EVENT - LogEvent - {"subscribe topic gameentry error":"The operation couldn’t be completed. (com.google.fcm error 0.)"}
// ....
EddyVerbruggen commented 5 years ago

You better Google that error. I have no clue.

khhily commented 5 years ago

I find a solution here. I need to downgrading ios-sdk to 5.15.0. How to change the ios-sdk version in my project? or what version of this plugin use sdk is 5.15.0?

EddyVerbruggen commented 5 years ago

@khhily Looking at that issue, it was fixed in 5.17.0. The plugin is at 5.20.1, so it should not be a problem using the latest plugin release.

khhily commented 5 years ago

But someone use 5.18, 5.19 or 5.20.1 still not work. And downgrade to 5.15, it's works. https://github.com/firebase/firebase-ios-sdk/issues/2372#issuecomment-473987298

EddyVerbruggen commented 5 years ago

If that’s then keep an eye on the upstream issue. Not much I can do about it.

khhily commented 5 years ago

I downgrade to version 7.7.0 of this plugin with ios-sdk 5.15.0, now it works.