云功能错误:fcm.googleapis.com网络超时。请再试一次

时间:2018-01-17 08:37:58

标签: javascript firebase firebase-cloud-messaging google-cloud-functions admin-sdk

我是JavaScriptFirebase cloud functions的新手。我尝试编写一个函数,只要该组中有新帖子,就会向组中的用户发送数据消息 但是,有时我的代码会出现此错误 -

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.<anonymous> (/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' }  

这是我写的云功能 -

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 => {
                                                        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 + "",
                                                                    'post_class_name' : className +  "",
                                                                    'post_class_code' : classCode + "",
                                                                    'post_title' : post.title + "",
                                                                    'post_description' : post.description + "",
                                                                    'post_postedByName' : post.postedByName + ""
                                                                }
                                                            };

                                                            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 => {
                                                                            console.log(`Couldn't send message to ${phone} : `  , reason);
                                                                        });
                                                                    }).catch(reason => {
                                                                        console.log('Reason : ', reason);
                                                                    });
                                                                    promises.push(promise);
                                                                });
                                                                return Promise.all(promises).catch(reason => {
                                                                    console.log('Reason : ',reason);
                                                                });
                                                            }).catch(reason => {
                                                                console.log('Reason : ',reason);
                                                            });
                                                        });
                                                    });  

此功能有时甚至需要30秒才能执行:

Function execution took 30371 ms, finished with status: 'ok'  

我该怎么做才能纠正这个问题?

0 个答案:

没有答案