question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

firebase subscribeToTopic error

See original GitHub issue

I’m not able to subscribe to a topic, below my app.ts excerpt

import "./bundle-config";
import * as app from 'application';

import * as firebase from "nativescript-plugin-firebase";

firebase.init({
    onMessageReceivedCallback: (message: firebase.Message) => {
        console.log(`Title: ${message.title}`);
        console.log(`Body: ${message.body}`);
        // if your server passed a custom property called 'foo', then do this:
        console.log(`value of 'foo': ${message.data.foo}`);
    }

}).then(
    instance => {
        console.log(`firebase.init done !! [${instance}]`);

         firebase.subscribeToTopic("/topics/musa-news").then(
            (value) => console.log("sub ok:", value),
            (reason) => console.log("sub err:", reason)
        );
    },
    error => {
        console.log(`firebase.init error: ${error}`);
    }
);

In the console I got:

JS: sub err: Can be run only after init

Probably I don’t understand when it is safe to call subscribeToTopic, the message says after init but apparently the init then() context is not such place.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
pjuroscommented, Dec 13, 2017

For me, file was firebase.nativescript.json located at the root folder of project and editing it didn’t work.

How I fixed it:

cd <root of your project>
cd node_modules/nativescript-plugin-firebase
npm run config
<this time be sure to enable Real time database>
cd <root of your project>
tns platform remove android
tns run anroid

You can check if firebase instance exists with:

firebase.init(
   {
      onPushTokenReceivedCallback: function(token) {
         console.log("app.js => onPushTokenReceivedCallback - Firebase plugin received a push token: " + token);
      },
      onMessageReceivedCallback: function(message) {
         console.log("app.js => onMessageReceivedCallback - " + JSON.stringify(message));
      }
   }).then(
      function (instance) {
         console.log("app.js => firebase.init done, instance = " + instance);
         firebase.subscribeToTopic("all").then(
            () => {console.log("app.js => subscribeToTopic(all): OK => subscribed");},
            (error) => {console.log("app.js => subscribeToTopic(all): ERROR => " + error);}
         );
      },
      function (error) {
         console.log("app.js => firebase.init error: " + error);
      }
   );
1reaction
ghostcommented, Dec 6, 2017

I had the same problem, I did some research and I found out that “firebase.instance” is null and that was caused by “com.google.firebase.database” being undefined.

How i fixed it I edited the nativescript.firebase.json and set “realtimedb” to true, after that it worked!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trying to subscribe to topic on Firebase Cloud Messaging ...
When i try to subscribe to a topic i get the following error: .subscribeToTopic is not a function. const messaging = firebase.messaging(); ...
Read more >
Topic messaging on Android - Firebase - Google
If you provide an array with over 1,000 registration tokens, the request will fail with a messaging/invalid-argument error. The subscribeToTopic() and ...
Read more >
Cloud Messaging | React Native Firebase
Subscribing to topics. To subscribe a device, call the subscribeToTopic method with the topic name (must not include "/"):.
Read more >
Cloud Messaging | FlutterFire
Before using Firebase Cloud Messaging, you must first have ensured you have ... To subscribe a device, call the subscribeToTopic method with the...
Read more >
FCM, send to topic and "Not Registered" error - Google Groups
to Firebase Google Group. Hello there, I developed an Android app that subscribes to a topic ( subscribeToTopic() ) and then receives notifications...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found