Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Firebase Messaging - iOS device loses registration after first app exit #1281

Closed
bipinbipin opened this issue Jun 4, 2019 · 7 comments
Closed
Assignees
Labels
bug iOS Upstream Problem in the Firebase SDK
Milestone

Comments

@bipinbipin
Copy link

bipinbipin commented Jun 4, 2019

I am struggling with an issue where my iOS app / device gets push notifications great on the initial load. I can get notifications on the device by posting via a push token or via a subscribed topic.

Once the app is "exited" and started up again. I still receive a push token from firebase, however any attempt to post a message to that token/device or subscribed topic fails.

A device/token post to https://fcm.googleapis.com/fcm/send yields the below error. Which is confusing because I register/initialize fine and get the same push token as initially.

{
    "multicast_id": 6867418926211388062,
    "success": 0,
    "failure": 1,
    "canonical_ids": 0,
    "results": [
        {
            "error": "NotRegistered"
        }
    ]
}

Any attempts to subscribe, or unsubscribe to a topic result in this vague error (from Firebase I believe)

Error Domain=com.google.fcm Code=0 "(null)"

Below are some relevant files / code, please let me know if I can provide additional details.

package.json

{
  "nativescript": {
    "id": "com.mpr.watermain.mobile",
    "tns-android": {
      "version": "5.3.1"
    },
    "tns-ios": {
      "version": "5.3.1"
    }
  },
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "scripts": {
    "lint": "tslint \"src/**/*.ts\""
  },
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@types/node": "^12.0.0",
    "nativescript-angular": "~7.2.0",
    "nativescript-calendar": "^2.0.1",
    "nativescript-cardview": "^3.1.1",
    "nativescript-clipboard": "^1.1.7",
    "nativescript-effects": "^1.0.0",
    "nativescript-geolocation": "^5.1.0",
    "nativescript-plugin-firebase": "^8.3.2",
    "nativescript-pulltorefresh": "^2.2.2",
    "nativescript-ripple": "^2.2.1",
    "nativescript-social-share": "^1.5.1",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-toasty": "^1.3.1",
    "nativescript-ui-autocomplete": "^4.0.0",
    "nativescript-ui-listview": "^6.3.0",
    "nativescript-ui-sidedrawer": "^6.0.0",
    "nativescript-vibrate": "^2.1.3",
    "nativescript-web-image-cache": "^5.0.0",
    "reflect-metadata": "~0.1.12",
    "rxjs": "~6.3.0",
    "tns-core-modules": "~5.3.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~7.2.0",
    "@nativescript/schematics": "~0.5.0",
    "@ngtools/webpack": "~7.2.0",
    "codelyzer": "~4.5.0",
    "nativescript-dev-sass": "~1.7.0",
    "nativescript-dev-typescript": "~0.9.0",
    "nativescript-dev-webpack": "^0.24.0",
    "tslint": "~5.11.0",
    "uglifyjs-webpack-plugin": "^2.1.3"
  },
  "gitHead": "803cec054c44c99eaa59cdc267993e6346f6cb3a",
  "readme": "NativeScript Application"
}

firebase.nativescript.json

{
    "external_push_client_only": false,
    "using_ios": true,
    "using_android": true,
    "firestore": false,
    "realtimedb": false,
    "authentication": false,
    "remote_config": false,
    "performance_monitoring": false,
    "messaging": true,
    "in_app_messaging": false,
    "crashlytics": false,
    "crash_reporting": false,
    "storage": false,
    "functions": false,
    "facebook_auth": false,
    "google_auth": false,
    "admob": false,
    "invites": false,
    "dynamic_links": false,
    "ml_kit": false
}

app.component.ts

export class AppComponent extends FileAware implements OnInit, AfterViewInit {

  tabSelectedIndex: number;

  constructor(private firebaseService: FirebaseService,
              private appSettingsService: AppSettingsService) {
    super('AppComponent');
  }

  ngOnInit(): void {
    // Init your component properties here.
    console.log();
    this.log('constructor', '{{{ INITIALIZING APP }}}');
    this.firebaseService.registerForPush();
    this.firebaseService.checkAndLogFcmToken();
    this.appSettingsService.monitorApplicationEvents();
    this.appSettingsService.initializeSettings();
    // this.tabSelectedIndex = 1;
  }

firebase.service.ts

import { Injectable } from '@angular/core';
import * as Firebase from 'nativescript-plugin-firebase';
import { AppSettingsService } from '~/app/services/app-settings.service';
import * as AppSettings from 'tns-core-modules/application-settings';
import { FileAware } from '~/app/common/file-aware';

@Injectable({providedIn: 'root'})
export class FirebaseService extends FileAware {

  constructor(private appSettingsService: AppSettingsService) {
    super('FirebaseService');
  }

  registerForPush() {

    Firebase.init({
      onPushTokenReceivedCallback: (token: string): void => {
        this.log('registerForPush', 'Firebase plugin received a push token: ' + token);
      },

      onMessageReceivedCallback: (message: Firebase.Message) => {
        this.log('registerForPush', 'Push message received: ' + message.title);
        this.log('registerForPush', 'Push message received: ' + message.data.tabSelectedIndex);
        this.appSettingsService.tabSelectedIndex = message.data.tabSelectedIndex;
      },

      // Whether you want this plugin to automatically display the notifications or just notify the callback.
      // Currently used in iOS only. Default true.
      showNotifications: true,

      // Whether you want this plugin to always handle the notifications when the app is in foreground.
      // Currently used in iOS only. Default false.
      showNotificationsWhenInForeground: true
    }).then(
      () => this.log('registerForPush', 'Registered for push'),
      error => this.error('registerForPush', 'Could not register for push', error)
    );
  }

