title: Cloud function error : fcm.googleapis.com network timeout. Please try again
body: <p>I tried to write a function to send a data message to users who are in a group whenever there is a new post in that group. Sometimes my code gives this error:</p>
  <pre><code>Couldn't send message to +XXXXXXXXXXXX:  { Error: fcm.googleapis.com network timeout. Please try again.  
  at FirebaseAppError.Error (native)
  at FirebaseAppError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
  at FirebaseAppError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28)
  at new FirebaseAppError (/user_code/node_modules/firebase-admin/lib/utils/error.js:119:28)
  at TLSSocket.&lt;anonymous&gt; (/user_code/node_modules/firebase-admin/lib/utils/api-request.js:151:51)
  at emitNone (events.js:86:13)
  at TLSSocket.emit (events.js:185:7)
  at TLSSocket.Socket._onTimeout (net.js:338:8)
  at ontimeout (timers.js:386:11)
  at tryOnTimeout (timers.js:250:5)  
  errorInfo:  
  { code: 'app/network-timeout',
   message: 'fcm.googleapis.com network timeout. Please try again.' },  
   codePrefix: 'app' }  
  </code></pre>
  <p>This is the cloud function I wrote -</p>
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  
  exports.sendNewPostNotifications = functions.database.ref('/classes/{classCode}/posts/{pushId}')
    .onCreate(event =&gt; {
      const classCode = event.params.classCode;
      const post = event.data.val();
  
      return admin.database().ref(`classes/${classCode}/name`).once('value').then(function(snapshot) {
        const className = snapshot.val();
  
        const payload = {
          'data': {
            'type': 'post',
            'post_id': post.time + &quot;&quot;,
            'post_class_name': className + &quot;&quot;,
            'post_class_code': classCode + &quot;&quot;,
            'post_title': post.title + &quot;&quot;,
            'post_description': post.description + &quot;&quot;,
            'post_postedByName': post.postedByName + &quot;&quot;
          }
        };
  
        return admin.database().ref(`classes/${classCode}/participants`).once('value').then(function(participantsSnapshot) {
          var promises = [];
          participantsSnapshot.forEach(function(participant) {
            const phone = participant.val().phone;
            var promise = admin.database().ref(`users/${phone}`).once('value').then(function(user) {
              return admin.messaging().sendToDevice(user.val().token, payload).catch(reason =&gt; {
                console.log(`Couldn't send message to ${phone} : `, reason);
              });
            }).catch(reason =&gt; {
              console.log('Reason : ', reason);
            });
            promises.push(promise);
          });
          return Promise.all(promises).catch(reason =&gt; {
            console.log('Reason : ', reason);
          });
        }).catch(reason =&gt; {
          console.log('Reason : ', reason);
        });
      });
    });
  </code></pre>
  <p>This functions even take up to 30 seconds to execute sometimes:</p>
  <pre><code>Function execution took 30371 ms, finished with status: 'ok'  
  </code></pre>
  <p>What should I do to correct this?</p>