smartnav / fcm-notification

Push notificaton for ios and android
24 stars 13 forks source link

"Firebase App already exists" error #3

Open relu-xz opened 5 years ago

relu-xz commented 5 years ago

Hi.

When I use your library consecutively, got the following error:

Firebase app already exists

Thanks.

relu-xz commented 5 years ago

Fixed in #4

smartnav commented 5 years ago

Can you share me exact scenario, because I'm not able to find this issue, this issue was with old app not new one.

relu-xz commented 5 years ago

I installed this lib using:

npm install fcm-notification --save

And then after the second call/use, I got that error.

What do you mean with "old app"?

apf

relu-xz commented 5 years ago

@smartnav Can you check it? Thanks in advance.

smartnav commented 5 years ago

@zorobabel ,

This issue is not coming from my end, can you please share the code with me with the same scenario. So that I can put check on it.

Thanks, Navish

matheusdavidson commented 5 years ago

The pull request works for me, can you please release a new version with that? Thank you

amirzandi commented 4 years ago

@smartnav is this problem resolved? I still get the same error which has been reported by @zorobabel.

soulInferno1996 commented 4 years ago

Go to yous fcm.js file -> there should be a function in line number 6 or 7 called admin.initializeApp(); create an if condition " if(!admin.apps.length) { } ". and cut paste admin.initializeAp inside it.

The reason for this problem was that the initializeApp method was getting called multiple times. Just add a condition which states that firebase will not be initialized if there is already an existing initialization

complete fcm.js code:

var admin = require("firebase-admin"); function FCM(key) { if(!key){ throw Error('Please provide the key file!'); }else { if(!admin.apps.length) { admin.initializeApp({ credential: admin.credential.cert(key) }); }

    this.send = function(payload, callback){
        if (!callback) {
            throw Error('Please provide a callback function');
        }
        else{
            if(!payload) callback(new Error('Please provide message object'))
            else {
                // See documentation on defining a message payload.
                  // var message = {
                  //   data: {
                  //     score: '850',
                  //     time: '2:45'
                  //   },
                  //   token: registrationToken
                  // };

                  // Send a message to the device corresponding to the provided
                  // registration token.
                  admin.messaging().send(payload)
                    .then((response) => {
                      // Response is a message ID string.
                      callback(null,response)
                      console.log('Successfully sent message:', response);
                    })
                    .catch((error) => {
                      callback(error)
                      console.log('Error sending message:', error);
                    });
                  }
              }
    }

    this.subscribeToTopic = function(registrationTokens, topic, callback) {
      admin.messaging().subscribeToTopic(registrationTokens, topic)
          .then(function(response) {
            // See the MessagingTopicManagementResponse reference documentation
            // for the contents of response.
            callback(null,response)
            console.log('Successfully subscribed to topic:', response);
          })
          .catch(function(error) {
            callback(error)
            console.log('Error subscribing to topic:', error);
          });
    }

    this.unsubscribeFromTopic = function(registrationTokens, topic, callback) {
      admin.messaging().unsubscribeFromTopic(registrationTokens, topic)
          .then(function(response) {
            // See the MessagingTopicManagementResponse reference documentation
            // for the contents of response.
            callback(null,response)
            console.log('Successfully unsubscribed to topic:', response);
          })
          .catch(function(error) {
            callback(error)
            console.log('Error unsubscribing to topic:', error);
          });
    }

    this.sendToMultipleTopic = function(payload, topics, callback) {
        multipleTopics(0, topics, payload, [], function(response) {
            callback(null, response);
        })
    }

    this.sendToMultipleToken = function(payload, tokens, callback) {
        multipleTokens(0, tokens, payload, [], function(response) {
            callback(null, response);
        })
    }
}

}

var multipleTokens = function(i, tokens, payload, arrayResult, callback) { if(tokens.length > i){ payload['token'] = tokens[i]; var arrayObj = { token : payload.token }; admin.messaging().send(payload) .then((response) => { arrayObj.response = 'Successfully sent message:'+ response; arrayResult.push(arrayObj); i = i+1; multipleTokens(i, tokens, payload, arrayResult, callback); //console.log(arrayResult); }) .catch((error) => { arrayObj.response = 'Error sending message:', error; arrayResult.push(arrayObj); i = i+1; multipleTokens(i, tokens, payload, arrayResult, callback); //console.log(arrayResult); }); }else { callback(arrayResult) } }

var multipleTopics = function(i, topics, payload, arrayResult, callback) { if(topics.length > i){ payload['topic'] = topics[i]; var arrayObj = { topic : payload.topic }; admin.messaging().send(payload) .then((response) => { arrayObj.response = 'Successfully sent message:'+ response; arrayResult.push(arrayObj); i = i+1; multipleTopics(i, topics, payload, arrayResult, callback); //console.log(arrayResult); }) .catch((error) => { arrayObj.response = 'Error sending message:', error; arrayResult.push(arrayObj); i = i+1; multipleTopics(i, topics, payload, arrayResult, callback); //console.log(arrayResult); }); }else { callback(arrayResult) } } module.exports = FCM;

susantadas commented 3 years ago

The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.