  checkAndLogFcmToken() {
    Firebase.getCurrentPushToken().then(
      (token: string) => {
        // may be null if not known yet
        this.log('checkAndLogFcmToken', `Current push token: ${token}`);
      },
      (error) => this.error('checkAndLogFcmToken', 'No current push token', error)
    );
  }

  togglePushTopicSubscription(topic: string, add: boolean) {
    if (add) {
      Firebase.subscribeToTopic(topic).then(
        () => this.log('togglePushTopicSubscription',
          `############## Subscribed to topic - ${topic} ####################`),
        error => this.error('togglePushTopicSubscription', `Could not subscribe to ${topic}`, error)
      );
    } else {
      Firebase.unsubscribeFromTopic(topic).then(() =>
        console.log('togglePushTopicSubscription',
          `-------------- Unsubscribed from topic - ${topic} -----------------`),
        error => this.error('togglePushTopicSubscription', `Could not unsubscribe to ${topic}`, error)
      );
    }
  }

I used to be able to get messaging to work consistenly with the below workaround:

Go into the /platforms/ios/Podfile and change:
pod 'Firebase/Core', '~> 5.20.1'

to:
pod 'Firebase/Core', '~> 5.15.0'

do a pod update and then rebuild. in that scenario messaging would work all the time. And it would not lose registration with firebase and messages would work via push token or via topic.

But for some reason I can no longer use that workaround. Because Xcode cannot link FirebaseInstanceID (which gets dropped down to 3.8.1)

I get the following in Xcode:
ld: warning: directory not found for option '-F/Users/astondeveloper/Library/Developer/Xcode/DerivedData/commprwatermainmobile-dwzdiavvnqzkxncvbtppyktvlafx/Build/Intermediates.noindex/ArchiveIntermediates/commprwatermainmobile/BuildProductsPath/Release-iphoneos/FirebaseInstanceID' ld: framework not found FirebaseInstanceID

and the same from tns cli build:
`
ld: warning: directory not found for option '-F/Users/astondeveloper/Documents/DEV/com.mpr.watermain.mobile/platforms/ios/build/Debug-iphonesimulator/FirebaseInstanceID'
ld: framework not found FirebaseInstanceID
clang: error: linker command failed with exit code 1 (use -v to see invocation)
note: Using new build systemnote: Planning buildnote: Constructing build description
** BUILD FAILED **

Command xcodebuild failed with exit code 65
`

Obviously, I'd like to just figure out what is causing it to lose registration on version 5.20, but I am on a crazy tight timeline and would be happy to just get the 5.15 workaround to work again.

@nickykln
Copy link
Contributor

nickykln commented Jun 5, 2019

+1, Please Mister @EddyVerbruggen :)

Thanks a lot,

N

@bipinbipin
Copy link
Author

@EddyVerbruggen I know your a busy guy and getting to the bottom of these issues is tough with the diversity of projects that use your plugin.

Please let me know if i can provide additional information / files / clarification that can help you.

I was for some reason able to build once (just once) after switch Podfile version to 5.15.0 still very perplexed as to why it sometimes builds the pod but most of the time skips over it. I have removed every other pod from my Podfile to no avail.

@joshstallnick
Copy link

Works great on Android so far - ios not so much .

My clients are also receiving their token but it shows not registered from Firebase and not entirely certain on a good fix. I tried @bipinbipin solution but still having issues building...

@bipinbipin
Copy link
Author

bipinbipin commented Jun 5, 2019

I got my downgrade workaround to build again.

For anyone who needs the 5.15.0 workaround. I had to edit:
/node_modules/nativescript-plugin-firebase/plafroms/ios/Podfile

pod 'Firebase/Core', '~> 5.20.1'

to:
pod 'Firebase/Core', '~> 5.15.0'

before running any tns commands. this allowed it to build in Xcode and tns cli

if i edit the Podfile that tns creates, and do a pod update it does not build all the pods. (....sometimes)

@bgrand-ch
Copy link

+1, Please Mister @EddyVerbruggen 🙏

Thanks a lot,

Ben

@EddyVerbruggen EddyVerbruggen self-assigned this Jun 8, 2019
@EddyVerbruggen EddyVerbruggen added bug iOS Upstream Problem in the Firebase SDK labels Jun 8, 2019
@EddyVerbruggen EddyVerbruggen added this to the 8.4.0 milestone Jun 8, 2019
@EddyVerbruggen
Copy link
Owner

Hey all, thanks for reporting this issue.

Looks like the problem creeped in between Firebase iOS SDK 5.15 and 5.20, and lots of users of the native iOS SDK noticed similar problems.

The good news is, it looks like it was fixed in Firebase iOS SDK 6.1.0 (scroll down to the FCM issues, where issue 2438 is mentioned).

The not so good news is you'll have to wait for me to bump the plugin to use the latest native SDKs and that's usually quite a bit of work because of all those components involved. It's even a major version bump on both platforms, so there's probably a few breaking changes I'll have to fix and test.

Keep an eye on issue #1283 where I'll keep track of the updating progress.

@bgrand-ch
Copy link

@EddyVerbruggen Thank you very much for your work and your speed 😉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug iOS Upstream Problem in the Firebase SDK
Projects
None yet
Development

No branches or pull requests

5 participants