capacitor-community / fcm

Enable Firebase Cloud Messaging for Capacitor apps
https://capacitor.ionicframework.com/docs/
MIT License
238 stars 83 forks source link

Can't delete instance after first performed #128

Open jadsy2107 opened 1 year ago

jadsy2107 commented 1 year ago

In my service i have a simple toggle function that to Enable / Disabled notifications with a toggle switch to fire it It works, creates the token, saves to database and then removes it when toggled again.... the first time only.

steps to reproduce, click toggle 4 times

  1. toggle = true; - works and updates database as expected.
  2. toggle = false; - works and deleteInstance() is fired without error , however messaging occurs
  3. toggle = true; works and updates database as expected.
  4. toggle = false; fails with error

code: "The operation couldn’t be completed. Installation for appID appName __FIRAPP_DEFAULT not found"

errorMessage: "Cant delete Firebase Instance ID"

message: "Cant delete Firebase Instance ID"

  async togglePush(toggle): Promise<boolean> {
    if (toggle) {
      await PushNotifications.requestPermissions();
      await PushNotifications.register();
      const pushPerms = await PushNotifications.checkPermissions();
      if (pushPerms.receive == 'granted') {
        // await FCM.refreshToken();
        await FCM.getToken()
        .then((r) => {
          this.fcmToken = r.token;
        })
        .catch((err) => console.log(err));
        await this.saveFcmToken(this.fcmToken);
        console.log(this.fcmToken);
        return true;
      } else {
        this.fcmToken = null;
        await this.saveFcmToken(null);
        return false;
      }
    } else {
      console.log('deleting instance');
      await FCM.deleteInstance();  ///////////////// THIS THROWS AN ERROR EVERYTIME AFTER INITIAL DELETE 
      this.fcmToken = null;
      await this.saveFcmToken(null);
      return false;
    }
  }

And in my component;

  async togglePush() {
    await this.delay(100);
    this.enableNotifications = await this.userService.togglePush(this.enableNotifications);
  }

the set token function

  async saveFcmToken(fcmToken) {
    const deviceId = await this.getDeviceId();
    const deviceDoc: AngularFirestoreDocument = this.afs.doc(`/devices/${deviceId}`);
    const data = {
      lastUpdated: firestore.serverTimestamp(),
      deviceId: deviceId,
      fcmToken: fcmToken
    }
    await deviceDoc.set(data, {merge: true});
  }
jadsy2107 commented 1 year ago

it's not a show stopper as I can use the value "fcmToken" in the database which is null when toggled off